Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: data migration #1692

Merged
merged 11 commits into from
Nov 25, 2024
Prev Previous commit
Next Next commit
temp
sangjanai committed Nov 14, 2024
commit 8554551d8898ffc541e802b6327231246c579f35
24 changes: 5 additions & 19 deletions engine/database/hardwares.cc → engine/database/hardware.cc
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@
#include "hardwares.h"
#include "hardware.h"
#include "database.h"
#include "utils/scope_exit.h"

namespace cortex::db {

Hardwares::Hardwares() : db_(cortex::db::Database::GetInstance().db()) {
db_.exec(
"CREATE TABLE IF NOT EXISTS hardwares ("
"uuid TEXT PRIMARY KEY,"
"type TEXT,"
"hardware_id INTEGER,"
"software_id INTEGER,"
"activated INTEGER);");
}

Hardwares::Hardwares(SQLite::Database& db) : db_(db) {
db_.exec(
"CREATE TABLE IF NOT EXISTS hardwares ("
"uuid TEXT PRIMARY KEY,"
"type TEXT,"
"hardware_id INTEGER,"
"software_id INTEGER,"
"activated INTEGER);");
}

Hardwares::~Hardwares() {}
@@ -35,7 +21,7 @@ Hardwares::LoadHardwareList() const {
SQLite::Statement query(
db_,
"SELECT uuid, type, "
"hardware_id, software_id, activated FROM hardwares");
"hardware_id, software_id, activated FROM hardware");

while (query.executeStep()) {
HardwareEntry entry;
@@ -57,7 +43,7 @@ cpp::result<bool, std::string> Hardwares::AddHardwareEntry(
try {
SQLite::Statement insert(
db_,
"INSERT INTO hardwares (uuid, type, "
"INSERT INTO hardware (uuid, type, "
"hardware_id, software_id, activated) VALUES (?, ?, "
"?, ?, ?)");
insert.bind(1, new_entry.uuid);
@@ -77,7 +63,7 @@ cpp::result<bool, std::string> Hardwares::UpdateHardwareEntry(
const std::string& id, const HardwareEntry& updated_entry) {
try {
SQLite::Statement upd(db_,
"UPDATE hardwares "
"UPDATE hardware "
"SET hardware_id = ?, software_id = ?, activated = ? "
"WHERE uuid = ?");
upd.bind(1, updated_entry.hardware_id);
@@ -97,7 +83,7 @@ cpp::result<bool, std::string> Hardwares::UpdateHardwareEntry(
cpp::result<bool, std::string> Hardwares::DeleteHardwareEntry(
const std::string& id) {
try {
SQLite::Statement del(db_, "DELETE from hardwares WHERE uuid = ?");
SQLite::Statement del(db_, "DELETE from hardware WHERE uuid = ?");
del.bind(1, id);
if (del.exec() == 1) {
CTL_INF("Deleted: " << id);
File renamed without changes.
22 changes: 0 additions & 22 deletions engine/database/models.cc
Original file line number Diff line number Diff line change
@@ -3,41 +3,19 @@
#include <iostream>
#include <sstream>
#include "database.h"
#include "schema_version.h"
#include "utils/result.hpp"
#include "utils/scope_exit.h"

namespace cortex::db {

Models::Models() : db_(cortex::db::Database::GetInstance().db()) {
InitializeDatabase();
}

Models::Models(SQLite::Database& db) : db_(db) {
InitializeDatabase();
}

Models::~Models() {}

void Models::InitializeDatabase() {
switch (SCHEMA_VERSION) {
case 0: {
db_.exec(
"CREATE TABLE IF NOT EXISTS models ("
"model_id TEXT PRIMARY KEY,"
"author_repo_id TEXT,"
"branch_name TEXT,"
"path_to_model_yaml TEXT,"
"model_alias TEXT);");
break;
}
default: {
CTL_ERR("Not supported, schema_version: " << SCHEMA_VERSION);
assert(false);
}
}
}

cpp::result<std::vector<ModelEntry>, std::string> Models::LoadModelList()
const {
try {
2 changes: 0 additions & 2 deletions engine/database/models.h
Original file line number Diff line number Diff line change
@@ -20,8 +20,6 @@ class Models {
private:
SQLite::Database& db_;

void InitializeDatabase();

bool IsUnique(const std::vector<ModelEntry>& entries,
const std::string& model_id,
const std::string& model_alias) const;
2 changes: 2 additions & 0 deletions engine/main.cc
Original file line number Diff line number Diff line change
@@ -201,6 +201,8 @@ int main(int argc, char* argv[]) {
// avoid printing logs to terminal
is_server = true;

// check if migration is needed

std::optional<int> server_port;
bool ignore_cout_log = false;
for (int i = 0; i < argc; i++) {
Empty file.
1 change: 1 addition & 0 deletions engine/migrations/migration_helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#pragma once
63 changes: 63 additions & 0 deletions engine/migrations/migration_manager.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "migration_manager.h"
#include "schema_version.h"

namespace cortex::migr {

namespace {
cpp::result<bool, std::string> DoUp(int version) {
switch (version) {
case 0:
return v0::MigrateUp();
break;

default:
return true;
}
}

cpp::result<bool, std::string> DoDown(int version) {
switch (version) {
case 0:
return v0::MigrateDown();
break;

default:
return true;
}
}

int GetSchemaVersion(SQLite::Database& db) {
int version = 0; // Default version if not set

try {
SQLite::Statement query(db, "SELECT version FROM schema_version LIMIT 1;");

// Execute the query and get the result
if (query.executeStep()) {
version =
query.getColumn(0).getInt(); // Get the version from the first column
}
} catch (const std::exception& e) {
std::cerr << "SQLite error: " << e.what() << std::endl;
// Handle exceptions, possibly setting a default version or taking corrective action
}

return version;
}
} // namespace

cpp::result<bool, std::string> MigrationManager::Up(SQLite::Database& db) {
// check schema db version
int schema_version = GetSchemaVersion(db);
int current_version = SCHEMA_VERSION;
for (int i = schema_version; i <= current_version; i++) {
if (auto res = DoUp(i /*version*/); res.has_error()) {
// Restore db and file structure
}
}
return true;
}
cpp::result<bool, std::string> MigrationManager::Down(SQLite::Database& db) {
return true;
}
} // namespace cortex::migr
9 changes: 9 additions & 0 deletions engine/migrations/migration_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once
#include "v0/migration.h"

namespace cortex::migr {
class MigrationManager {
cpp::result<bool, std::string> Up(SQLite::Database& db);
cpp::result<bool, std::string> Down(SQLite::Database& db);
};
} // namespace cortex::migr
File renamed without changes.
54 changes: 54 additions & 0 deletions engine/migrations/v0/migration.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#pragma once
#include <SQLiteCpp/Database.h>
#include <string>
#include "utils/logging_utils.h"
#include "utils/result.hpp"

namespace cortex::migr::v0 {
// Data folder

inline cpp::result<bool, std::string> MigrateUp() {
return true;
}

inline cpp::result<bool, std::string> MigrateDown() {
return true;
}

// Database
inline cpp::result<bool, std::string> MigrateUp(SQLite::Database& db) {
try {
db.exec(
"CREATE TABLE IF NOT EXISTS schema_version ( version INTEGER NOT "
"NULL);");

db.exec(
"CREATE TABLE IF NOT EXISTS models ("
"model_id TEXT PRIMARY KEY,"
"author_repo_id TEXT,"
"branch_name TEXT,"
"path_to_model_yaml TEXT,"
"model_alias TEXT);");

db.exec(
"CREATE TABLE IF NOT EXISTS hardware ("
"uuid TEXT NOT NULL, "
"type TEXT NOT NULL, "
"hardware_id INTEGER NOT NULL, "
"software_id INTEGER NOT NULL, "
"activated INTEGER NOT NULL CHECK (activated IN (0, 1)));");

CTL_INF("Database migration up completed successfully.");
return true;
} catch (const std::exception& e) {
CTL_WRN("Migration up failed: " << e.what());
return cpp::fail(e.what());
}
};

inline cpp::result<bool, std::string> MigrateDown(SQLite::Database& db) {
CTL_INF("No need to drop tables for version 0");
return true;
}

}; // namespace cortex::migr::v0
2 changes: 1 addition & 1 deletion engine/services/hardware_service.cc
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@
#include <processenv.h>
#endif
#include "cli/commands/cortex_upd_cmd.h"
#include "database/hardwares.h"
#include "database/hardware.h"
#include "services/engine_service.h"
#include "utils/cortex_utils.h"