You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

62 lines
1.7 KiB

#include "Schema.h"
#include <io/debug.h>
#include "migrations/1624829187_create_events.h"
namespace db {
namespace db_builder = db::migrations::builder;
Schema::Schema(sql::Database* db) : m_db(db) {
}
uint32_t Schema::get_user_version() const {
try {
int user_version = m_db->execAndGet("PRAGMA user_version;");
return user_version;
} catch (std::exception& e) {
std::cout << "Exception: " << e.what() << std::endl;
return -1;
}
}
void Schema::set_user_version(uint32_t version) {
try {
m_db->exec("PRAGMA user_version = " + std::to_string(version) + ";");
} catch (std::exception& e) {
std::cout << "Exception: " << e.what() << std::endl;
}
}
void Schema::run_migrations() {
assemble_migrations();
if(migrations.begin() == migrations.end()) {
DEBUG << "No migrations found...";
return;
}
// TODO try and catch block
// migration should stop on error and restore old schema
uint32_t user_version = get_user_version();
for(auto const& [epoch, migration] : migrations) {
if(epoch <= user_version) {
DEBUG << "Skipping: " << migration->get_migration_name();
continue;
}
DEBUG << "migrating: " << migration->get_migration_name();
try {
m_db->exec(migration->get_statement());
} catch (std::exception& e) {
std::cout << "Exception: " << e.what() << std::endl;
}
}
DEBUG << migrations.rbegin()->first;
set_user_version(migrations.rbegin()->first);
user_version = get_user_version();
DEBUG << user_version;
}
void Schema::assemble_migrations() {
migrations.emplace(1624829187, new db::migrations::m1624829187_create_events());
}
}