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/health.rs | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 55 insertions(+), 4 deletions(-) (limited to 'src/health.rs') diff --git a/src/health.rs b/src/health.rs index c3ae80a..546d5d4 100644 --- a/src/health.rs +++ b/src/health.rs @@ -1,16 +1,67 @@ -use axum::{Json, response::IntoResponse}; +use axum::Json; use serde_json::json; +use sqlx::SqlitePool; +use std::sync::Arc; +use tracing::warn; + +use crate::migrations::MigrationManager; #[utoipa::path( get, path = "/health", responses( - (status = 200, description = "Health check", body = inline(HealthResponse)) + (status = 200, description = "Health check response", body = serde_json::Value) ), tag = "health" )] -pub async fn health_handler() -> impl IntoResponse { - Json(json!({"status": "ok"})) +pub async fn health_handler( + axum::extract::State(pool): axum::extract::State>, +) -> Json { + let mut status = "ok"; + let details; + + // Check database connectivity + match sqlx::query("SELECT 1").execute(&*pool).await { + Ok(_) => { + // Check migration status + match MigrationManager::check_migrations_status(&pool).await { + Ok(migrations_ok) => { + if migrations_ok { + details = json!({ + "database": "connected", + "migrations": "up_to_date" + }); + } else { + warn!("Health check: migrations are not up to date"); + details = json!({ + "database": "connected", + "migrations": "pending" + }); + } + } + Err(e) => { + warn!("Health check: failed to check migration status: {}", e); + details = json!({ + "database": "connected", + "migrations": "unknown", + "migration_error": e.to_string() + }); + } + } + } + Err(e) => { + status = "error"; + details = json!({ + "database": "disconnected", + "error": e.to_string() + }); + } + } + + Json(json!({ + "status": status, + "details": details + })) } #[derive(utoipa::ToSchema, serde::Serialize)] -- cgit v1.2.3