caretta-sync/lazy-supplements-core/src/global/database_connection.rs

32 lines
1.3 KiB
Rust
Raw Normal View History

2025-06-19 07:24:44 +09:00
use std::path::Path;
use sea_orm::{ConnectOptions, Database, DbErr, DatabaseConnection};
use sea_orm_migration::MigratorTrait;
use crate::error::Error;
use tokio::sync::OnceCell;
use super::storage_config::GlobalStorageConfig;
static UNINITIALIZED_MESSAGE: &str = "global database connection uninitialized!";
pub trait GlobalDatabaseConnection: GlobalStorageConfig {
fn get_data_database_connection_as_once_cell(&'static self) -> &'static OnceCell<DatabaseConnection>;
fn get_data_database_connection(&'static self) -> Option<&'static DatabaseConnection> {
self.get_data_database_connection_as_once_cell().get()
}
fn get_and_unwrap_data_database_connection(&'static self) -> &'static DatabaseConnection {
self.get_data_database_connection().expect(UNINITIALIZED_MESSAGE)
}
async fn get_or_try_init_data_database_connection<T>(&'static self, _: T) -> Result<&DatabaseConnection, Error>
where
T: MigratorTrait
{
let url = "sqlite://".to_string() + self.get_and_unwrap_storage_config().get_data_database_path().to_str().unwrap() + "?mode=rwc";
Ok(self.get_data_database_connection_as_once_cell().get_or_try_init(|| async {
let db = Database::connect(&url).await?;
T::up(&db, None).await?;
Ok::<DatabaseConnection, DbErr>(db)
}).await?)
}
}