Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c507569

Browse files
author
Gabrielle Ong
authored
Merge branch 'dev' into patch-1
2 parents 64d99d8 + adfbda1 commit c507569

File tree

13 files changed

+466
-36
lines changed

13 files changed

+466
-36
lines changed

engine/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,11 @@ aux_source_directory(models MODEL_SRC)
173173
aux_source_directory(cortex-common CORTEX_COMMON)
174174
aux_source_directory(config CONFIG_SRC)
175175
aux_source_directory(database DB_SRC)
176+
aux_source_directory(migrations MIGR_SRC)
176177

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

179-
target_sources(${TARGET_NAME} PRIVATE ${CONFIG_SRC} ${CTL_SRC} ${COMMON_SRC} ${SERVICES_SRC} ${DB_SRC})
180+
target_sources(${TARGET_NAME} PRIVATE ${CONFIG_SRC} ${CTL_SRC} ${COMMON_SRC} ${SERVICES_SRC} ${DB_SRC} ${MIGR_SRC})
180181

181182
set_target_properties(${TARGET_NAME} PROPERTIES
182183
RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}

engine/database/hardwares.cc renamed to engine/database/hardware.cc

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,13 @@
1-
#include "hardwares.h"
1+
#include "hardware.h"
22
#include "database.h"
33
#include "utils/scope_exit.h"
44

55
namespace cortex::db {
66

77
Hardwares::Hardwares() : db_(cortex::db::Database::GetInstance().db()) {
8-
db_.exec(
9-
"CREATE TABLE IF NOT EXISTS hardwares ("
10-
"uuid TEXT PRIMARY KEY,"
11-
"type TEXT,"
12-
"hardware_id INTEGER,"
13-
"software_id INTEGER,"
14-
"activated INTEGER);");
158
}
169

1710
Hardwares::Hardwares(SQLite::Database& db) : db_(db) {
18-
db_.exec(
19-
"CREATE TABLE IF NOT EXISTS hardwares ("
20-
"uuid TEXT PRIMARY KEY,"
21-
"type TEXT,"
22-
"hardware_id INTEGER,"
23-
"software_id INTEGER,"
24-
"activated INTEGER);");
2511
}
2612

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

4026
while (query.executeStep()) {
4127
HardwareEntry entry;
@@ -57,7 +43,7 @@ cpp::result<bool, std::string> Hardwares::AddHardwareEntry(
5743
try {
5844
SQLite::Statement insert(
5945
db_,
60-
"INSERT INTO hardwares (uuid, type, "
46+
"INSERT INTO hardware (uuid, type, "
6147
"hardware_id, software_id, activated) VALUES (?, ?, "
6248
"?, ?, ?)");
6349
insert.bind(1, new_entry.uuid);
@@ -77,7 +63,7 @@ cpp::result<bool, std::string> Hardwares::UpdateHardwareEntry(
7763
const std::string& id, const HardwareEntry& updated_entry) {
7864
try {
7965
SQLite::Statement upd(db_,
80-
"UPDATE hardwares "
66+
"UPDATE hardware "
8167
"SET hardware_id = ?, software_id = ?, activated = ? "
8268
"WHERE uuid = ?");
8369
upd.bind(1, updated_entry.hardware_id);
@@ -97,7 +83,7 @@ cpp::result<bool, std::string> Hardwares::UpdateHardwareEntry(
9783
cpp::result<bool, std::string> Hardwares::DeleteHardwareEntry(
9884
const std::string& id) {
9985
try {
100-
SQLite::Statement del(db_, "DELETE from hardwares WHERE uuid = ?");
86+
SQLite::Statement del(db_, "DELETE from hardware WHERE uuid = ?");
10187
del.bind(1, id);
10288
if (del.exec() == 1) {
10389
CTL_INF("Deleted: " << id);
File renamed without changes.

engine/database/models.cc

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,9 @@
99
namespace cortex::db {
1010

1111
Models::Models() : db_(cortex::db::Database::GetInstance().db()) {
12-
db_.exec(
13-
"CREATE TABLE IF NOT EXISTS models ("
14-
"model_id TEXT PRIMARY KEY,"
15-
"author_repo_id TEXT,"
16-
"branch_name TEXT,"
17-
"path_to_model_yaml TEXT,"
18-
"model_alias TEXT);");
1912
}
2013

2114
Models::Models(SQLite::Database& db) : db_(db) {
22-
db_.exec(
23-
"CREATE TABLE IF NOT EXISTS models ("
24-
"model_id TEXT PRIMARY KEY,"
25-
"author_repo_id TEXT,"
26-
"branch_name TEXT,"
27-
"path_to_model_yaml TEXT,"
28-
"model_alias TEXT UNIQUE);");
2915
}
3016

3117
Models::~Models() {}

engine/main.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include "controllers/process_manager.h"
1010
#include "controllers/server.h"
1111
#include "cortex-common/cortexpythoni.h"
12+
#include "database/database.h"
13+
#include "migrations/migration_manager.h"
1214
#include "services/config_service.h"
1315
#include "services/file_watcher_service.h"
1416
#include "services/model_service.h"
@@ -209,6 +211,15 @@ int main(int argc, char* argv[]) {
209211
// avoid printing logs to terminal
210212
is_server = true;
211213

214+
// check if migration is needed
215+
if (auto res = cortex::migr::MigrationManager(
216+
cortex::db::Database::GetInstance().db())
217+
.Migrate();
218+
res.has_error()) {
219+
CLI_LOG("Error: " << res.error());
220+
return 1;
221+
}
222+
212223
std::optional<int> server_port;
213224
bool ignore_cout_log = false;
214225
for (int i = 0; i < argc; i++) {
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include "migration_helper.h"
2+
3+
namespace cortex::migr {
4+
cpp::result<bool, std::string> MigrationHelper::BackupDatabase(
5+
const std::string& src_db_path, const std::string& backup_db_path) {
6+
try {
7+
SQLite::Database src_db(src_db_path, SQLite::OPEN_READONLY);
8+
sqlite3* backup_db;
9+
10+
if (sqlite3_open(backup_db_path.c_str(), &backup_db) != SQLITE_OK) {
11+
throw std::runtime_error("Failed to open backup database");
12+
}
13+
14+
sqlite3_backup* backup =
15+
sqlite3_backup_init(backup_db, "main", src_db.getHandle(), "main");
16+
if (!backup) {
17+
sqlite3_close(backup_db);
18+
throw std::runtime_error("Failed to initialize backup");
19+
}
20+
21+
if (sqlite3_backup_step(backup, -1) != SQLITE_DONE) {
22+
sqlite3_backup_finish(backup);
23+
sqlite3_close(backup_db);
24+
throw std::runtime_error("Failed to perform backup");
25+
}
26+
27+
sqlite3_backup_finish(backup);
28+
sqlite3_close(backup_db);
29+
// CTL_INF("Backup completed successfully.");
30+
return true;
31+
} catch (const std::exception& e) {
32+
CTL_WRN("Error during backup: " << e.what());
33+
return cpp::fail(e.what());
34+
}
35+
}
36+
37+
cpp::result<bool, std::string> MigrationHelper::RestoreDatabase(
38+
const std::string& backup_db_path, const std::string& target_db_path) {
39+
try {
40+
SQLite::Database target_db(target_db_path,
41+
SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE);
42+
sqlite3* backup_db;
43+
44+
if (sqlite3_open(backup_db_path.c_str(), &backup_db) != SQLITE_OK) {
45+
throw std::runtime_error("Failed to open backup database");
46+
}
47+
48+
sqlite3_backup* backup =
49+
sqlite3_backup_init(target_db.getHandle(), "main", backup_db, "main");
50+
if (!backup) {
51+
sqlite3_close(backup_db);
52+
throw std::runtime_error("Failed to initialize restore");
53+
}
54+
55+
if (sqlite3_backup_step(backup, -1) != SQLITE_DONE) {
56+
sqlite3_backup_finish(backup);
57+
sqlite3_close(backup_db);
58+
throw std::runtime_error("Failed to perform restore");
59+
}
60+
61+
sqlite3_backup_finish(backup);
62+
sqlite3_close(backup_db);
63+
// CTL_INF("Restore completed successfully.");
64+
return true;
65+
} catch (const std::exception& e) {
66+
CTL_WRN("Error during restore: " << e.what());
67+
return cpp::fail(e.what());
68+
}
69+
}
70+
} // namespace cortex::migr
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
3+
#include <SQLiteCpp/SQLiteCpp.h>
4+
#include <sqlite3.h>
5+
#include "utils/logging_utils.h"
6+
#include "utils/result.hpp"
7+
8+
namespace cortex::migr {
9+
class MigrationHelper {
10+
public:
11+
cpp::result<bool, std::string> BackupDatabase(
12+
const std::string& src_db_path, const std::string& backup_db_path);
13+
14+
cpp::result<bool, std::string> RestoreDatabase(
15+
const std::string& backup_db_path, const std::string& target_db_path);
16+
};
17+
} // namespace cortex::migr

0 commit comments

Comments
 (0)