@@ -39,68 +39,67 @@ def _configure_logging() -> None:
3939@asynccontextmanager
4040async def lifespan (app : FastAPI ):
4141 """Handle application startup and shutdown."""
42+ import asyncio
43+
4244 # Startup: Test database connection
4345 logger .info ("Testing database connection..." )
4446 try :
45- with SessionLocal () as session :
46- session .execute (text ("SELECT 1" ))
47- session .commit ()
48- logger .info ("Database connection successful" )
49- except Exception as e :
50- logger .error (f"Database connection failed: { e } " , exc_info = True )
51- # Don't fail startup, but log the error
52-
53- # Run database migrations
54- logger .info ("Running database migrations..." )
55- try :
56- from pathlib import Path
57- from alembic .config import Config
58- from alembic import command
59-
60- project_root = Path (__file__ ).parent .parent
61- alembic_ini = project_root / "alembic.ini"
62-
63- if not alembic_ini .exists ():
64- logger .error (f"alembic.ini not found at { alembic_ini } " )
65- else :
66- logger .info (f"Found alembic.ini at { alembic_ini } " )
67- alembic_cfg = Config (str (alembic_ini ))
68-
69- # Run migrations
70- command .upgrade (alembic_cfg , "head" )
71- logger .info ("Database migrations completed successfully" )
47+ def test_connection ():
48+ with SessionLocal () as session :
49+ session .execute (text ("SELECT 1" ))
50+ session .commit ()
51+
52+ # Run in executor to avoid blocking
53+ loop = asyncio .get_event_loop ()
54+ await asyncio .wait_for (
55+ loop .run_in_executor (None , test_connection ),
56+ timeout = 5.0
57+ )
58+ logger .info ("✅ Database connection successful" )
59+ except asyncio .TimeoutError :
60+ logger .error ("❌ Database connection timeout after 5 seconds" )
7261 except Exception as e :
73- logger .error (f"Database migration failed: { e } " , exc_info = True )
74- # Don't fail startup, but log the error
62+ logger .error (f"❌ Database connection failed: { e } " , exc_info = True )
7563
7664 # Verify schema: Check if required columns exist
65+ # Note: Migrations are run in pre-deploy script, not here
7766 logger .info ("Verifying database schema..." )
7867 try :
79- with SessionLocal () as session :
80- # Check if primary_language column exists
81- result = session .execute (
82- text (
83- """
84- SELECT column_name
85- FROM information_schema.columns
86- WHERE table_name = 'generated_roadmaps'
87- AND column_name = 'primary_language'
88- """
68+ def verify_schema ():
69+ with SessionLocal () as session :
70+ # Check if primary_language column exists
71+ result = session .execute (
72+ text (
73+ """
74+ SELECT column_name
75+ FROM information_schema.columns
76+ WHERE table_name = 'generated_roadmaps'
77+ AND column_name = 'primary_language'
78+ """
79+ )
8980 )
81+ return result .fetchone () is not None
82+
83+ # Run in executor to avoid blocking
84+ loop = asyncio .get_event_loop ()
85+ column_exists = await asyncio .wait_for (
86+ loop .run_in_executor (None , verify_schema ),
87+ timeout = 5.0
88+ )
89+
90+ if column_exists :
91+ logger .info ("✅ Database schema verified - all required columns exist" )
92+ else :
93+ logger .warning (
94+ "⚠️ Required columns missing. Pre-deploy script should have fixed this. "
95+ "If this persists, check pre-deploy command execution."
9096 )
91- column_exists = result .fetchone () is not None
92-
93- if column_exists :
94- logger .info ("Database schema verified successfully - all required columns exist" )
95- else :
96- logger .error (
97- "Required columns still missing after migration attempt. "
98- "Please check migration files and database connection."
99- )
97+ except asyncio .TimeoutError :
98+ logger .error ("❌ Schema verification timeout after 5 seconds" )
10099 except Exception as e :
101- logger .error (f"Schema verification failed: { e } " , exc_info = True )
102- # Don't fail startup, but log the error
100+ logger .error (f"❌ Schema verification failed: { e } " , exc_info = True )
103101
102+ logger .info ("🚀 Application startup complete" )
104103 yield
105104 # Shutdown
106105 logger .info ("Application shutting down" )
0 commit comments