Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,11 @@ aux_source_directory(models MODEL_SRC)
aux_source_directory(cortex-common CORTEX_COMMON)
aux_source_directory(config CONFIG_SRC)
aux_source_directory(database DB_SRC)
aux_source_directory(migrations MIGR_SRC)

target_include_directories(${TARGET_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} )

target_sources(${TARGET_NAME} PRIVATE ${CONFIG_SRC} ${CTL_SRC} ${COMMON_SRC} ${SERVICES_SRC} ${DB_SRC})
target_sources(${TARGET_NAME} PRIVATE ${CONFIG_SRC} ${CTL_SRC} ${COMMON_SRC} ${SERVICES_SRC} ${DB_SRC} ${MIGR_SRC})

set_target_properties(${TARGET_NAME} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}
Expand Down
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() {}
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
File renamed without changes.
14 changes: 0 additions & 14 deletions engine/database/models.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,9 @@
namespace cortex::db {

Models::Models() : db_(cortex::db::Database::GetInstance().db()) {
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);");
}

Models::Models(SQLite::Database& db) : db_(db) {
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 UNIQUE);");
}

Models::~Models() {}
Expand Down
11 changes: 11 additions & 0 deletions engine/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include "controllers/process_manager.h"
#include "controllers/server.h"
#include "cortex-common/cortexpythoni.h"
#include "database/database.h"
#include "migrations/migration_manager.h"
#include "services/config_service.h"
#include "services/file_watcher_service.h"
#include "services/model_service.h"
Expand Down Expand Up @@ -209,6 +211,15 @@ int main(int argc, char* argv[]) {
// avoid printing logs to terminal
is_server = true;

// check if migration is needed
if (auto res = cortex::migr::MigrationManager(
cortex::db::Database::GetInstance().db())
.Migrate();
res.has_error()) {
CLI_LOG("Error: " << res.error());
return 1;
}

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

namespace cortex::migr {
cpp::result<bool, std::string> MigrationHelper::BackupDatabase(
const std::string& src_db_path, const std::string& backup_db_path) {
try {
SQLite::Database src_db(src_db_path, SQLite::OPEN_READONLY);
sqlite3* backup_db;

if (sqlite3_open(backup_db_path.c_str(), &backup_db) != SQLITE_OK) {
throw std::runtime_error("Failed to open backup database");
}

sqlite3_backup* backup =
sqlite3_backup_init(backup_db, "main", src_db.getHandle(), "main");
if (!backup) {
sqlite3_close(backup_db);
throw std::runtime_error("Failed to initialize backup");
}

if (sqlite3_backup_step(backup, -1) != SQLITE_DONE) {
sqlite3_backup_finish(backup);
sqlite3_close(backup_db);
throw std::runtime_error("Failed to perform backup");
}

sqlite3_backup_finish(backup);
sqlite3_close(backup_db);
// CTL_INF("Backup completed successfully.");
return true;
} catch (const std::exception& e) {
CTL_WRN("Error during backup: " << e.what());
return cpp::fail(e.what());
}
}

cpp::result<bool, std::string> MigrationHelper::RestoreDatabase(
const std::string& backup_db_path, const std::string& target_db_path) {
try {
SQLite::Database target_db(target_db_path,
SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE);
sqlite3* backup_db;

if (sqlite3_open(backup_db_path.c_str(), &backup_db) != SQLITE_OK) {
throw std::runtime_error("Failed to open backup database");
}

sqlite3_backup* backup =
sqlite3_backup_init(target_db.getHandle(), "main", backup_db, "main");
if (!backup) {
sqlite3_close(backup_db);
throw std::runtime_error("Failed to initialize restore");
}

if (sqlite3_backup_step(backup, -1) != SQLITE_DONE) {
sqlite3_backup_finish(backup);
sqlite3_close(backup_db);
throw std::runtime_error("Failed to perform restore");
}

sqlite3_backup_finish(backup);
sqlite3_close(backup_db);
// CTL_INF("Restore completed successfully.");
return true;
} catch (const std::exception& e) {
CTL_WRN("Error during restore: " << e.what());
return cpp::fail(e.what());
}
}
} // namespace cortex::migr
17 changes: 17 additions & 0 deletions engine/migrations/migration_helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <SQLiteCpp/SQLiteCpp.h>
#include <sqlite3.h>
#include "utils/logging_utils.h"
#include "utils/result.hpp"

namespace cortex::migr {
class MigrationHelper {
public:
cpp::result<bool, std::string> BackupDatabase(
const std::string& src_db_path, const std::string& backup_db_path);

cpp::result<bool, std::string> RestoreDatabase(
const std::string& backup_db_path, const std::string& target_db_path);
};
} // namespace cortex::migr
Loading
Loading