|
@ -3,7 +3,6 @@ |
|
|
|
|
|
|
|
|
#include "migrations/1624829187_create_events.h"
|
|
|
#include "migrations/1624829187_create_events.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace db { |
|
|
namespace db { |
|
|
namespace db_builder = db::migrations::builder; |
|
|
namespace db_builder = db::migrations::builder; |
|
|
|
|
|
|
|
@ -23,37 +22,64 @@ namespace db { |
|
|
void Schema::set_user_version(uint32_t version) { |
|
|
void Schema::set_user_version(uint32_t version) { |
|
|
try { |
|
|
try { |
|
|
m_db->exec("PRAGMA user_version = " + std::to_string(version) + ";"); |
|
|
m_db->exec("PRAGMA user_version = " + std::to_string(version) + ";"); |
|
|
|
|
|
m_user_version = version; |
|
|
} catch (std::exception& e) { |
|
|
} catch (std::exception& e) { |
|
|
std::cout << "Exception: " << e.what() << std::endl; |
|
|
std::cout << "Exception: " << e.what() << std::endl; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void Schema::run_migrations() { |
|
|
|
|
|
|
|
|
void Schema::set_savepoint() { |
|
|
|
|
|
try { |
|
|
|
|
|
m_db->exec("BEGIN;"); |
|
|
|
|
|
m_db->exec("SAVEPOINT " + m_migration_savepoint + ";"); |
|
|
|
|
|
} catch (std::exception& e) { |
|
|
|
|
|
std::cout << "Exception: " << e.what() << std::endl; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Schema::rollback_migrations() { |
|
|
|
|
|
try { |
|
|
|
|
|
m_db->exec("ROLLBACK TO " + m_migration_savepoint + ";"); |
|
|
|
|
|
} catch (std::exception& e) { |
|
|
|
|
|
std::cout << "Exception: " << e.what() << std::endl; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Schema::commit_migrations() { |
|
|
|
|
|
try { |
|
|
|
|
|
m_db->exec("COMMIT;"); |
|
|
|
|
|
} catch (std::exception& e) { |
|
|
|
|
|
std::cout << "Exception: " << e.what() << std::endl; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
uint32_t Schema::run_migrations() { |
|
|
assemble_migrations(); |
|
|
assemble_migrations(); |
|
|
if(migrations.begin() == migrations.end()) { |
|
|
if(migrations.begin() == migrations.end()) { |
|
|
DEBUG << "No migrations found..."; |
|
|
DEBUG << "No migrations found..."; |
|
|
return; |
|
|
|
|
|
|
|
|
return ERROR_NO_MIGRATIONS; |
|
|
} |
|
|
} |
|
|
// TODO try and catch block
|
|
|
|
|
|
// migration should stop on error and restore old schema
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
set_savepoint(); |
|
|
uint32_t user_version = get_user_version(); |
|
|
uint32_t user_version = get_user_version(); |
|
|
for(auto const& [epoch, migration] : migrations) { |
|
|
for(auto const& [epoch, migration] : migrations) { |
|
|
if(epoch <= user_version) { |
|
|
if(epoch <= user_version) { |
|
|
DEBUG << "Skipping: " << migration->get_migration_name(); |
|
|
DEBUG << "Skipping: " << migration->get_migration_name(); |
|
|
continue; |
|
|
continue; |
|
|
} |
|
|
} |
|
|
DEBUG << "migrating: " << migration->get_migration_name(); |
|
|
|
|
|
|
|
|
std::cout << "migrating: " << migration->get_migration_name() << std::endl; |
|
|
try { |
|
|
try { |
|
|
m_db->exec(migration->get_statement()); |
|
|
m_db->exec(migration->get_statement()); |
|
|
} catch (std::exception& e) { |
|
|
} catch (std::exception& e) { |
|
|
std::cout << "Exception: " << e.what() << std::endl; |
|
|
std::cout << "Exception: " << e.what() << std::endl; |
|
|
|
|
|
std::cout << "Rolling back migrations..." << std::endl; |
|
|
|
|
|
rollback_migrations(); |
|
|
|
|
|
return ERROR_FAULTY_MIGRATION; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
DEBUG << migrations.rbegin()->first; |
|
|
|
|
|
|
|
|
commit_migrations(); |
|
|
set_user_version(migrations.rbegin()->first); |
|
|
set_user_version(migrations.rbegin()->first); |
|
|
user_version = get_user_version(); |
|
|
|
|
|
DEBUG << user_version; |
|
|
|
|
|
|
|
|
return 0; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void Schema::assemble_migrations() { |
|
|
void Schema::assemble_migrations() { |
|
|