Add moderator column to the DB and a DB versioning scheme

- Implements a DB versioning system utilizing user_version.
- Implements a moderator column to the bans table, for storing the moderator who issued a ban.
This commit is contained in:
MangosArentLiterature 2021-05-07 12:10:33 -05:00
parent 4117d6f3b0
commit ae808bd51b
2 changed files with 51 additions and 1 deletions

View File

@ -18,6 +18,8 @@
#ifndef BAN_MANAGER_H
#define BAN_MANAGER_H
#define DB_VERSION 1
#include <QDebug>
#include <QDateTime>
#include <QHostAddress>
@ -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

View File

@ -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::BanInfo> 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();