From d2b63140fd85fffd3c16b0888e09bfb7ce502acd Mon Sep 17 00:00:00 2001 From: fluo10 Date: Wed, 28 May 2025 12:55:09 +0900 Subject: [PATCH] Update database initialization --- examples/simple-list/src/main.rs | 5 +---- lazy-supplements/Cargo.toml | 1 + lazy-supplements/src/global/database.rs | 21 +++++++++------------ 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/examples/simple-list/src/main.rs b/examples/simple-list/src/main.rs index d90ff84..5388b04 100644 --- a/examples/simple-list/src/main.rs +++ b/examples/simple-list/src/main.rs @@ -14,10 +14,7 @@ pub mod tests { use super::*; pub async fn get_or_init_temporary_database() -> &'static DatabaseConnection { - global::GLOBAL.get_or_try_init_temporary_database( |x| async { - migration::Migrator::up(&x, None).await?; - Ok(x) - }).await.unwrap() + global::GLOBAL.get_or_try_init_temporary_database(migration::Migrator).await.unwrap() } } diff --git a/lazy-supplements/Cargo.toml b/lazy-supplements/Cargo.toml index 2826cbc..35fc5fa 100644 --- a/lazy-supplements/Cargo.toml +++ b/lazy-supplements/Cargo.toml @@ -17,6 +17,7 @@ chrono-tz = "0.10.3" clap = { version = "4.5.38", features = ["derive"] } libp2p.workspace = true sea-orm = { version = "1.1.11", features = ["sqlx-sqlite", "runtime-tokio-native-tls", "macros", "with-chrono", "with-uuid"] } +sea-orm-migration.workspace = true serde = { version = "1.0.219", features = ["derive"] } tempfile = { version = "3.20.0", optional = true } thiserror = "2.0.12" diff --git a/lazy-supplements/src/global/database.rs b/lazy-supplements/src/global/database.rs index a8c0ad3..86e8a10 100644 --- a/lazy-supplements/src/global/database.rs +++ b/lazy-supplements/src/global/database.rs @@ -1,6 +1,7 @@ use std::path::Path; use sea_orm::{ConnectOptions, Database, DbErr, DatabaseConnection}; +use sea_orm_migration::MigratorTrait; use crate::error::Error; use tokio::sync::OnceCell; @@ -18,26 +19,25 @@ impl Global { fn get_database(&self) -> Option<&DatabaseConnection> { self.database.get() } - async fn get_or_try_init_database(&self, path: T, migration: F) -> Result<&DatabaseConnection, Error> + async fn get_or_try_init_database(&self, path: T, _: U) -> Result<&DatabaseConnection, Error> where T: AsRef, - F: FnOnce(DatabaseConnection) -> Fut, - Fut: Future> + U: MigratorTrait, { let url = "sqlite://".to_string() + path.as_ref().to_str().unwrap() + "?mode=rwc"; Ok(self.database.get_or_try_init(|| async { let db = Database::connect(&url).await?; - Ok::(migration(db).await?) + U::up(&db, None).await?; + Ok::(db) }).await?) } #[cfg(any(test, feature="test"))] - pub async fn get_or_try_init_temporary_database(&self, migration: F) -> Result<&DatabaseConnection, Error> + pub async fn get_or_try_init_temporary_database(&self, migrator: T) -> Result<&DatabaseConnection, Error> where - F: FnOnce(DatabaseConnection) -> Fut, - Fut: Future> + T: MigratorTrait, { - self.get_or_try_init_database(&*TEST_DATABASE_URL, migration).await + self.get_or_try_init_database(&*TEST_DATABASE_URL, migrator).await } } @@ -56,10 +56,7 @@ pub mod tests { use super::*; pub async fn get_or_init_temporary_database() -> &'static DatabaseConnection { - GLOBAL.get_or_try_init_temporary_database( |x| async { - Migrator::up(&x, None).await?; - Ok(x) - }).await.unwrap() + GLOBAL.get_or_try_init_temporary_database( Migrator).await.unwrap() } #[tokio::test]