From 7bebdf7490b828c384659b2ac60ebe67dd5d532e Mon Sep 17 00:00:00 2001 From: fluo10 Date: Sat, 3 May 2025 15:31:13 +0900 Subject: [PATCH] Implement Into for &DatabaseConfig --- Cargo.lock | 1 + Cargo.toml | 11 +++++++++++ dpts-config/Cargo.toml | 1 + dpts-config/src/database.rs | 29 +++++++++++++++++++++++++++++ dpts-database/Cargo.toml | 12 ++---------- dpts-database/src/connection.rs | 12 +----------- 6 files changed, 45 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 771ccaf..53169a8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -821,6 +821,7 @@ dependencies = [ "dpts-entity", "dpts-error", "iana-time-zone", + "sea-orm", "serde", "thiserror 2.0.12", "tokio", diff --git a/Cargo.toml b/Cargo.toml index e292f05..27cc32d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,6 +33,17 @@ thiserror = "2.0.12" tokio = "1.44.2" toml = "0.8.22" +[workspace.dependencies.sea-orm] +version = "1.1" +features = [ + "macros", + "debug-print", + "runtime-tokio-native-tls", + "sqlx-sqlite", + "with-chrono", +] +default-features = false + [workspace.package] version = "0.1.0" edition = "2024" diff --git a/dpts-config/Cargo.toml b/dpts-config/Cargo.toml index c30c131..f22caea 100644 --- a/dpts-config/Cargo.toml +++ b/dpts-config/Cargo.toml @@ -16,6 +16,7 @@ chrono-tz.workspace = true dpts-entity.workspace = true dpts-error.workspace = true iana-time-zone = "0.1.63" +sea-orm.workspace = true serde.workspace = true thiserror.workspace = true tokio.workspace = true diff --git a/dpts-config/src/database.rs b/dpts-config/src/database.rs index 898ae1f..8f49b9f 100644 --- a/dpts-config/src/database.rs +++ b/dpts-config/src/database.rs @@ -1,5 +1,6 @@ use std::time::Duration; +use sea_orm::ConnectOptions; use serde::Deserialize; use crate::Error; @@ -16,6 +17,33 @@ pub struct DatabaseConfig { pub sqlx_logging: bool, } +impl Into for &DatabaseConfig { + fn into(self) -> ConnectOptions { + let mut opt = ConnectOptions::new(&self.url); + if let Some(x) = self.max_connections { + opt.max_connections(x); + } + if let Some(x) = self.min_connections { + opt.min_connections(x); + } + if let Some(x) = self.connect_timeout { + opt.connect_timeout(x); + } + if let Some(x) = self.acquire_timeout { + opt.acquire_timeout(x); + } + if let Some(x) = self.idle_timeout { + opt.idle_timeout(x); + } + if let Some(x) = self.max_lifetime { + opt.max_lifetime(x); + } + opt.sqlx_logging(self.sqlx_logging); + + opt + } +} + impl TryFrom for DatabaseConfig{ type Error = Error; fn try_from(p: PartialDatabaseConfig) -> Result { @@ -31,6 +59,7 @@ impl TryFrom for DatabaseConfig{ }) } } + #[derive(Clone, Debug, Default, Deserialize, PartialEq)] pub struct PartialDatabaseConfig { pub url: Option, diff --git a/dpts-database/Cargo.toml b/dpts-database/Cargo.toml index 14b9fe6..919569f 100644 --- a/dpts-database/Cargo.toml +++ b/dpts-database/Cargo.toml @@ -20,18 +20,10 @@ axum = "0.8" chrono = {workspace = true} dotenv = {workspace = true} log = "0.4.27" +sea-orm.workspace = true tokio.workspace = true -[dependencies.sea-orm] -version = "1.1" -features = [ - "macros", - "debug-print", - "runtime-tokio-native-tls", - "sqlx-sqlite", - "with-chrono", -] -default-features = false + [dev-dependencies] dpts-config = { workspace = true, features = ["test"] } \ No newline at end of file diff --git a/dpts-database/src/connection.rs b/dpts-database/src/connection.rs index 96b8c07..a5908cf 100644 --- a/dpts-database/src/connection.rs +++ b/dpts-database/src/connection.rs @@ -27,17 +27,7 @@ impl OnceDatabaseConnection { pub async fn get_or_init_with_server_config(&self, c: &ServerConfig) -> &DatabaseConnection { self.get_or_init( || async { - let mut opt = ConnectOptions::new(&c.database_url); - opt.max_connections(100) - .min_connections(5) - .connect_timeout(Duration::from_secs(8)) - .acquire_timeout(Duration::from_secs(8)) - .idle_timeout(Duration::from_secs(8)) - .max_lifetime(Duration::from_secs(8)) - .sqlx_logging(true) - .sqlx_logging_level(log::LevelFilter::Info); - //.set_schema_search_path("my_schema"); // Setting default PostgreSQL schema - let db = Database::connect(opt).await.unwrap(); + let db = Database::connect(&c.database).await.unwrap(); Migrator::fresh(&db).await.unwrap(); db }).await