Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit 4e77bdc

Browse files
committed
add db support
1 parent 6f9e8d4 commit 4e77bdc

File tree

10 files changed

+290
-150
lines changed

10 files changed

+290
-150
lines changed

engine/database/file.cc

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#include "file.h"
2+
#include "utils/logging_utils.h"
3+
#include "utils/scope_exit.h"
4+
5+
namespace cortex::db {
6+
7+
cpp::result<std::vector<OpenAi::File>, std::string> File::GetFileList() const {
8+
try {
9+
db_.exec("BEGIN TRANSACTION;");
10+
cortex::utils::ScopeExit se([this] { db_.exec("COMMIT;"); });
11+
std::vector<OpenAi::File> entries;
12+
SQLite::Statement query(db_,
13+
"SELECT id, object, "
14+
"purpose, filename, created_at, bytes FROM files");
15+
16+
while (query.executeStep()) {
17+
OpenAi::File entry;
18+
entry.id = query.getColumn(0).getString();
19+
entry.object = query.getColumn(1).getString();
20+
entry.purpose = query.getColumn(2).getString();
21+
entry.filename = query.getColumn(3).getString();
22+
entry.created_at = query.getColumn(4).getInt();
23+
entry.bytes = query.getColumn(5).getInt();
24+
entries.push_back(entry);
25+
}
26+
return entries;
27+
} catch (const std::exception& e) {
28+
CTL_WRN(e.what());
29+
return cpp::fail(e.what());
30+
}
31+
}
32+
33+
cpp::result<OpenAi::File, std::string> File::GetFileById(
34+
const std::string& file_id) const {
35+
try {
36+
SQLite::Statement query(db_,
37+
"SELECT id, object, "
38+
"purpose, filename, created_at, bytes FROM files "
39+
"WHERE id = ?");
40+
41+
query.bind(1, file_id);
42+
if (query.executeStep()) {
43+
OpenAi::File entry;
44+
entry.id = query.getColumn(0).getString();
45+
entry.object = query.getColumn(1).getString();
46+
entry.purpose = query.getColumn(2).getString();
47+
entry.filename = query.getColumn(3).getString();
48+
entry.created_at = query.getColumn(4).getInt();
49+
entry.bytes = query.getColumn(5).getInt64();
50+
return entry;
51+
} else {
52+
return cpp::fail("File not found: " + file_id);
53+
}
54+
} catch (const std::exception& e) {
55+
return cpp::fail(e.what());
56+
}
57+
}
58+
59+
cpp::result<void, std::string> File::AddFileEntry(OpenAi::File& file) {
60+
try {
61+
SQLite::Statement insert(
62+
db_,
63+
"INSERT INTO files (id, object, "
64+
"purpose, filename, created_at, bytes) VALUES (?, ?, "
65+
"?, ?, ?, ?)");
66+
insert.bind(1, file.id);
67+
insert.bind(2, file.object);
68+
insert.bind(3, file.purpose);
69+
insert.bind(4, file.filename);
70+
insert.bind(5, std::to_string(file.created_at));
71+
insert.bind(6, std::to_string(file.bytes));
72+
insert.exec();
73+
74+
CTL_INF("Inserted: " << file.ToJson()->toStyledString());
75+
return {};
76+
} catch (const std::exception& e) {
77+
CTL_WRN(e.what());
78+
return cpp::fail(e.what());
79+
}
80+
}
81+
82+
cpp::result<void, std::string> File::DeleteFileEntry(
83+
const std::string& file_id) {
84+
try {
85+
SQLite::Statement del(db_, "DELETE from files WHERE id = ?");
86+
del.bind(1, file_id);
87+
if (del.exec() == 1) {
88+
CTL_INF("Deleted: " << file_id);
89+
return {};
90+
}
91+
return {};
92+
} catch (const std::exception& e) {
93+
return cpp::fail(e.what());
94+
}
95+
}
96+
} // namespace cortex::db

engine/database/file.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#pragma once
2+
3+
#include <SQLiteCpp/Database.h>
4+
#include <trantor/utils/Logger.h>
5+
#include <string>
6+
#include <vector>
7+
#include "common/file.h"
8+
#include "database.h"
9+
#include "utils/result.hpp"
10+
11+
namespace cortex::db {
12+
class File {
13+
SQLite::Database& db_;
14+
15+
public:
16+
File(SQLite::Database& db) : db_{db} {};
17+
18+
File() : db_(cortex::db::Database::GetInstance().db()) {}
19+
20+
~File() {}
21+
22+
cpp::result<std::vector<OpenAi::File>, std::string> GetFileList() const;
23+
24+
cpp::result<OpenAi::File, std::string> GetFileById(
25+
const std::string& file_id) const;
26+
27+
cpp::result<void, std::string> AddFileEntry(OpenAi::File& file);
28+
29+
cpp::result<void, std::string> DeleteFileEntry(const std::string& file_id);
30+
};
31+
} // namespace cortex::db

engine/database/models.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,10 @@
88

99
namespace cortex::db {
1010

11-
enum class ModelStatus {
12-
Remote,
13-
Downloaded,
14-
Undownloaded
15-
};
11+
enum class ModelStatus { Remote, Downloaded, Undownloaded };
1612

1713
struct ModelEntry {
18-
std::string model;
14+
std::string model;
1915
std::string author_repo_id;
2016
std::string branch_name;
2117
std::string path_to_model_yaml;
@@ -64,4 +60,4 @@ class Models {
6460
bool HasModel(const std::string& identifier) const;
6561
};
6662

67-
} // namespace cortex::db
63+
} // namespace cortex::db

engine/migrations/migration_manager.cc

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include "v0/migration.h"
99
#include "v1/migration.h"
1010
#include "v2/migration.h"
11+
#include "v3/migration.h"
12+
1113
namespace cortex::migr {
1214

1315
namespace {
@@ -145,8 +147,8 @@ cpp::result<bool, std::string> MigrationManager::DoUpFolderStructure(
145147
return v1::MigrateFolderStructureUp();
146148
case 2:
147149
return v2::MigrateFolderStructureUp();
148-
149-
break;
150+
case 3:
151+
return v3::MigrateFolderStructureUp();
150152

151153
default:
152154
return true;
@@ -161,7 +163,8 @@ cpp::result<bool, std::string> MigrationManager::DoDownFolderStructure(
161163
return v1::MigrateFolderStructureDown();
162164
case 2:
163165
return v2::MigrateFolderStructureDown();
164-
break;
166+
case 3:
167+
return v3::MigrateFolderStructureDown();
165168

166169
default:
167170
return true;
@@ -198,7 +201,8 @@ cpp::result<bool, std::string> MigrationManager::DoUpDB(int version) {
198201
return v1::MigrateDBUp(db_);
199202
case 2:
200203
return v2::MigrateDBUp(db_);
201-
break;
204+
case 3:
205+
return v3::MigrateDBUp(db_);
202206

203207
default:
204208
return true;
@@ -213,7 +217,8 @@ cpp::result<bool, std::string> MigrationManager::DoDownDB(int version) {
213217
return v1::MigrateDBDown(db_);
214218
case 2:
215219
return v2::MigrateDBDown(db_);
216-
break;
220+
case 3:
221+
return v3::MigrateDBDown(db_);
217222

218223
default:
219224
return true;
@@ -247,4 +252,4 @@ cpp::result<bool, std::string> MigrationManager::UpdateSchemaVersion(
247252
return cpp::fail(e.what());
248253
}
249254
}
250-
} // namespace cortex::migr
255+
} // namespace cortex::migr

engine/migrations/migration_manager.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
2+
23
#include "migration_helper.h"
3-
#include "v0/migration.h"
44

55
namespace cortex::migr {
66
class MigrationManager {
@@ -28,4 +28,4 @@ class MigrationManager {
2828
MigrationHelper mgr_helper_;
2929
SQLite::Database& db_;
3030
};
31-
} // namespace cortex::migr
31+
} // namespace cortex::migr

engine/migrations/schema_version.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#pragma once
22

33
//Track the current schema version
4-
#define SCHEMA_VERSION 2
5-
4+
#define SCHEMA_VERSION 3

engine/migrations/v3/migration.h

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#pragma once
2+
3+
#include <SQLiteCpp/Database.h>
4+
#include <string>
5+
#include "utils/logging_utils.h"
6+
#include "utils/result.hpp"
7+
8+
namespace cortex::migr::v3 {
9+
inline cpp::result<bool, std::string> MigrateFolderStructureUp() {
10+
return true;
11+
}
12+
13+
inline cpp::result<bool, std::string> MigrateFolderStructureDown() {
14+
// CTL_INF("Folder structure already up to date!");
15+
return true;
16+
}
17+
18+
// Database
19+
inline cpp::result<bool, std::string> MigrateDBUp(SQLite::Database& db) {
20+
try {
21+
db.exec(
22+
"CREATE TABLE IF NOT EXISTS schema_version ( version INTEGER PRIMARY "
23+
"KEY);");
24+
25+
// files
26+
{
27+
// Check if the table exists
28+
SQLite::Statement query(db,
29+
"SELECT name FROM sqlite_master WHERE "
30+
"type='table' AND name='files'");
31+
auto table_exists = query.executeStep();
32+
33+
if (!table_exists) {
34+
// Create new table
35+
db.exec(
36+
"CREATE TABLE files ("
37+
"id TEXT PRIMARY KEY,"
38+
"object TEXT,"
39+
"purpose TEXT,"
40+
"filename TEXT,"
41+
"created_at INTEGER,"
42+
"bytes INTEGER"
43+
")");
44+
}
45+
}
46+
47+
return true;
48+
} catch (const std::exception& e) {
49+
CTL_WRN("Migration up failed: " << e.what());
50+
return cpp::fail(e.what());
51+
}
52+
};
53+
54+
inline cpp::result<bool, std::string> MigrateDBDown(SQLite::Database& db) {
55+
try {
56+
// hardware
57+
{
58+
SQLite::Statement query(db,
59+
"SELECT name FROM sqlite_master WHERE "
60+
"type='table' AND name='hardware'");
61+
auto table_exists = query.executeStep();
62+
if (table_exists) {
63+
db.exec("DROP TABLE files");
64+
}
65+
}
66+
67+
return true;
68+
} catch (const std::exception& e) {
69+
CTL_WRN("Migration down failed: " << e.what());
70+
return cpp::fail(e.what());
71+
}
72+
}
73+
}; // namespace cortex::migr::v3

0 commit comments

Comments
 (0)