From 0dfa3d8aa2313b705f85c4730e74095ba9ffdee4 Mon Sep 17 00:00:00 2001 From: oranges Date: Thu, 29 Jun 2017 18:47:17 +1200 Subject: [PATCH] Adds a database versioning schema table (#28897) * Adds a database versioning schema table All it does right now is warn admins when the version in the db gets behind the defined version in the compile options file This also moves the db connection warning messages to the CheckSchemaVersion proc in world, out of the roundid proc as it's the first db proc called in a world round * Update database_changelog.txt * Update tgstation_schema.sql * Update tgstation_schema_prefixed.sql --- SQL/database_changelog.txt | 19 +++++++++++++++++++ SQL/tgstation_schema.sql | 11 +++++++++++ SQL/tgstation_schema_prefixed.sql | 11 +++++++++++ code/_compile_options.dm | 6 +++++- code/world.dm | 21 ++++++++++++++++++--- 5 files changed, 64 insertions(+), 4 deletions(-) diff --git a/SQL/database_changelog.txt b/SQL/database_changelog.txt index 59f8346b0fa6..7bc85d5c8531 100644 --- a/SQL/database_changelog.txt +++ b/SQL/database_changelog.txt @@ -1,3 +1,22 @@ +28 June 2017, by oranges +Added schema_revision to store the current db revision, why start at 3.0? + +because: +15:09 <+MrStonedOne> 1.0 was erro, 2.0 was when i removed erro_, 3.0 was when jordie made all the strings that hold numbers numbers + +CREATE TABLE `schema_revision` ( +`major` TINYINT(3) UNSIGNED NOT NULL , +`minor` TINYINT(3) UNSIGNED NOT NULL , +`date` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL, +PRIMARY KEY ( `major`,`minor` ) +) ENGINE = INNODB; + +INSERT INTO `schema_revision` (`major`, `minor`) VALUES (3, 0); + +Remember to add a prefix to the table name if you use them. + +---------------------------------------------------- + 26 June 2017, by Jordie0608 Modified table 'poll_option', adding the column 'default_percentage_calc'. diff --git a/SQL/tgstation_schema.sql b/SQL/tgstation_schema.sql index e1fe2738cd39..870417a2fdcb 100644 --- a/SQL/tgstation_schema.sql +++ b/SQL/tgstation_schema.sql @@ -387,6 +387,17 @@ CREATE TABLE `round` ( /*!40101 SET character_set_client = @saved_cs_client */; /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; +-- +-- Table structure for table `schema_revision` +-- +DROP TABLE IF EXISTS `schema_revision`; +CREATE TABLE `schema_revision` ( + `major` TINYINT(3) unsigned NOT NULL, + `minor` TINYINT(3) unsigned NOT NULL, + `date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + PRIMARY KEY (`major`, `minor`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1; + /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; diff --git a/SQL/tgstation_schema_prefixed.sql b/SQL/tgstation_schema_prefixed.sql index 333df8f50404..be5de60440bb 100644 --- a/SQL/tgstation_schema_prefixed.sql +++ b/SQL/tgstation_schema_prefixed.sql @@ -387,6 +387,17 @@ CREATE TABLE `SS13_round` ( /*!40101 SET character_set_client = @saved_cs_client */; /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; +-- +-- Table structure for table `SS13_schema_revision` +-- +DROP TABLE IF EXISTS `SS13_schema_revision`; +CREATE TABLE `SS13_schema_revision` ( + `major` TINYINT(3) unsigned NOT NULL, + `minor` TINYINT(3) unsigned NOT NULL, + `date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + PRIMARY KEY (`major`,`minor`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1; + /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; diff --git a/code/_compile_options.dm b/code/_compile_options.dm index 880276ddc8fb..f7e91246658e 100644 --- a/code/_compile_options.dm +++ b/code/_compile_options.dm @@ -70,4 +70,8 @@ #endif #define SERVICE_CMD_PARAM_KEY "serviceCommsKey" - \ No newline at end of file + +//Update this whenever the db schema changes +//make sure you add an update to the schema_version stable in the db changelog +#define DB_MAJOR_VERSION 3 +#define DB_MINOR_VERSION 0 diff --git a/code/world.dm b/code/world.dm index 1709d02c5e96..404f982adb88 100644 --- a/code/world.dm +++ b/code/world.dm @@ -24,6 +24,7 @@ config = new + CheckSchemaVersion() SetRoundID() SetupLogs() @@ -56,18 +57,32 @@ external_rsc_urls.Cut(i,i+1) #endif -/world/proc/SetRoundID() +/world/proc/CheckSchemaVersion() if(config.sql_enabled) if(SSdbcore.Connect()) log_world("Database connection established.") + var/datum/DBQuery/db_version = SSdbcore.NewQuery("SELECT major, minor FROM [format_table_name("schema_version")]") + db_version.Execute() + if(db_version.NextRow()) + var/db_major = db_version.item[1] + var/db_minor = db_version.item[2] + if(db_major < DB_MAJOR_VERSION || db_minor < DB_MINOR_VERSION) + message_admins("db schema ([db_major].[db_minor]) is behind latest tg schema version ([DB_MAJOR_VERSION].[DB_MINOR_VERSION]), this may lead to undefined behaviour or errors") + log_sql("db schema ([db_major].[db_minor]) is behind latest tg schema version ([DB_MAJOR_VERSION].[DB_MINOR_VERSION]), this may lead to undefined behaviour or errors") + else + message_admins("Could not get schema version from db") + else + log_world("Your server failed to establish a connection with the database.") + +/world/proc/SetRoundID() + if(config.sql_enabled) + if(SSdbcore.Connect()) var/datum/DBQuery/query_round_start = SSdbcore.NewQuery("INSERT INTO [format_table_name("round")] (start_datetime, server_ip, server_port) VALUES (Now(), INET_ATON(IF('[world.internet_address]' LIKE '', '0', '[world.internet_address]')), '[world.port]')") query_round_start.Execute() var/datum/DBQuery/query_round_last_id = SSdbcore.NewQuery("SELECT LAST_INSERT_ID()") query_round_last_id.Execute() if(query_round_last_id.NextRow()) GLOB.round_id = query_round_last_id.item[1] - else - log_world("Your server failed to establish a connection with the database.") /world/proc/SetupLogs() GLOB.log_directory = "data/logs/[time2text(world.realtime, "YYYY/MM/DD")]/round-"