Merge global database and config to Global

This commit is contained in:
fluo10 2025-05-13 06:03:16 +09:00
parent 42bc1d59f7
commit 56d839c1d2
6 changed files with 39 additions and 41 deletions

View file

@ -27,14 +27,11 @@ mod tests {
use progress_pile_migration::{ClientMigrator, MigratorTrait}; use progress_pile_migration::{ClientMigrator, MigratorTrait};
use uuid::Uuid; use uuid::Uuid;
use crate::error::Error; use crate::error::Error;
use crate::global::database::{ use crate::global::GLOBAL;
GLOBAL_DATABASE,
tests::*
};
#[tokio::test] #[tokio::test]
async fn check_insert_entity() { 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{ let category = ProgressCategoryActiveModel{
name: Set("test_category".to_owned()), name: Set("test_category".to_owned()),

View file

@ -4,31 +4,20 @@ use crate::error::Error;
use tokio::sync::OnceCell; use tokio::sync::OnceCell;
use progress_pile_core::global::GlobalDatabase; use progress_pile_core::global::GlobalDatabase;
use super::Global;
pub static GLOBAL_DATABASE: OnceGlobalDatabase = OnceGlobalDatabase{ impl GlobalDatabase for Global {
inner: OnceCell::const_new(), fn get_database(&self) -> Option<&DatabaseConnection> {
}; self.database.get()
pub struct OnceGlobalDatabase {
inner: OnceCell<DatabaseConnection>,
} }
async fn get_or_try_init_database(&self) -> Result<&DatabaseConnection, Error> {
impl OnceGlobalDatabase {
}
impl GlobalDatabase for OnceGlobalDatabase {
fn get(&self) -> Option<& DatabaseConnection> {
self.inner.get()
}
async fn get_or_try_init(&self) -> Result<&DatabaseConnection, Error> {
todo!() todo!()
} }
async fn get_or_try_init_with_connect_options<T>(&self, options: T) -> Result<&DatabaseConnection, Error> where async fn get_or_try_init_database_with_connect_options<T>(&self, options: T) -> Result<&DatabaseConnection, Error> where
T: Into<ConnectOptions>, T: Into<ConnectOptions>,
{ {
Ok(self.inner.get_or_try_init(|| async { Ok(self.database.get_or_try_init(|| async {
let db = Database::connect(options).await?; let db = Database::connect(options).await?;
ClientMigrator::up(&db, None).await?; ClientMigrator::up(&db, None).await?;
Ok::<DatabaseConnection, Error>(db) Ok::<DatabaseConnection, Error>(db)
@ -40,7 +29,8 @@ impl GlobalDatabase for OnceGlobalDatabase {
pub mod tests { pub mod tests {
use std::sync::LazyLock; use std::sync::LazyLock;
use tokio::sync::OnceCell; use crate::global::GLOBAL;
use super::*; use super::*;
pub static TEST_DATABASE_URL: LazyLock<String> = LazyLock::new(|| { pub static TEST_DATABASE_URL: LazyLock<String> = LazyLock::new(|| {
@ -51,16 +41,16 @@ pub mod tests {
url url
}); });
impl OnceGlobalDatabase { impl Global {
pub async fn get_or_init_temp(&self) -> &DatabaseConnection { 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] #[tokio::test]
async fn connect_database () { 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()); assert!(db.ping().await.is_ok());
} }

View file

@ -1 +1,15 @@
pub mod database; 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<ClientConfig>,
database: OnceCell<DatabaseConnection>,
}

View file

@ -2,9 +2,9 @@ use chrono_tz::Tz;
use crate::{config::DatabaseConfig, error::Error}; use crate::{config::DatabaseConfig, error::Error};
pub trait GlobalConfigTrait<T> { pub trait GlobalConfig<T> {
fn get(&self) -> Option<T>; fn get_config(&self) -> Option<T>;
fn get_or_try_init(&self) -> Result<T, Error>; fn get_or_try_init_config(&self) -> Result<T, Error>;
fn get_database_config(&self) -> Option<DatabaseConfig>; fn get_database_config(&self) -> Option<DatabaseConfig>;
fn get_time_zone(&self) -> Option<Tz>; fn get_time_zone(&self) -> Option<Tz>;
} }

View file

@ -4,13 +4,10 @@ use sea_orm::{ConnectOptions, Database, DatabaseConnection, DbErr};
use sea_orm_migration::MigratorTrait; use sea_orm_migration::MigratorTrait;
use tokio::sync::OnceCell; use tokio::sync::OnceCell;
pub static DATABASE_CONNECTION: OnceCell<DatabaseConnection> = OnceCell::const_new();
pub trait GlobalDatabase { pub trait GlobalDatabase {
fn get(&self) -> Option<&DatabaseConnection>; fn get_database(&self) -> Option<&DatabaseConnection>;
async fn get_or_try_init(&self) -> Result<&DatabaseConnection, Error>; async fn get_or_try_init_database(&self) -> Result<&DatabaseConnection, Error>;
async fn get_or_try_init_with_connect_options<T>(&self, options: T) -> Result<&DatabaseConnection, Error> where async fn get_or_try_init_database_with_connect_options<T>(&self, options: T) -> Result<&DatabaseConnection, Error> where
T: Into<ConnectOptions>; T: Into<ConnectOptions>;
} }

View file

@ -1,5 +1,5 @@
pub mod config; mod config;
mod database; mod database;
pub use config::GlobalConfig;
pub use database::GlobalDatabase; pub use database::GlobalDatabase;
pub use database::DATABASE_CONNECTION;