fix: use PRAGMA user_version to track migrations

fix: minor fixes
This commit is contained in:
Tobias Reisinger 2020-08-21 14:03:23 +02:00
parent 521e82b7b5
commit 3117427f1f
6 changed files with 15 additions and 36 deletions

View file

@ -20,10 +20,7 @@ database_init()
exit(1);
}
if(database_migrate())
{
exit(1);
}
database_migrate();
sqlite3_exec(global_database, "PRAGMA foreign_keys = ON", 0, 0, 0);
in_transaction = 0;
@ -35,13 +32,13 @@ database_free()
sqlite3_close(global_database);
}
int
void
database_migrate()
{
uint16_t version_num = 0;
int s, rc;
sqlite3_stmt *stmt;
sqlite3_prepare_v2(global_database, "SELECT version_num FROM meta LIMIT 1;", -1, &stmt, NULL);
sqlite3_prepare_v2(global_database, "PRAGMA user_version;", -1, &stmt, NULL);
s = sqlite3_step(stmt);
if (s == SQLITE_ROW)
{
@ -65,32 +62,19 @@ database_migrate()
if(rc)
{
LOGGER_CRIT("couldn't migrate LEVEL 0 (%s)\n", err_msg);
break;
exit(1);
}
new_version_num = 1;
default:
break;
}
if(version_num == 0)
{
sqlite3_prepare_v2(global_database, "INSERT INTO meta (version_num) VALUES (?1);", -1, &stmt, NULL);
}
else
{
sqlite3_prepare_v2(global_database, "UPDATE meta SET version_num=?1;", -1, &stmt, NULL);
}
sqlite3_bind_int(stmt, 1, new_version_num);
char pragma_query[32];
sprintf(pragma_query, "PRAGMA user_version=%d;", new_version_num);
sqlite3_exec(global_database, pragma_query, 0, 0, 0);
LOGGER_DEBUG("storing new user_version %d\n", new_version_num);
rc = sqlite3_step(stmt);
if (rc != SQLITE_DONE)
{
LOGGER_CRIT("couldn't write new schema version\n");
}
sqlite3_finalize(stmt);
return rc != SQLITE_DONE;
return;
}
int