From 84dca02c34bf8984d81568780aa5268e61735a30 Mon Sep 17 00:00:00 2001 From: n08i40k Date: Wed, 10 Sep 2025 20:04:19 +0400 Subject: [PATCH] fix(database): use migrator and change connection options --- src/state/mod.rs | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/state/mod.rs b/src/state/mod.rs index 9e9e064..ba76901 100644 --- a/src/state/mod.rs +++ b/src/state/mod.rs @@ -2,10 +2,12 @@ mod env; pub use crate::state::env::AppEnv; use actix_web::web; -use database::sea_orm::{Database, DatabaseConnection}; +use database::migration::{Migrator, MigratorTrait}; +use database::sea_orm::{ConnectOptions, Database, DatabaseConnection}; use providers::base::{ScheduleProvider, ScheduleSnapshot}; use std::collections::HashMap; use std::sync::Arc; +use std::time::Duration; use tokio_util::sync::CancellationToken; /// Common data provided to endpoints. @@ -55,9 +57,24 @@ impl AppState { database } else { let database_url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set"); - Database::connect(&database_url) + + let mut opt = ConnectOptions::new(database_url.clone()); + + opt.max_connections(4) + .min_connections(2) + .connect_timeout(Duration::from_secs(10)) + .idle_timeout(Duration::from_secs(8)) + .sqlx_logging(true); + + let database = Database::connect(opt) .await - .unwrap_or_else(|_| panic!("Error connecting to {}", database_url)) + .unwrap_or_else(|_| panic!("Error connecting to {}", database_url)); + + Migrator::up(&database, None) + .await + .expect("Failed to run database migrations"); + + database }, env, providers,