From 228879a2c390e08c227b7b1b2a5f44e1c34bf4e3 Mon Sep 17 00:00:00 2001 From: Dawid Rycerz Date: Wed, 30 Jul 2025 19:26:07 +0300 Subject: feat: add automatic migrations --- src/main.rs | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 9f97e37..64da786 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,6 +18,7 @@ mod auth; mod config; mod health; mod locations; +mod migrations; mod notifications; mod users; mod weather_api_data; @@ -952,10 +953,11 @@ async fn main() -> anyhow::Result<()> { match cli.command.unwrap_or(Commands::Server) { Commands::Server => { - // Connect to database - let pool = SqlitePool::connect(&config.database_url) - .await - .expect("Failed to connect to DB"); + // Ensure database exists and run migrations + let pool = + migrations::MigrationManager::ensure_database_and_migrate(&config.database_url) + .await + .expect("Failed to setup database and run migrations"); // Create initial admin user if none exists { @@ -1017,7 +1019,9 @@ async fn main() -> anyhow::Result<()> { } } Commands::CreateUser { uuid } => { - let pool = SqlitePool::connect(&config.database_url).await?; + let pool = + migrations::MigrationManager::ensure_database_and_migrate(&config.database_url) + .await?; let repo = crate::users::UserRepository { db: &pool }; let user_id = uuid.unwrap_or_else(|| Uuid::new_v4().to_string()); let user = repo @@ -1075,9 +1079,10 @@ async fn main() -> anyhow::Result<()> { info!("User {} created", user_id); } Commands::CheckWeather => { - let pool = SqlitePool::connect(&config.database_url) - .await - .expect("Failed to connect to DB"); + let pool = + migrations::MigrationManager::ensure_database_and_migrate(&config.database_url) + .await + .expect("Failed to setup database and run migrations"); let poller = crate::weather_poller::WeatherPoller::new(Arc::new(pool)); info!("Manually triggering weather check..."); @@ -1115,6 +1120,7 @@ mod tests { .unwrap(); assert_eq!(response.status(), StatusCode::OK); let body = to_bytes(response.into_body(), 1024).await.unwrap(); - assert_eq!(&body[..], b"{\"status\":\"ok\"}"); + let body_str = std::str::from_utf8(&body).unwrap(); + assert!(body_str.contains("\"status\":\"ok\"")); } } -- cgit v1.2.3