diff options
author | Robert Adam <dev@robert-adam.de> | 2020-05-29 14:02:37 +0300 |
---|---|---|
committer | Robert Adam <dev@robert-adam.de> | 2020-05-29 14:02:37 +0300 |
commit | c4c358cc7a7fe6ef91a37390398aceeb0348ef44 (patch) | |
tree | 75fe02767e3f980dcefa87ca1b649b73288c8ff6 /src/murmur/ServerDB.cpp | |
parent | 50aea07d6ae7887994b35a190607a34382f3608a (diff) |
src/murmur/ServerDB: Turned db version into static field & added some documentation to init/update process
Diffstat (limited to 'src/murmur/ServerDB.cpp')
-rw-r--r-- | src/murmur/ServerDB.cpp | 25 |
1 files changed, 23 insertions, 2 deletions
diff --git a/src/murmur/ServerDB.cpp b/src/murmur/ServerDB.cpp index ac0204a63..9f5402796 100644 --- a/src/murmur/ServerDB.cpp +++ b/src/murmur/ServerDB.cpp @@ -215,13 +215,18 @@ ServerDB::ServerDB() { int version = 0; + // Make sure a table called "meta" is present + // We use the meta table to keep track of various meta information such as the + // database structure version this database conforms to. if (Meta::mp.qsDBDriver == "QSQLITE") SQLDO("CREATE TABLE IF NOT EXISTS `%1meta` (`keystring` TEXT PRIMARY KEY, `value` TEXT)"); else if (Meta::mp.qsDBDriver == "QPSQL") SQLQUERY("CREATE TABLE IF NOT EXISTS `%1meta` (`keystring` varchar(255) PRIMARY KEY, `value` varchar(255))"); else + // MySQL SQLDO("CREATE TABLE IF NOT EXISTS `%1meta`(`keystring` varchar(255) PRIMARY KEY, `value` varchar(255)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin"); + // Query the database structure version the existing database conforms to SQLDO("SELECT `value` FROM `%1meta` WHERE `keystring` = 'version'"); if (query.next()) @@ -229,9 +234,17 @@ ServerDB::ServerDB() { loadOrSetupMetaPBKDF2IterationCount(query); - if (version < 6) { + // Check if the database structure conforms to what this version of the code expects yb comparing to + // DB_STRUCTURE_VERSION. If the queried version is less than that, we might have to perform compatibility + // changes or create the tables in the first place. + if (version < DB_STRUCTURE_VERSION) { if (version > 0) { + // A version > 0 means that there are tables in the DB already but they don't conform to the + // most recent structure. That means that we have to update them. + // Before doing that though we create backups of the existing tables in case something goes wrong. qWarning("Renaming old tables..."); + // %s is the table prefix we are using and %2 is the upgrade suffix we defined above. + // See ServerDB::query for mor info on the %-notation. SQLQUERY("ALTER TABLE `%1servers` RENAME TO `%1servers%2`"); if (version < 2) SQLMAY("ALTER TABLE `%1log` RENAME TO `%1slog`"); @@ -254,6 +267,7 @@ ServerDB::ServerDB() { } } + // Now we generate new tables that conform to the state-of-the-art structure qWarning("Generating new tables..."); if (Meta::mp.qsDBDriver == "QSQLITE") { if (version > 0) { @@ -438,6 +452,7 @@ ServerDB::ServerDB() { SQLQUERY("CREATE TABLE `%1bans` (`server_id` INTEGER NOT NULL, `base` BYTEA, `mask` INTEGER, `name` varchar(255), `hash` CHAR(40), `reason` TEXT, `start` TIMESTAMP, `duration` INTEGER)"); SQLQUERY("ALTER TABLE `%1bans` ADD CONSTRAINT `%1bans_del_server` FOREIGN KEY(`server_id`) REFERENCES `%1servers`(`server_id`) ON DELETE CASCADE"); } else { + // MySQL if (version > 0) { typedef QPair<QString, QString> qsp; QList<qsp> qlForeignKeys; @@ -505,9 +520,15 @@ ServerDB::ServerDB() { SQLDO("CREATE TABLE `%1bans` (`server_id` INTEGER NOT NULL, `base` BINARY(16), `mask` INTEGER, `name` varchar(255), `hash` CHAR(40), `reason` TEXT, `start` DATETIME, `duration` INTEGER) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin"); SQLDO("ALTER TABLE `%1bans` ADD CONSTRAINT `%1bans_del_server` FOREIGN KEY(`server_id`) REFERENCES `%1servers`(`server_id`) ON DELETE CASCADE"); } + if (version == 0) { + // The database was empty until we started populating it so the first (and so far only) + // entries in it will be the server ID (we know it must be 1 as there has been no previous + // server) as well as the version info about the database structure version. SQLDO("INSERT INTO `%1servers` (`server_id`) VALUES(1)"); - SQLDO("INSERT INTO `%1meta` (`keystring`, `value`) VALUES('version','6')"); + // Calling this function is the same as using the SQLDO macro + ServerDB::exec(query, QLatin1String("INSERT INTO `%1meta` (`keystring`, `value`) ") + + QLatin1String("VALUES('version','%1')").arg(QString::number(DB_STRUCTURE_VERSION)), true); } else { qWarning("Importing old data..."); |