@@ -562,6 +562,103 @@ async def check_cache_health() -> ComponentStatus:
562562{% endif %}
563563
564564
565+ {% if cookiecutter .include_database == "yes" %}
566+ async def check_database_health() -> ComponentStatus:
567+ """
568+ Check database connectivity and basic functionality.
569+
570+ Returns:
571+ ComponentStatus indicating database health
572+ """
573+ try:
574+ # Import db_session from generated project
575+ from app.core.db import db_session
576+ from pathlib import Path
577+
578+ # Check if database file exists for SQLite
579+ db_url = settings.DATABASE_URL
580+ if db_url.startswith("sqlite:///"):
581+ # Extract path from SQLite URL
582+ db_path = db_url.replace("sqlite:///", "").lstrip("./")
583+
584+ # Check if database file exists
585+ if not Path(db_path).exists():
586+ return ComponentStatus(
587+ name="database",
588+ status=ComponentStatusType.WARNING,
589+ message="Database not initialized - file does not exist",
590+ response_time_ms=None,
591+ metadata={
592+ "implementation": "sqlite",
593+ "database_exists": False,
594+ "expected_path": db_path,
595+ "url": settings.DATABASE_URL,
596+ "recommendation": "Run database migrations or create database file",
597+ },
598+ )
599+
600+ # Test database connection with simple query
601+ with db_session(autocommit=False) as session:
602+ # Execute a simple query to test connectivity
603+ from sqlalchemy import text
604+ session.execute(text("SELECT 1"))
605+ # No need to commit since we're just testing connectivity
606+
607+ return ComponentStatus(
608+ name="database",
609+ status=ComponentStatusType.HEALTHY,
610+ message="Database connection successful",
611+ response_time_ms=None, # Will be set by caller
612+ metadata={
613+ "implementation": "sqlite",
614+ "url": settings.DATABASE_URL,
615+ "database_exists": True,
616+ "engine_echo": settings.DATABASE_ENGINE_ECHO,
617+ },
618+ )
619+
620+ except ImportError:
621+ return ComponentStatus(
622+ name="database",
623+ status=ComponentStatusType.UNHEALTHY,
624+ message="Database module not available",
625+ response_time_ms=None,
626+ metadata={
627+ "implementation": "sqlite",
628+ "error": "Database module not imported or configured",
629+ },
630+ )
631+ except Exception as e:
632+ # Check if it's a file not found error
633+ error_str = str(e).lower()
634+ if "unable to open database file" in error_str or "no such file" in error_str:
635+ return ComponentStatus(
636+ name="database",
637+ status=ComponentStatusType.WARNING,
638+ message="Database file not accessible",
639+ response_time_ms=None,
640+ metadata={
641+ "implementation": "sqlite",
642+ "url": settings.DATABASE_URL,
643+ "error": str(e),
644+ "recommendation": "Check database file path and permissions",
645+ },
646+ )
647+
648+ return ComponentStatus(
649+ name="database",
650+ status=ComponentStatusType.UNHEALTHY,
651+ message=f"Database connection failed: {str(e)}",
652+ response_time_ms=None,
653+ metadata={
654+ "implementation": "sqlite",
655+ "url": settings.DATABASE_URL,
656+ "error": str(e),
657+ },
658+ )
659+ {% endif %}
660+
661+
565662{% if cookiecutter .include_worker == "yes" %}
566663async def check_worker_health() -> ComponentStatus:
567664 """
0 commit comments