From 42bc1d59f74a997ca3ebf32ba7562a8b9a85cc4b Mon Sep 17 00:00:00 2001 From: fluo10 Date: Mon, 12 May 2025 20:29:46 +0900 Subject: [PATCH] Implement temp database --- Cargo.lock | 5 +- progress-pile-client/Cargo.toml | 5 +- progress-pile-client/src/entity/mod.rs | 25 +++----- progress-pile-client/src/global/database.rs | 67 +++++++++++++++++++++ progress-pile-client/src/global/mod.rs | 1 + progress-pile-client/src/lib.rs | 1 + progress-pile-core/src/global/config.rs | 10 +++ progress-pile-core/src/global/database.rs | 55 ++--------------- progress-pile-core/src/global/mod.rs | 3 +- 9 files changed, 104 insertions(+), 68 deletions(-) create mode 100644 progress-pile-client/src/global/database.rs create mode 100644 progress-pile-client/src/global/mod.rs diff --git a/Cargo.lock b/Cargo.lock index 4cfae41..b428e24 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2266,6 +2266,7 @@ dependencies = [ "progress-pile-migration", "sea-orm", "serde", + "tempfile", "thiserror 2.0.12", "tokio", "toml", @@ -3245,9 +3246,9 @@ dependencies = [ [[package]] name = "tempfile" -version = "3.19.1" +version = "3.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7437ac7763b9b123ccf33c338a5cc1bac6f69b45a136c19bdd8a65e3916435bf" +checksum = "e8a64e3985349f2441a1a9ef0b853f869006c3855f2cda6862a94d26ebb9d6a1" dependencies = [ "fastrand", "getrandom 0.3.2", diff --git a/progress-pile-client/Cargo.toml b/progress-pile-client/Cargo.toml index 4d1fab7..84adc71 100644 --- a/progress-pile-client/Cargo.toml +++ b/progress-pile-client/Cargo.toml @@ -20,4 +20,7 @@ serde.workspace = true thiserror.workspace = true tokio.workspace = true toml.workspace = true -uuid.workspace = true \ No newline at end of file +uuid.workspace = true + +[dev-dependencies] +tempfile = "3.20.0" diff --git a/progress-pile-client/src/entity/mod.rs b/progress-pile-client/src/entity/mod.rs index 54b5b46..288ba2c 100644 --- a/progress-pile-client/src/entity/mod.rs +++ b/progress-pile-client/src/entity/mod.rs @@ -17,25 +17,24 @@ pub use progress_entry::{ #[cfg(test)] mod tests { + use std::time::Duration; + use super::*; use chrono::Local; use progress_pile_core::global::GlobalDatabase; - use sea_orm::{entity::*, DatabaseConnection}; + use sea_orm::{entity::*, ConnectOptions, Database, DatabaseConnection}; use progress_pile_migration::{ClientMigrator, MigratorTrait}; use uuid::Uuid; use crate::error::Error; + use crate::global::database::{ + GLOBAL_DATABASE, + tests::* + }; - async fn get_or_try_init_test_database() -> Result<&'static DatabaseConnection, Error> { - Ok(GlobalDatabase::get_or_try_init("sqlite::memory:", ClientMigrator).await?) - - } - - - - #[tokio::test] + #[tokio::test] async fn check_insert_entity() { - let db = get_or_try_init_test_database().await.unwrap(); + let db = GLOBAL_DATABASE.get_or_init_temp().await; let category = ProgressCategoryActiveModel{ name: Set("test_category".to_owned()), @@ -47,9 +46,5 @@ mod tests { ..ProgressEntryActiveModel::new() }.insert(db).await.unwrap(); } - #[tokio::test] - async fn connect_database () { - let db = get_or_try_init_test_database().await.unwrap(); - assert!(db.ping().await.is_ok()); - } + } \ No newline at end of file diff --git a/progress-pile-client/src/global/database.rs b/progress-pile-client/src/global/database.rs new file mode 100644 index 0000000..7943e2e --- /dev/null +++ b/progress-pile-client/src/global/database.rs @@ -0,0 +1,67 @@ +use progress_pile_migration::{ClientMigrator, MigratorTrait}; +use sea_orm::{ConnectOptions, Database, DatabaseConnection}; +use crate::error::Error; +use tokio::sync::OnceCell; +use progress_pile_core::global::GlobalDatabase; + + +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() + } + async fn get_or_try_init(&self) -> Result<&DatabaseConnection, Error> { + todo!() + } + + async fn get_or_try_init_with_connect_options(&self, options: T) -> Result<&DatabaseConnection, Error> where + T: Into, + { + Ok(self.inner.get_or_try_init(|| async { + let db = Database::connect(options).await?; + ClientMigrator::up(&db, None).await?; + Ok::(db) + }).await?) + } +} + +#[cfg(test)] +pub mod tests { + use std::sync::LazyLock; + + use tokio::sync::OnceCell; + use super::*; + + pub static TEST_DATABASE_URL: LazyLock = LazyLock::new(|| { + let mut temp_path = tempfile::NamedTempFile::new().unwrap().into_temp_path(); + temp_path.disable_cleanup(true); + let url = "sqlite://".to_string() + temp_path.as_os_str().to_str().unwrap() + "?mode=rwc"; + println!("{}", &url); + url + }); + + impl OnceGlobalDatabase { + pub async fn get_or_init_temp(&self) -> &DatabaseConnection { + + self.get_or_try_init_with_connect_options(&*TEST_DATABASE_URL).await.unwrap() + } + } + + #[tokio::test] + async fn connect_database () { + let db = GLOBAL_DATABASE.get_or_init_temp().await; + assert!(db.ping().await.is_ok()); + } + +} \ No newline at end of file diff --git a/progress-pile-client/src/global/mod.rs b/progress-pile-client/src/global/mod.rs new file mode 100644 index 0000000..3ea3f13 --- /dev/null +++ b/progress-pile-client/src/global/mod.rs @@ -0,0 +1 @@ +pub mod database; \ No newline at end of file diff --git a/progress-pile-client/src/lib.rs b/progress-pile-client/src/lib.rs index 7d50681..2c6b204 100644 --- a/progress-pile-client/src/lib.rs +++ b/progress-pile-client/src/lib.rs @@ -1,6 +1,7 @@ pub mod auth; pub mod config; pub mod entity; +pub mod global; pub use progress_pile_core::{ error, diff --git a/progress-pile-core/src/global/config.rs b/progress-pile-core/src/global/config.rs index e69de29..48012fe 100644 --- a/progress-pile-core/src/global/config.rs +++ b/progress-pile-core/src/global/config.rs @@ -0,0 +1,10 @@ +use chrono_tz::Tz; + +use crate::{config::DatabaseConfig, error::Error}; + +pub trait GlobalConfigTrait { + fn get(&self) -> Option; + fn get_or_try_init(&self) -> Result; + fn get_database_config(&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 c87645a..6fd9468 100644 --- a/progress-pile-core/src/global/database.rs +++ b/progress-pile-core/src/global/database.rs @@ -1,60 +1,17 @@ use crate::error::Error; -use sea_orm::{ConnectOptions, Database, DatabaseConnection}; +use sea_orm::{ConnectOptions, Database, DatabaseConnection, DbErr}; use sea_orm_migration::MigratorTrait; use tokio::sync::OnceCell; -/* -pub struct OnceDatabaseConnection { - inner: OnceCell, -} - -impl OnceDatabaseConnection { - const fn new() -> Self { - Self { - inner: OnceCell::const_new(), - } - } - pub fn get(&self) -> Option<&DatabaseConnection> { - self.inner.get() - } - - pub async fn get_or_try_init(&self, options: T, _: U) -> Result<&DatabaseConnection, Error> where - T: Into, - U: MigratorTrait - { - self.inner.get_or_try_init(|| async { - let db = Database::connect(options).await?; - U::up(&db, None).await?; - Ok(db) - }).await -} - -} - - -pub static DATABASE_CONNECTION: OnceDatabaseConnection = OnceDatabaseConnection::new(); -*/ pub static DATABASE_CONNECTION: OnceCell = OnceCell::const_new(); -pub struct GlobalDatabase; - -impl GlobalDatabase { - pub fn get() -> Option<&'static DatabaseConnection> { - DATABASE_CONNECTION.get() - } - - pub async fn get_or_try_init(options: T, _: U) -> Result<&'static DatabaseConnection, Error> where - T: Into, - U: MigratorTrait - { - DATABASE_CONNECTION.get_or_try_init(|| async { - let db = Database::connect(options).await?; - U::up(&db, None).await?; - Ok(db) - }).await - } +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 + T: Into; } #[cfg(test)] diff --git a/progress-pile-core/src/global/mod.rs b/progress-pile-core/src/global/mod.rs index 6e52323..4fc8d4d 100644 --- a/progress-pile-core/src/global/mod.rs +++ b/progress-pile-core/src/global/mod.rs @@ -1,4 +1,5 @@ pub mod config; mod database; -pub use database::GlobalDatabase; \ No newline at end of file +pub use database::GlobalDatabase; +pub use database::DATABASE_CONNECTION; \ No newline at end of file