2022-07-10 19:40:26 +00:00
# include "MigrationRunner.h"
# include "GeneralUtils.h"
# include <fstream>
# include <algorithm>
# include <thread>
void MigrationRunner : : RunMigrations ( ) {
auto stmt = Database : : CreatePreppedStmt ( " CREATE TABLE IF NOT EXISTS migration_history (name TEXT NOT NULL, date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP()); " ) ;
stmt - > executeQuery ( ) ;
2022-07-28 13:39:57 +00:00
delete stmt ;
sql : : SQLString finalSQL = " " ;
Migration checkMigration { } ;
for ( const auto & entry : GeneralUtils : : GetFileNamesFromFolder ( " ./migrations/ " ) ) {
auto migration = LoadMigration ( entry ) ;
if ( migration . data . empty ( ) ) {
continue ;
}
checkMigration = migration ;
stmt = Database : : CreatePreppedStmt ( " SELECT name FROM migration_history WHERE name = ?; " ) ;
stmt - > setString ( 1 , migration . name ) ;
auto res = stmt - > executeQuery ( ) ;
bool doExit = res - > next ( ) ;
delete res ;
delete stmt ;
if ( doExit ) continue ;
Game : : logger - > Log ( " MigrationRunner " , " Running migration: %s " , migration . name . c_str ( ) ) ;
finalSQL . append ( migration . data ) ;
finalSQL . append ( ' \n ' ) ;
stmt = Database : : CreatePreppedStmt ( " INSERT INTO migration_history (name) VALUES (?); " ) ;
stmt - > setString ( 1 , entry ) ;
stmt - > execute ( ) ;
delete stmt ;
}
if ( ! finalSQL . empty ( ) ) {
try {
auto simpleStatement = Database : : CreateStmt ( ) ;
simpleStatement - > execute ( finalSQL ) ;
delete simpleStatement ;
} catch ( sql : : SQLException e ) {
Game : : logger - > Log ( " MigrationRunner " , " Encountered error running migration: %s " , e . what ( ) ) ;
}
}
2022-07-10 19:40:26 +00:00
}
Migration MigrationRunner : : LoadMigration ( std : : string path ) {
2022-07-28 13:39:57 +00:00
Migration migration { } ;
std : : ifstream file ( " ./migrations/ " + path ) ;
2022-07-10 19:40:26 +00:00
2022-07-28 13:39:57 +00:00
if ( file . is_open ( ) ) {
std : : hash < std : : string > hash ;
2022-07-25 02:26:51 +00:00
2022-07-28 13:39:57 +00:00
std : : string line ;
std : : string total = " " ;
2022-07-10 19:40:26 +00:00
2022-07-28 13:39:57 +00:00
while ( std : : getline ( file , line ) ) {
total + = line ;
}
2022-07-10 19:40:26 +00:00
2022-07-28 13:39:57 +00:00
file . close ( ) ;
2022-07-10 19:40:26 +00:00
2022-07-28 13:39:57 +00:00
migration . name = path ;
migration . data = total ;
}
2022-07-25 02:26:51 +00:00
2022-07-28 13:39:57 +00:00
return migration ;
2022-07-10 19:40:26 +00:00
}