Merge global database and config to Global
This commit is contained in:
parent
42bc1d59f7
commit
56d839c1d2
6 changed files with 39 additions and 41 deletions
|
@ -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()),
|
||||
|
|
|
@ -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<DatabaseConnection>,
|
||||
}
|
||||
|
||||
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<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>,
|
||||
{
|
||||
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::<DatabaseConnection, Error>(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<String> = 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());
|
||||
}
|
||||
|
||||
|
|
|
@ -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>,
|
||||
}
|
||||
|
|
|
@ -2,9 +2,9 @@ use chrono_tz::Tz;
|
|||
|
||||
use crate::{config::DatabaseConfig, error::Error};
|
||||
|
||||
pub trait GlobalConfigTrait<T> {
|
||||
fn get(&self) -> Option<T>;
|
||||
fn get_or_try_init(&self) -> Result<T, Error>;
|
||||
pub trait GlobalConfig<T> {
|
||||
fn get_config(&self) -> Option<T>;
|
||||
fn get_or_try_init_config(&self) -> Result<T, Error>;
|
||||
fn get_database_config(&self) -> Option<DatabaseConfig>;
|
||||
fn get_time_zone(&self) -> Option<Tz>;
|
||||
fn get_time_zone(&self) -> Option<Tz>;
|
||||
}
|
|
@ -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<DatabaseConnection> = 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<T>(&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<T>(&self, options: T) -> Result<&DatabaseConnection, Error> where
|
||||
T: Into<ConnectOptions>;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
pub mod config;
|
||||
mod config;
|
||||
mod database;
|
||||
|
||||
pub use config::GlobalConfig;
|
||||
pub use database::GlobalDatabase;
|
||||
pub use database::DATABASE_CONNECTION;
|
Loading…
Add table
Reference in a new issue