add: basic macro functions

add: better migration handling (transactions)
This commit is contained in:
Tobias Reisinger 2020-08-29 22:58:02 +02:00
parent 7275d66c86
commit f67b7e9e0f
7 changed files with 122 additions and 36 deletions

View file

@ -44,24 +44,18 @@ add_custom_target(run
WORKING_DIRECTORY ${CMAKE_BINARY_DIR} WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
) )
add_custom_target(test
COMMAND ./run_tests.sh ${CMAKE_BINARY_DIR}/core "--leak-check=full --show-leak-kinds=all --track-origins=yes"
DEPENDS core
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests
)
add_custom_target(docs add_custom_target(docs
COMMAND doxygen COMMAND doxygen
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
) )
add_custom_target(test IF(CMAKE_BUILD_TYPE MATCHES Debug)
COMMAND ./run_tests.sh ${CMAKE_BINARY_DIR}/core "--leak-check=full --show-leak-kinds=all --track-origins=yes"
DEPENDS core
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests
)
add_custom_target(test-callgrind
COMMAND ./run_tests.sh ${CMAKE_BINARY_DIR}/core "--tool=callgrind"
DEPENDS core
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests
)
IF(CMAKE_BUILD_TYPE MATCHES DEBUG)
message("debug mode") message("debug mode")
add_custom_target(debug add_custom_target(debug
COMMAND valgrind -s ./core start COMMAND valgrind -s ./core start
@ -83,4 +77,10 @@ IF(CMAKE_BUILD_TYPE MATCHES DEBUG)
DEPENDS test DEPENDS test
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
) )
ENDIF(CMAKE_BUILD_TYPE MATCHES DEBUG)
add_custom_target(test-callgrind
COMMAND ./run_tests.sh ${CMAKE_BINARY_DIR}/core "--tool=callgrind"
DEPENDS core
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests
)
ENDIF(CMAKE_BUILD_TYPE MATCHES Debug)

View file

@ -0,0 +1,7 @@
#ifndef CORE_JUNCTION_MACRO_H
#define CORE_JUNCTION_MACRO_H
int
junction_macro_insert(int macro_id, int relay_id, int schedule_id, uint8_t weekday);
#endif /* CORE_JUNCTION_MACRO_H */

View file

@ -1,3 +1,5 @@
-- a key-value table used for the json-cache
CREATE TABLE cache ( CREATE TABLE cache (
key STRING key STRING
PRIMARY KEY, PRIMARY KEY,

View file

@ -1,4 +1,6 @@
create table controllers -- base migration
CREATE TABLE controllers
( (
id INTEGER id INTEGER
PRIMARY KEY PRIMARY KEY
@ -14,7 +16,7 @@ create table controllers
NOT NULL NOT NULL
); );
create table relays CREATE TABLE relays
( (
id INTEGER id INTEGER
PRIMARY KEY PRIMARY KEY
@ -28,7 +30,7 @@ create table relays
ON DELETE CASCADE ON DELETE CASCADE
); );
create table schedules CREATE TABLE schedules
( (
id INTEGER id INTEGER
PRIMARY KEY PRIMARY KEY
@ -40,7 +42,7 @@ create table schedules
periods BLOB periods BLOB
); );
create table tags CREATE TABLE tags
( (
id INTEGER id INTEGER
PRIMARY KEY PRIMARY KEY
@ -50,7 +52,7 @@ create table tags
UNIQUE UNIQUE
); );
create table junction_tag CREATE TABLE junction_tag
( (
tag_id INTEGER tag_id INTEGER
NOT NULL NOT NULL
@ -64,7 +66,7 @@ create table junction_tag
ON DELETE CASCADE ON DELETE CASCADE
); );
create table junction_relay_schedule CREATE TABLE junction_relay_schedule
( (
weekday SMALLINT weekday SMALLINT
NOT NULL, NOT NULL,

28
sql/migration_1.sql Normal file
View file

@ -0,0 +1,28 @@
-- migration to add macros
CREATE TABLE macros
(
id INTEGER
PRIMARY KEY
AUTOINCREMENT,
uid BLOB
NOT NULL
UNIQUE,
name VARCHAR(128)
);
CREATE TABLE junction_macro
(
macro_id INTEGER
NOT NULL
REFERENCES macros (id)
ON DELETE CASCADE,
relay_id INTEGER
REFERENCES relays (id)
ON DELETE CASCADE,
schedule_id INTEGER
REFERENCES schedules (id)
ON DELETE CASCADE,
weekday SMALLINT
NOT NULL
);

View file

@ -5,6 +5,7 @@
#include <database.h> #include <database.h>
#include <sql/migration_0.h> #include <sql/migration_0.h>
#include <sql/migration_1.h>
sqlite3 *global_database; sqlite3 *global_database;
static int in_transaction; static int in_transaction;
@ -32,11 +33,35 @@ database_free()
sqlite3_close(global_database); sqlite3_close(global_database);
} }
static void
database_migrate_step_simple(int level, const char* sql_migration)
{
LOGGER_INFO("migrating LEVEL %d\n", level);
char* err_msg;
sqlite3_exec(global_database, "BEGIN TRANSACTION;", NULL, NULL, NULL);
int rc = sqlite3_exec(global_database, sql_migration, NULL, NULL, &err_msg);
if(rc)
{
LOGGER_CRIT("couldn't migrate LEVEL %d (%s)\n", level, err_msg);
sqlite3_exec(global_database, "ROLLBACK TRANSACTION;", NULL, NULL, NULL);
exit(1);
}
LOGGER_DEBUG("storing new user_version %d\n", level + 1);
char pragma_query[32];
sprintf(pragma_query, "PRAGMA user_version=%d;", level + 1);
sqlite3_exec(global_database, pragma_query, 0, 0, 0);
sqlite3_exec(global_database, "COMMIT TRANSACTION;", NULL, NULL, NULL);
}
void void
database_migrate() database_migrate()
{ {
uint16_t version_num = 0; uint16_t version_num = 0;
int s, rc; int s;
sqlite3_stmt *stmt; sqlite3_stmt *stmt;
sqlite3_prepare_v2(global_database, "PRAGMA user_version;", -1, &stmt, NULL); sqlite3_prepare_v2(global_database, "PRAGMA user_version;", -1, &stmt, NULL);
s = sqlite3_step(stmt); s = sqlite3_step(stmt);
@ -49,31 +74,20 @@ database_migrate()
version_num = 0; version_num = 0;
} }
uint16_t new_version_num = version_num;
char* err_msg;
sqlite3_finalize(stmt); sqlite3_finalize(stmt);
switch(version_num) switch(version_num)
{ {
case 0: case 0:
LOGGER_INFO("migrating LEVEL 0\n"); database_migrate_step_simple(0, (const char*)sql_migration_0_sql);
rc = sqlite3_exec(global_database, (const char *)sql_migration_0_sql, NULL, NULL, &err_msg); __attribute__ ((fallthrough));
if(rc) case 1:
{ database_migrate_step_simple(1, (const char*)sql_migration_1_sql);
LOGGER_CRIT("couldn't migrate LEVEL 0 (%s)\n", err_msg); __attribute__ ((fallthrough));
exit(1);
}
new_version_num = 1;
default: default:
break; break;
} }
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);
return; return;
} }

View file

@ -0,0 +1,33 @@
#include <stdlib.h>
#include <stdint.h>
#include <stddef.h>
#include <models/junction_macro.h>
#include <logger.h>
#include <macros.h>
#include <database.h>
int
junction_macro_insert(int macro_id, int relay_id, int schedule_id, uint8_t weekday)
{
int rc;
sqlite3_stmt *stmt;
sqlite3_prepare_v2(global_database, "INSERT INTO junction_macro(macro_id, schedule_id, relay_id, weekday) values (?1, ?2, ?3);", -1, &stmt, NULL);
sqlite3_bind_int(stmt, 1, macro_id);
sqlite3_bind_int(stmt, 2, relay_id);
sqlite3_bind_int(stmt, 3, schedule_id);
sqlite3_bind_int(stmt, 4, weekday);
rc = sqlite3_step(stmt);
if (rc != SQLITE_DONE)
{
LOGGER_ERR("error inserting data: %s", sqlite3_errmsg(global_database));
return 1;
}
sqlite3_finalize(stmt);
return 0;
}