core-legacy/helpers/migrate_sql.cc
Tobias Reisinger 907b52160a add: migration from main exe
add: sql files embedded in exe
2019-09-22 02:34:22 +02:00

63 lines
1.4 KiB
C++

#include <helpers.h>
#include <globals.h>
#include <drogon/trantor/trantor/utils/Logger.h>
#include <config.h>
#include <sql/migration_0.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:
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;
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";
}
return rc == SQLITE_DONE;
}