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}
 | 
			
		||||
)
 | 
			
		||||
add_custom_target(debug
 | 
			
		||||
    COMMAND valgrind ./core start
 | 
			
		||||
    COMMAND valgrind -s ./core start
 | 
			
		||||
    DEPENDS core
 | 
			
		||||
    WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
 | 
			
		||||
)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -11,7 +11,7 @@ database_init();
 | 
			
		|||
void
 | 
			
		||||
database_free();
 | 
			
		||||
 | 
			
		||||
int
 | 
			
		||||
void
 | 
			
		||||
database_migrate();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,5 @@
 | 
			
		|||
#ifndef CORE_LOGGER_H
 | 
			
		||||
#define CORE_LOGGER_H
 | 
			
		||||
#ifndef EMGAUWA_LOGGER_H
 | 
			
		||||
#define EMGAUWA_LOGGER_H
 | 
			
		||||
 | 
			
		||||
#include <stdio.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_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
 | 
			
		||||
(
 | 
			
		||||
    id      INTEGER
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,6 +1,7 @@
 | 
			
		|||
#include <stdio.h>
 | 
			
		||||
#include <stdarg.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
 | 
			
		||||
#include <config.h>
 | 
			
		||||
#include <logger.h>
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue