diff --git a/include/db_manager.h b/include/db_manager.h index b696dfe..d5e14ae 100644 --- a/include/db_manager.h +++ b/include/db_manager.h @@ -18,6 +18,8 @@ #ifndef BAN_MANAGER_H #define BAN_MANAGER_H +#define DB_VERSION 1 + #include #include #include @@ -268,6 +270,25 @@ private: * @brief The backing database that stores user details. */ QSqlDatabase db; + + /** + * @brief The current server DB version. + */ + int db_version; + + /** + * @brief checkVersion Checks the current server DB version. + * + * @return Returns the server DB version. + */ + int checkVersion(); + + /** + * @brief updateDB Updates the server DB to the latest version. + * + * @param current_version The current DB version. + */ + void updateDB(int current_version); }; #endif // BAN_MANAGER_H diff --git a/src/db_manager.cpp b/src/db_manager.cpp index 861ec1a..b8bf836 100644 --- a/src/db_manager.cpp +++ b/src/db_manager.cpp @@ -24,8 +24,13 @@ DBManager::DBManager() : db.setDatabaseName("config/akashi.db"); if (!db.open()) qCritical() << "Database Error:" << db.lastError(); - QSqlQuery create_ban_table("CREATE TABLE IF NOT EXISTS bans ('ID' INTEGER, 'IPID' TEXT, 'HDID' TEXT, 'IP' TEXT, 'TIME' INTEGER, 'REASON' TEXT, 'DURATION' INTEGER, PRIMARY KEY('ID' AUTOINCREMENT))"); + qDebug() << QString::number(DB_VERSION); + db_version = checkVersion(); + qDebug() << QString::number(db_version); + QSqlQuery create_ban_table("CREATE TABLE IF NOT EXISTS bans ('ID' INTEGER, 'IPID' TEXT, 'HDID' TEXT, 'IP' TEXT, 'TIME' INTEGER, 'REASON' TEXT, 'DURATION' INTEGER, 'MODERATOR' TEXT, PRIMARY KEY('ID' AUTOINCREMENT))"); QSqlQuery create_user_table("CREATE TABLE IF NOT EXISTS users ('ID' INTEGER, 'USERNAME' TEXT, 'SALT' TEXT, 'PASSWORD' TEXT, 'ACL' TEXT, PRIMARY KEY('ID' AUTOINCREMENT))"); + if (db_version != DB_VERSION) + updateDB(db_version); } bool DBManager::isIPBanned(QHostAddress ip) @@ -361,6 +366,30 @@ QList DBManager::getBanInfo(QString lookup_type, QString id) return return_list; } +int DBManager::checkVersion() +{ + QSqlQuery query; + query.prepare("PRAGMA user_version"); + query.exec(); + if (query.first()) { + return query.value(0).toInt(); + } + else { + return 0; + } +} + +void DBManager::updateDB(int current_version) +{ + switch (current_version) { + case 0: + QSqlQuery("ALTER TABLE bans ADD COLUMN MODERATOR TEXT"); + case 1: + QSqlQuery ("PRAGMA user_version = " + QString::number(DB_VERSION)); + break; + } +} + DBManager::~DBManager() { db.close();