core-legacy/helpers/migrate_sql.cc

75 lines
1.8 KiB
C++
Raw Normal View History

#include <helpers.h>
#include <globals.h>
#include <drogon/trantor/trantor/utils/Logger.h>
#include <config.h>
#include <sql/migration_0.h>
#include <sql/migration_1.h>
int
helpers::migrate_sql()
{
uint16_t version_num = 0;
int s, rc;
sqlite3_stmt *stmt;
sqlite3_prepare_v2(globals::db, "SELECT version_num FROM meta LIMIT 1;", -1, &stmt, nullptr);
s = sqlite3_step(stmt);
if (s == SQLITE_ROW)
{
version_num = sqlite3_column_int(stmt, 0);
}
else
{
version_num = 0;
}
uint16_t new_version_num = version_num;
char* err_msg;
char* sql;
sqlite3_finalize(stmt);
switch(version_num)
{
case 0:
LOG_INFO << "Migrating LEVEL 0";
rc = sqlite3_exec(globals::db, (const char *)sql_migration_0_sql, nullptr, nullptr, &err_msg);
if(rc != 0)
{
LOG_FATAL << "Couldn't migrate LEVEL 0 (" << err_msg << ")";
break;
}
new_version_num = 1;
case 1:
LOG_INFO << "Migrating LEVEL 1";
rc = sqlite3_exec(globals::db, (const char *)sql_migration_1_sql, nullptr, nullptr, &err_msg);
if(rc != 0)
{
LOG_FATAL << "Couldn't migrate LEVEL 1 (" << err_msg << ")";
break;
}
new_version_num = 2;
default:
break;
}
if(version_num == 0)
{
sqlite3_prepare_v2(globals::db, "INSERT INTO meta (version_num) VALUES (?1);", -1, &stmt, nullptr);
}
else
{
sqlite3_prepare_v2(globals::db, "UPDATE meta SET version_num=?1;", -1, &stmt, nullptr);
}
sqlite3_bind_int(stmt, 1, new_version_num);
rc = sqlite3_step(stmt);
if (rc != SQLITE_DONE)
{
LOG_FATAL << "Couldn't write new Schema Version";
}
2019-10-27 00:07:01 +00:00
return rc != SQLITE_DONE;
}