diff --git a/code/controllers/subsystem/dbcore.dm b/code/controllers/subsystem/dbcore.dm index bbf1fef8d9..a4a6ca8f51 100644 --- a/code/controllers/subsystem/dbcore.dm +++ b/code/controllers/subsystem/dbcore.dm @@ -1,6 +1,6 @@ SUBSYSTEM_DEF(dbcore) name = "Database" - flags = SS_NO_INIT|SS_NO_FIRE + flags = SS_NO_FIRE init_order = INIT_ORDER_DBCORE var/const/FAILED_DB_CONNECTION_CUTOFF = 5 @@ -17,6 +17,9 @@ SUBSYSTEM_DEF(dbcore) var/const/IS_NOT_NULL = 4 var/const/IS_PRIMARY_KEY = 8 var/const/IS_UNSIGNED = 16 + var/schema_mismatch = 0 + var/db_minor = 0 + var/db_major = 0 // TODO: Investigate more recent type additions and see if I can handle them. - Nadrew var/_db_con// This variable contains a reference to the actual database connection. @@ -26,6 +29,16 @@ SUBSYSTEM_DEF(dbcore) if(!_db_con) _db_con = _dm_db_new_con() +/datum/controller/subsystem/dbcore/Initialize() + //We send warnings to the admins during subsystem init, as the clients will be New'd and messages + //will queue properly with goonchat + switch(schema_mismatch) + if(1) + message_admins("Database schema ([db_major].[db_minor]) doesn't match the latest schema version ([DB_MAJOR_VERSION].[DB_MINOR_VERSION]), this may lead to undefined behaviour or errors") + if(2) + message_admins("Could not get schema version from database") + + /datum/controller/subsystem/dbcore/Recover() _db_con = SSdbcore._db_con @@ -69,6 +82,36 @@ SUBSYSTEM_DEF(dbcore) log_sql("Connect() failed | [ErrorMsg()]") ++failed_connections +/datum/controller/subsystem/dbcore/proc/CheckSchemaVersion() + if(CONFIG_GET(flag/sql_enabled)) + if(SSdbcore.Connect()) + log_world("Database connection established.") + var/datum/DBQuery/query_db_version = SSdbcore.NewQuery("SELECT major, minor FROM [format_table_name("schema_revision")] ORDER BY date DESC LIMIT 1") + query_db_version.Execute() + if(query_db_version.NextRow()) + db_major = text2num(query_db_version.item[1]) + db_minor = text2num(query_db_version.item[2]) + if(db_major != DB_MAJOR_VERSION || db_minor != DB_MINOR_VERSION) + schema_mismatch = 1 // flag admin message about mismatch + log_sql("Database schema ([db_major].[db_minor]) doesn't match the latest schema version ([DB_MAJOR_VERSION].[DB_MINOR_VERSION]), this may lead to undefined behaviour or errors") + else + schema_mismatch = 2 //flag admin message about no schema version + log_sql("Could not get schema version from database") + else + log_sql("Your server failed to establish a connection with the database.") + else + log_sql("Database is not enabled in configuration.") + +/datum/controller/subsystem/dbcore/proc/SetRoundID() + if(CONFIG_GET(flag/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] + /datum/controller/subsystem/dbcore/proc/Disconnect() failed_connections = 0 return _dm_db_close(_db_con) diff --git a/code/game/world.dm b/code/game/world.dm index 22829dd963..2da8c849de 100644 --- a/code/game/world.dm +++ b/code/game/world.dm @@ -4,6 +4,8 @@ GLOBAL_VAR(security_mode) GLOBAL_VAR(restart_counter) GLOBAL_PROTECT(security_mode) +//This happens after the Master subsystem news (it's a global datum) +//So subsystems globals exist, but are not initialised /world/New() log_world("World loaded at [time_stamp()]") @@ -17,9 +19,10 @@ GLOBAL_PROTECT(security_mode) new /datum/controller/configuration - CheckSchemaVersion() - SetRoundID() - + //SetupLogs depends on the RoundID, so lets check + //DB schema and set RoundID if we can + SSdbcore.CheckSchemaVersion() + SSdbcore.SetRoundID() SetupLogs() SERVER_TOOLS_ON_NEW @@ -55,36 +58,6 @@ GLOBAL_PROTECT(security_mode) GLOB.external_rsc_urls.Cut(i,i+1) #endif -/world/proc/CheckSchemaVersion() - if(CONFIG_GET(flag/sql_enabled)) - if(SSdbcore.Connect()) - log_world("Database connection established.") - var/datum/DBQuery/query_db_version = SSdbcore.NewQuery("SELECT major, minor FROM [format_table_name("schema_revision")] ORDER BY date DESC LIMIT 1") - query_db_version.Execute() - if(query_db_version.NextRow()) - var/db_major = text2num(query_db_version.item[1]) - var/db_minor = text2num(query_db_version.item[2]) - if(db_major != DB_MAJOR_VERSION || db_minor != DB_MINOR_VERSION) - message_admins("Database schema ([db_major].[db_minor]) doesn't match the latest schema version ([DB_MAJOR_VERSION].[DB_MINOR_VERSION]), this may lead to undefined behaviour or errors") - log_sql("Database schema ([db_major].[db_minor]) doesn't match the latest 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 database") - log_sql("Could not get schema version from database") - else - log_sql("Your server failed to establish a connection with the database.") - else - log_sql("Database is not enabled in configuration.") - -/world/proc/SetRoundID() - if(CONFIG_GET(flag/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] - /world/proc/SetupLogs() GLOB.log_directory = "data/logs/[time2text(world.realtime, "YYYY/MM/DD")]/round-" if(GLOB.round_id)