metis_core/dal/database/
mod.rs1pub mod configuration_repository;
2pub mod models;
3pub mod repository;
4pub mod schema;
5
6use diesel::prelude::*;
7use diesel::sqlite::SqliteConnection;
8use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
9
10pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!("src/dal/database/migrations");
11
12pub struct Database {
14 connection_string: String,
15}
16
17fn configure_connection(connection: &mut SqliteConnection) -> Result<(), diesel::result::Error> {
22 diesel::sql_query("PRAGMA busy_timeout = 5000").execute(connection)?;
24
25 diesel::sql_query("PRAGMA journal_mode = WAL").execute(connection)?;
28
29 diesel::sql_query("PRAGMA synchronous = NORMAL").execute(connection)?;
31
32 Ok(())
33}
34
35impl Database {
36 pub fn new(connection_string: &str) -> Result<Self, Box<dyn std::error::Error + Send + Sync>> {
41 let mut connection = SqliteConnection::establish(connection_string)?;
43 configure_connection(&mut connection)?;
44 connection.run_pending_migrations(MIGRATIONS)?;
45
46 Ok(Self {
47 connection_string: connection_string.to_string(),
48 })
49 }
50
51 pub fn get_connection(
53 &self,
54 ) -> Result<SqliteConnection, Box<dyn std::error::Error + Send + Sync>> {
55 let mut connection = SqliteConnection::establish(&self.connection_string)?;
56 configure_connection(&mut connection)?;
57
58 if self.connection_string == ":memory:" {
61 connection.run_pending_migrations(MIGRATIONS)?;
62 }
63
64 Ok(connection)
65 }
66
67 pub fn repository(
69 &self,
70 ) -> Result<repository::DocumentRepository, Box<dyn std::error::Error + Send + Sync>> {
71 let connection = self.get_connection()?;
72 Ok(repository::DocumentRepository::new(connection))
73 }
74
75 pub fn into_repository(self) -> repository::DocumentRepository {
77 let connection = self.get_connection().expect("Failed to get connection");
78 repository::DocumentRepository::new(connection)
79 }
80
81 pub fn configuration_repository(
83 &self,
84 ) -> Result<
85 configuration_repository::ConfigurationRepository,
86 Box<dyn std::error::Error + Send + Sync>,
87 > {
88 let connection = self.get_connection()?;
89 Ok(configuration_repository::ConfigurationRepository::new(
90 connection,
91 ))
92 }
93}