summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorDawid Rycerz <dawid@rycerz.xyz>2025-07-30 19:26:07 +0300
committerDawid Rycerz <dawid@rycerz.xyz>2025-07-30 19:26:07 +0300
commit228879a2c390e08c227b7b1b2a5f44e1c34bf4e3 (patch)
treeb1af3a0008ead34805e267c5d9c356873952961b /src/main.rs
parent934fb31059da10fa843d96a10c37f181eaa89456 (diff)
feat: add automatic migrations
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs24
1 files changed, 15 insertions, 9 deletions
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\""));
}
}