2019-09-22 00:34:22 +00:00
|
|
|
#include <helpers.h>
|
|
|
|
#include <globals.h>
|
2020-02-23 19:06:14 +00:00
|
|
|
#include <uuid/uuid.h>
|
2019-09-22 00:34:22 +00:00
|
|
|
#include <drogon/trantor/trantor/utils/Logger.h>
|
2020-02-23 19:06:14 +00:00
|
|
|
#include <drogon/utils/Utilities.h>
|
2019-09-22 00:34:22 +00:00
|
|
|
|
|
|
|
#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:
|
2019-09-24 21:33:35 +00:00
|
|
|
LOG_INFO << "Migrating LEVEL 0";
|
2019-09-22 00:34:22 +00:00
|
|
|
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";
|
|
|
|
}
|
|
|
|
|
2019-10-27 00:07:01 +00:00
|
|
|
return rc != SQLITE_DONE;
|
2019-09-22 00:34:22 +00:00
|
|
|
}
|