add: migration from main exe
add: sql files embedded in exe
This commit is contained in:
		
							parent
							
								
									d47a12dbc5
								
							
						
					
					
						commit
						907b52160a
					
				
					 8 changed files with 90 additions and 28 deletions
				
			
		
							
								
								
									
										2
									
								
								.gitignore
									
										
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.gitignore
									
										
									
									
										vendored
									
									
								
							| 
						 | 
				
			
			@ -2,4 +2,6 @@ build
 | 
			
		|||
cmake-build-debug
 | 
			
		||||
.idea
 | 
			
		||||
 | 
			
		||||
sql/*.h
 | 
			
		||||
 | 
			
		||||
core.sqlite
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -61,5 +61,8 @@ endforeach()
 | 
			
		|||
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
 | 
			
		||||
add_executable(core ${SRC_DIR} ${CTL_SRC} ${FILTER_SRC} ${VIEWSRC} ${PLUGIN_SRC} ${MODEL_SRC} ${HELPER_SRC})
 | 
			
		||||
 | 
			
		||||
add_custom_target(compiled_migrations ${CMAKE_CURRENT_SOURCE_DIR}/compile_migrations.sh)
 | 
			
		||||
add_dependencies(core compiled_migrations)
 | 
			
		||||
 | 
			
		||||
add_subdirectory(drogon)
 | 
			
		||||
target_link_libraries(${PROJECT_NAME} PRIVATE drogon)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										13
									
								
								compile_migrations.sh
									
										
									
									
									
										Executable file
									
								
							
							
						
						
									
										13
									
								
								compile_migrations.sh
									
										
									
									
									
										Executable file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,13 @@
 | 
			
		|||
#!/usr/bin/env bash
 | 
			
		||||
 | 
			
		||||
cd "$(dirname "$0")";
 | 
			
		||||
 | 
			
		||||
migration_num=0;
 | 
			
		||||
 | 
			
		||||
while [ -f sql/migration_$migration_num.sql ]
 | 
			
		||||
do
 | 
			
		||||
  xxd -i sql/migration_$migration_num.sql | sed 's/\([0-9a-f]\)$/\0, 0x00/' > sql/migration_$migration_num.h
 | 
			
		||||
  ((migration_num++));
 | 
			
		||||
done;
 | 
			
		||||
 | 
			
		||||
cd $PWD;
 | 
			
		||||
| 
						 | 
				
			
			@ -35,6 +35,9 @@ namespace helpers
 | 
			
		|||
 | 
			
		||||
    int
 | 
			
		||||
    open_tcp_connection(char* host, char* port);
 | 
			
		||||
 | 
			
		||||
    int
 | 
			
		||||
    migrate_sql();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif //EMGAUWA_CORE_HELPERS_H
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										63
									
								
								helpers/migrate_sql.cc
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										63
									
								
								helpers/migrate_sql.cc
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,63 @@
 | 
			
		|||
#include <helpers.h>
 | 
			
		||||
#include <globals.h>
 | 
			
		||||
#include <drogon/trantor/trantor/utils/Logger.h>
 | 
			
		||||
 | 
			
		||||
#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:
 | 
			
		||||
            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";
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return rc == SQLITE_DONE;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										5
									
								
								main.cc
									
										
									
									
									
								
							
							
						
						
									
										5
									
								
								main.cc
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -46,6 +46,11 @@ main()
 | 
			
		|||
        return 1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if(!helpers::migrate_sql())
 | 
			
		||||
    {
 | 
			
		||||
        terminate(1);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //Load config file
 | 
			
		||||
    drogon::app().loadConfigFile("config.json");
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,27 +0,0 @@
 | 
			
		|||
#!/usr/bin/env bash
 | 
			
		||||
 | 
			
		||||
sqlite_return=$(sqlite3 core.sqlite "SELECT version_num FROM meta LIMIT 1;" 2>/dev/null);
 | 
			
		||||
 | 
			
		||||
if ! [[ $sqlite_return =~ ^-?[0-9]+$ ]]
 | 
			
		||||
then
 | 
			
		||||
  echo "No version number found. Initiating database."
 | 
			
		||||
  sqlite_return=0;
 | 
			
		||||
fi;
 | 
			
		||||
 | 
			
		||||
version_num=$sqlite_return;
 | 
			
		||||
 | 
			
		||||
while [ -f sql/migration_$version_num.sql ]
 | 
			
		||||
do
 | 
			
		||||
  echo "sql/migration_$version_num.sql";
 | 
			
		||||
  sqlite3 core.sqlite < sql/migration_$version_num.sql;
 | 
			
		||||
  ((version_num++));
 | 
			
		||||
done;
 | 
			
		||||
 | 
			
		||||
echo "New Schemaversion: $version_num";
 | 
			
		||||
 | 
			
		||||
if [ $sqlite_return -eq 0 ]
 | 
			
		||||
then
 | 
			
		||||
  sqlite3 core.sqlite "INSERT INTO meta (version_num) VALUES ($version_num); ";
 | 
			
		||||
else
 | 
			
		||||
  sqlite3 core.sqlite "UPDATE meta SET version_num=$version_num;";
 | 
			
		||||
fi
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue