Update database initialization
This commit is contained in:
parent
d93d3a83d1
commit
d2b63140fd
3 changed files with 11 additions and 16 deletions
|
@ -14,10 +14,7 @@ pub mod tests {
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
pub async fn get_or_init_temporary_database() -> &'static DatabaseConnection {
|
pub async fn get_or_init_temporary_database() -> &'static DatabaseConnection {
|
||||||
global::GLOBAL.get_or_try_init_temporary_database( |x| async {
|
global::GLOBAL.get_or_try_init_temporary_database(migration::Migrator).await.unwrap()
|
||||||
migration::Migrator::up(&x, None).await?;
|
|
||||||
Ok(x)
|
|
||||||
}).await.unwrap()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,7 @@ chrono-tz = "0.10.3"
|
||||||
clap = { version = "4.5.38", features = ["derive"] }
|
clap = { version = "4.5.38", features = ["derive"] }
|
||||||
libp2p.workspace = true
|
libp2p.workspace = true
|
||||||
sea-orm = { version = "1.1.11", features = ["sqlx-sqlite", "runtime-tokio-native-tls", "macros", "with-chrono", "with-uuid"] }
|
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"] }
|
serde = { version = "1.0.219", features = ["derive"] }
|
||||||
tempfile = { version = "3.20.0", optional = true }
|
tempfile = { version = "3.20.0", optional = true }
|
||||||
thiserror = "2.0.12"
|
thiserror = "2.0.12"
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
use sea_orm::{ConnectOptions, Database, DbErr, DatabaseConnection};
|
use sea_orm::{ConnectOptions, Database, DbErr, DatabaseConnection};
|
||||||
|
use sea_orm_migration::MigratorTrait;
|
||||||
use crate::error::Error;
|
use crate::error::Error;
|
||||||
use tokio::sync::OnceCell;
|
use tokio::sync::OnceCell;
|
||||||
|
|
||||||
|
@ -18,26 +19,25 @@ impl Global {
|
||||||
fn get_database(&self) -> Option<&DatabaseConnection> {
|
fn get_database(&self) -> Option<&DatabaseConnection> {
|
||||||
self.database.get()
|
self.database.get()
|
||||||
}
|
}
|
||||||
async fn get_or_try_init_database<T, F, Fut>(&self, path: T, migration: F) -> Result<&DatabaseConnection, Error>
|
async fn get_or_try_init_database<T, U>(&self, path: T, _: U) -> Result<&DatabaseConnection, Error>
|
||||||
where
|
where
|
||||||
T: AsRef<Path>,
|
T: AsRef<Path>,
|
||||||
F: FnOnce(DatabaseConnection) -> Fut,
|
U: MigratorTrait,
|
||||||
Fut: Future<Output = Result<DatabaseConnection, DbErr>>
|
|
||||||
{
|
{
|
||||||
let url = "sqlite://".to_string() + path.as_ref().to_str().unwrap() + "?mode=rwc";
|
let url = "sqlite://".to_string() + path.as_ref().to_str().unwrap() + "?mode=rwc";
|
||||||
|
|
||||||
Ok(self.database.get_or_try_init(|| async {
|
Ok(self.database.get_or_try_init(|| async {
|
||||||
let db = Database::connect(&url).await?;
|
let db = Database::connect(&url).await?;
|
||||||
Ok::<DatabaseConnection, DbErr>(migration(db).await?)
|
U::up(&db, None).await?;
|
||||||
|
Ok::<DatabaseConnection, DbErr>(db)
|
||||||
}).await?)
|
}).await?)
|
||||||
}
|
}
|
||||||
#[cfg(any(test, feature="test"))]
|
#[cfg(any(test, feature="test"))]
|
||||||
pub async fn get_or_try_init_temporary_database<F, Fut>(&self, migration: F) -> Result<&DatabaseConnection, Error>
|
pub async fn get_or_try_init_temporary_database<T>(&self, migrator: T) -> Result<&DatabaseConnection, Error>
|
||||||
where
|
where
|
||||||
F: FnOnce(DatabaseConnection) -> Fut,
|
T: MigratorTrait,
|
||||||
Fut: Future<Output = Result<DatabaseConnection, DbErr>>
|
|
||||||
{
|
{
|
||||||
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::*;
|
use super::*;
|
||||||
|
|
||||||
pub async fn get_or_init_temporary_database() -> &'static DatabaseConnection {
|
pub async fn get_or_init_temporary_database() -> &'static DatabaseConnection {
|
||||||
GLOBAL.get_or_try_init_temporary_database( |x| async {
|
GLOBAL.get_or_try_init_temporary_database( Migrator).await.unwrap()
|
||||||
Migrator::up(&x, None).await?;
|
|
||||||
Ok(x)
|
|
||||||
}).await.unwrap()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
|
|
Loading…
Add table
Reference in a new issue