From 56d839c1d2ec5967a8d4f0b19d68994add41fba6 Mon Sep 17 00:00:00 2001 From: fluo10 Date: Tue, 13 May 2025 06:03:16 +0900 Subject: [PATCH] Merge global database and config to Global --- progress-pile-client/src/entity/mod.rs | 7 ++-- progress-pile-client/src/global/database.rs | 36 ++++++++------------- progress-pile-client/src/global/mod.rs | 16 ++++++++- progress-pile-core/src/global/config.rs | 8 ++--- progress-pile-core/src/global/database.rs | 9 ++---- progress-pile-core/src/global/mod.rs | 4 +-- 6 files changed, 39 insertions(+), 41 deletions(-) diff --git a/progress-pile-client/src/entity/mod.rs b/progress-pile-client/src/entity/mod.rs index 288ba2c..b93f7cc 100644 --- a/progress-pile-client/src/entity/mod.rs +++ b/progress-pile-client/src/entity/mod.rs @@ -27,14 +27,11 @@ mod tests { use progress_pile_migration::{ClientMigrator, MigratorTrait}; use uuid::Uuid; use crate::error::Error; - use crate::global::database::{ - GLOBAL_DATABASE, - tests::* - }; + use crate::global::GLOBAL; #[tokio::test] async fn check_insert_entity() { - let db = GLOBAL_DATABASE.get_or_init_temp().await; + let db = GLOBAL.get_or_init_temporary_database().await; let category = ProgressCategoryActiveModel{ name: Set("test_category".to_owned()), diff --git a/progress-pile-client/src/global/database.rs b/progress-pile-client/src/global/database.rs index 7943e2e..e2474d7 100644 --- a/progress-pile-client/src/global/database.rs +++ b/progress-pile-client/src/global/database.rs @@ -4,31 +4,20 @@ use crate::error::Error; use tokio::sync::OnceCell; use progress_pile_core::global::GlobalDatabase; +use super::Global; -pub static GLOBAL_DATABASE: OnceGlobalDatabase = OnceGlobalDatabase{ - inner: OnceCell::const_new(), -}; -pub struct OnceGlobalDatabase { - inner: OnceCell, -} - -impl OnceGlobalDatabase { - -} - - -impl GlobalDatabase for OnceGlobalDatabase { - fn get(&self) -> Option<& DatabaseConnection> { - self.inner.get() +impl GlobalDatabase for Global { + fn get_database(&self) -> Option<&DatabaseConnection> { + self.database.get() } - async fn get_or_try_init(&self) -> Result<&DatabaseConnection, Error> { + async fn get_or_try_init_database(&self) -> Result<&DatabaseConnection, Error> { todo!() } - async fn get_or_try_init_with_connect_options(&self, options: T) -> Result<&DatabaseConnection, Error> where + async fn get_or_try_init_database_with_connect_options(&self, options: T) -> Result<&DatabaseConnection, Error> where T: Into, { - Ok(self.inner.get_or_try_init(|| async { + Ok(self.database.get_or_try_init(|| async { let db = Database::connect(options).await?; ClientMigrator::up(&db, None).await?; Ok::(db) @@ -40,7 +29,8 @@ impl GlobalDatabase for OnceGlobalDatabase { pub mod tests { use std::sync::LazyLock; - use tokio::sync::OnceCell; + use crate::global::GLOBAL; + use super::*; pub static TEST_DATABASE_URL: LazyLock = LazyLock::new(|| { @@ -51,16 +41,16 @@ pub mod tests { url }); - impl OnceGlobalDatabase { - pub async fn get_or_init_temp(&self) -> &DatabaseConnection { + impl Global { + pub async fn get_or_init_temporary_database(&self) -> &DatabaseConnection { - self.get_or_try_init_with_connect_options(&*TEST_DATABASE_URL).await.unwrap() + self.get_or_try_init_database_with_connect_options(&*TEST_DATABASE_URL).await.unwrap() } } #[tokio::test] async fn connect_database () { - let db = GLOBAL_DATABASE.get_or_init_temp().await; + let db = GLOBAL.get_or_init_temporary_database().await; assert!(db.ping().await.is_ok()); } diff --git a/progress-pile-client/src/global/mod.rs b/progress-pile-client/src/global/mod.rs index 3ea3f13..4999a79 100644 --- a/progress-pile-client/src/global/mod.rs +++ b/progress-pile-client/src/global/mod.rs @@ -1 +1,15 @@ -pub mod database; \ No newline at end of file +use crate::config::ClientConfig; +use sea_orm::DatabaseConnection; +use tokio::sync::OnceCell; + + +mod database; + +pub static GLOBAL: Global = Global{ + config: OnceCell::const_new(), + database: OnceCell::const_new(), +}; +pub struct Global { + config: OnceCell, + database: OnceCell, +} diff --git a/progress-pile-core/src/global/config.rs b/progress-pile-core/src/global/config.rs index 48012fe..82e29ac 100644 --- a/progress-pile-core/src/global/config.rs +++ b/progress-pile-core/src/global/config.rs @@ -2,9 +2,9 @@ use chrono_tz::Tz; use crate::{config::DatabaseConfig, error::Error}; -pub trait GlobalConfigTrait { - fn get(&self) -> Option; - fn get_or_try_init(&self) -> Result; +pub trait GlobalConfig { + fn get_config(&self) -> Option; + fn get_or_try_init_config(&self) -> Result; fn get_database_config(&self) -> Option; - fn get_time_zone(&self) -> Option; + fn get_time_zone(&self) -> Option; } \ No newline at end of file diff --git a/progress-pile-core/src/global/database.rs b/progress-pile-core/src/global/database.rs index 6fd9468..25f8c12 100644 --- a/progress-pile-core/src/global/database.rs +++ b/progress-pile-core/src/global/database.rs @@ -4,13 +4,10 @@ use sea_orm::{ConnectOptions, Database, DatabaseConnection, DbErr}; use sea_orm_migration::MigratorTrait; use tokio::sync::OnceCell; - -pub static DATABASE_CONNECTION: OnceCell = OnceCell::const_new(); - pub trait GlobalDatabase { - fn get(&self) -> Option<&DatabaseConnection>; - async fn get_or_try_init(&self) -> Result<&DatabaseConnection, Error>; - async fn get_or_try_init_with_connect_options(&self, options: T) -> Result<&DatabaseConnection, Error> where + fn get_database(&self) -> Option<&DatabaseConnection>; + async fn get_or_try_init_database(&self) -> Result<&DatabaseConnection, Error>; + async fn get_or_try_init_database_with_connect_options(&self, options: T) -> Result<&DatabaseConnection, Error> where T: Into; } diff --git a/progress-pile-core/src/global/mod.rs b/progress-pile-core/src/global/mod.rs index 4fc8d4d..fcd023e 100644 --- a/progress-pile-core/src/global/mod.rs +++ b/progress-pile-core/src/global/mod.rs @@ -1,5 +1,5 @@ -pub mod config; +mod config; mod database; +pub use config::GlobalConfig; pub use database::GlobalDatabase; -pub use database::DATABASE_CONNECTION; \ No newline at end of file