This repository was archived by the owner on Jul 20, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlib.rs
More file actions
114 lines (94 loc) · 2.78 KB
/
Copy pathlib.rs
File metadata and controls
114 lines (94 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#![allow(dead_code)]
use std::sync::LazyLock;
use sea_orm::*;
use sea_orm_migration::prelude::*;
use migrator::Migrator;
pub mod entities;
mod migrator;
pub mod cost_model;
pub use cost_model::interface::CostModelStorageLayer;
mod memo;
pub use memo::interface::MemoStorage;
/// The filename of the SQLite database for migration.
pub const DATABASE_FILENAME: &str = "sqlite.db";
/// The URL of the SQLite database for migration.
pub const DATABASE_URL: &str = "sqlite:./sqlite.db?mode=rwc";
/// The filename of the SQLite database for testing.
pub const TEST_DATABASE_FILENAME: &str = "init.db";
/// The URL of the SQLite database for testing.
pub static TEST_DATABASE_FILE: LazyLock<String> = LazyLock::new(|| {
std::env::current_dir()
.unwrap()
.join("src")
.join("db")
.join(TEST_DATABASE_FILENAME)
.to_str()
.unwrap()
.to_owned()
});
/// The URL of the SQLite database for testing.
pub static TEST_DATABASE_URL: LazyLock<String> =
LazyLock::new(|| get_sqlite_url(TEST_DATABASE_FILE.as_str()));
fn get_sqlite_url(file: &str) -> String {
format!("sqlite:{}?mode=rwc", file)
}
#[derive(Debug)]
pub enum CostModelError {
// TODO: Add more error types
UnknownStatisticType,
VersionedStatisticNotFound,
}
/// TODO convert this to `thiserror`
#[derive(Debug)]
/// The different kinds of errors that might occur while running operations on a memo table.
pub enum MemoError {
UnknownGroup,
UnknownLogicalExpression,
UnknownPhysicalExpression,
InvalidExpression,
Database(DbErr),
}
/// TODO convert this to `thiserror`
#[derive(Debug)]
pub enum BackendError {
CostModel(CostModelError),
Memo(MemoError),
Database(DbErr),
// TODO: Add other variants as needed for different error types
}
impl From<CostModelError> for BackendError {
fn from(value: CostModelError) -> Self {
BackendError::CostModel(value)
}
}
impl From<MemoError> for BackendError {
fn from(value: MemoError) -> Self {
BackendError::Memo(value)
}
}
impl From<DbErr> for BackendError {
fn from(value: DbErr) -> Self {
BackendError::Database(value)
}
}
impl From<DbErr> for MemoError {
fn from(value: DbErr) -> Self {
MemoError::Database(value)
}
}
/// A type alias for a result with [`BackendError`] as the error type.
pub type StorageResult<T> = Result<T, BackendError>;
pub struct BackendManager {
db: DatabaseConnection,
}
impl BackendManager {
/// Creates a new `BackendManager`.
pub async fn new(database_url: Option<&str>) -> StorageResult<Self> {
Ok(Self {
db: Database::connect(database_url.unwrap_or(DATABASE_URL)).await?,
})
}
}
pub async fn migrate(db: &DatabaseConnection) -> Result<(), DbErr> {
Migrator::refresh(db).await
}