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;
|
|
|
|
|
|
2025-06-22 11:29:44 +09:00
|
|
|
pub static DATA_DATABASE_CONNECTION: GlobalDatabaseConnection = GlobalDatabaseConnection::const_new(stringify!(DATA_DATABASE_CONNECTION));
|
|
|
|
|
pub static CACHE_DATABASE_CONNECTION: GlobalDatabaseConnection = GlobalDatabaseConnection::const_new(stringify!(CACHE_DATABASE_CONNECTION));
|
2025-06-19 07:24:44 +09:00
|
|
|
|
2025-06-22 11:29:44 +09:00
|
|
|
pub struct GlobalDatabaseConnection {
|
|
|
|
|
name: &'static str,
|
2025-06-20 08:42:02 +09:00
|
|
|
inner: OnceCell<DatabaseConnection>
|
2025-06-20 07:28:51 +09:00
|
|
|
}
|
2025-06-19 07:24:44 +09:00
|
|
|
|
2025-06-20 08:42:02 +09:00
|
|
|
impl GlobalDatabaseConnection {
|
2025-06-22 11:29:44 +09:00
|
|
|
pub const fn const_new(name: &'static str) -> Self {
|
2025-06-20 08:42:02 +09:00
|
|
|
Self {
|
2025-06-22 11:29:44 +09:00
|
|
|
name: name,
|
2025-06-20 08:42:02 +09:00
|
|
|
inner: OnceCell::const_new()
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-06-22 11:29:44 +09:00
|
|
|
pub fn get(&'static self) -> &'static DatabaseConnection {
|
|
|
|
|
self.inner.get().expect(&format!("{} is uninitialized!", self.name))
|
2025-06-20 08:42:02 +09:00
|
|
|
}
|
2025-06-22 11:29:44 +09:00
|
|
|
pub async fn get_or_init<T, U>(&'static self, path: T, _: U) -> &'static DatabaseConnection
|
2025-06-20 08:42:02 +09:00
|
|
|
where
|
|
|
|
|
T: AsRef<Path>,
|
|
|
|
|
U: MigratorTrait
|
|
|
|
|
{
|
|
|
|
|
let url = "sqlite://".to_string() + path.as_ref().to_str().unwrap() + "?mode=rwc";
|
2025-06-22 11:29:44 +09:00
|
|
|
self.inner.get_or_try_init(|| async {
|
2025-06-20 08:42:02 +09:00
|
|
|
let db = Database::connect(&url).await?;
|
|
|
|
|
U::up(&db, None).await?;
|
|
|
|
|
Ok::<DatabaseConnection, DbErr>(db)
|
2025-06-22 11:29:44 +09:00
|
|
|
}).await.expect(&format!("Fail to initialize {}!", self.name))
|
2025-06-20 08:42:02 +09:00
|
|
|
}
|
2025-06-20 07:28:51 +09:00
|
|
|
}
|
2025-06-22 11:29:44 +09:00
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
pub use tests::*;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
2025-08-18 08:22:20 +09:00
|
|
|
use crate::{cache::migration::CacheMigrator, data::migration::DataMigrator, global::CONFIG, tests::GlobalTestDefault};
|
2025-06-22 11:29:44 +09:00
|
|
|
|
|
|
|
|
pub async fn get_or_init_test_data_database() -> &'static DatabaseConnection{
|
2025-08-18 08:22:20 +09:00
|
|
|
DATA_DATABASE_CONNECTION.get_or_init(CONFIG.get_or_init_test_default().await.storage.get_cache_database_path(), DataMigrator).await
|
2025-06-22 11:29:44 +09:00
|
|
|
}
|
|
|
|
|
pub async fn get_or_init_test_cache_database() -> &'static DatabaseConnection{
|
2025-08-18 08:22:20 +09:00
|
|
|
CACHE_DATABASE_CONNECTION.get_or_init(CONFIG.get_or_init_test_default()await.storage.get_cache_database_path(), CacheMigrator).await
|
2025-06-22 11:29:44 +09:00
|
|
|
}
|
|
|
|
|
}
|