fix: use PRAGMA user_version to track migrations
fix: minor fixes
This commit is contained in:
parent
521e82b7b5
commit
3117427f1f
6 changed files with 15 additions and 36 deletions
|
@ -45,7 +45,7 @@ add_custom_target(run
|
||||||
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
|
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
|
||||||
)
|
)
|
||||||
add_custom_target(debug
|
add_custom_target(debug
|
||||||
COMMAND valgrind ./core start
|
COMMAND valgrind -s ./core start
|
||||||
DEPENDS core
|
DEPENDS core
|
||||||
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
|
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
|
||||||
)
|
)
|
||||||
|
|
|
@ -11,7 +11,7 @@ database_init();
|
||||||
void
|
void
|
||||||
database_free();
|
database_free();
|
||||||
|
|
||||||
int
|
void
|
||||||
database_migrate();
|
database_migrate();
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
#ifndef CORE_LOGGER_H
|
#ifndef EMGAUWA_LOGGER_H
|
||||||
#define CORE_LOGGER_H
|
#define EMGAUWA_LOGGER_H
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
@ -19,4 +19,4 @@ logger_log(int level, const char *filename, int line, const char *func, const ch
|
||||||
#define LOGGER_CRIT(...) logger_log(LOG_CRIT , __FILE__, __LINE__, __func__, ##__VA_ARGS__)
|
#define LOGGER_CRIT(...) logger_log(LOG_CRIT , __FILE__, __LINE__, __func__, ##__VA_ARGS__)
|
||||||
#define LOGGER_EMERG(...) logger_log(LOG_EMERG , __FILE__, __LINE__, __func__, ##__VA_ARGS__)
|
#define LOGGER_EMERG(...) logger_log(LOG_EMERG , __FILE__, __LINE__, __func__, ##__VA_ARGS__)
|
||||||
|
|
||||||
#endif //CORE_LOGGER_H
|
#endif //EMGAUWA_LOGGER_H
|
||||||
|
|
|
@ -1,9 +1,3 @@
|
||||||
create table meta
|
|
||||||
(
|
|
||||||
version_num INTEGER
|
|
||||||
NOT NULL
|
|
||||||
);
|
|
||||||
|
|
||||||
create table controllers
|
create table controllers
|
||||||
(
|
(
|
||||||
id INTEGER
|
id INTEGER
|
||||||
|
|
|
@ -20,10 +20,7 @@ database_init()
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(database_migrate())
|
database_migrate();
|
||||||
{
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
sqlite3_exec(global_database, "PRAGMA foreign_keys = ON", 0, 0, 0);
|
sqlite3_exec(global_database, "PRAGMA foreign_keys = ON", 0, 0, 0);
|
||||||
in_transaction = 0;
|
in_transaction = 0;
|
||||||
|
@ -35,13 +32,13 @@ database_free()
|
||||||
sqlite3_close(global_database);
|
sqlite3_close(global_database);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
void
|
||||||
database_migrate()
|
database_migrate()
|
||||||
{
|
{
|
||||||
uint16_t version_num = 0;
|
uint16_t version_num = 0;
|
||||||
int s, rc;
|
int s, rc;
|
||||||
sqlite3_stmt *stmt;
|
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);
|
s = sqlite3_step(stmt);
|
||||||
if (s == SQLITE_ROW)
|
if (s == SQLITE_ROW)
|
||||||
{
|
{
|
||||||
|
@ -65,32 +62,19 @@ database_migrate()
|
||||||
if(rc)
|
if(rc)
|
||||||
{
|
{
|
||||||
LOGGER_CRIT("couldn't migrate LEVEL 0 (%s)\n", err_msg);
|
LOGGER_CRIT("couldn't migrate LEVEL 0 (%s)\n", err_msg);
|
||||||
break;
|
exit(1);
|
||||||
}
|
}
|
||||||
new_version_num = 1;
|
new_version_num = 1;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(version_num == 0)
|
char pragma_query[32];
|
||||||
{
|
sprintf(pragma_query, "PRAGMA user_version=%d;", new_version_num);
|
||||||
sqlite3_prepare_v2(global_database, "INSERT INTO meta (version_num) VALUES (?1);", -1, &stmt, NULL);
|
sqlite3_exec(global_database, pragma_query, 0, 0, 0);
|
||||||
}
|
LOGGER_DEBUG("storing new user_version %d\n", new_version_num);
|
||||||
else
|
|
||||||
{
|
|
||||||
sqlite3_prepare_v2(global_database, "UPDATE meta SET version_num=?1;", -1, &stmt, NULL);
|
|
||||||
}
|
|
||||||
sqlite3_bind_int(stmt, 1, new_version_num);
|
|
||||||
|
|
||||||
rc = sqlite3_step(stmt);
|
return;
|
||||||
if (rc != SQLITE_DONE)
|
|
||||||
{
|
|
||||||
LOGGER_CRIT("couldn't write new schema version\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
sqlite3_finalize(stmt);
|
|
||||||
|
|
||||||
return rc != SQLITE_DONE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include <config.h>
|
#include <config.h>
|
||||||
#include <logger.h>
|
#include <logger.h>
|
||||||
|
|
Loading…
Reference in a new issue