mirror of
https://github.com/Xe138/AI-Trader.git
synced 2026-04-01 17:17:24 -04:00
refactor: move database initialization from entrypoint to application
Move database initialization logic from shell script to Python application lifespan, following separation of concerns and improving maintainability. Benefits: - Single source of truth for database initialization (api/main.py lifespan) - Better testability - Python code vs shell scripts - Clearer logging with structured messages - Easier to debug and maintain - Infrastructure (entrypoint.sh) focuses on service orchestration - Application (api/main.py) owns its data layer Changes: - Removed database init from entrypoint.sh - Enhanced lifespan function with detailed logging - Simplified entrypoint script (now 4 steps instead of 5) - All tests pass (28/28 API endpoint tests)
This commit is contained in:
10
api/main.py
10
api/main.py
@@ -135,19 +135,27 @@ def create_app(
|
||||
from api.database import initialize_dev_database, initialize_database
|
||||
|
||||
# Startup - use closure to access db_path from create_app scope
|
||||
logger.info("🚀 FastAPI application starting...")
|
||||
logger.info("📊 Initializing database...")
|
||||
|
||||
if is_dev_mode():
|
||||
# Initialize dev database (reset unless PRESERVE_DEV_DATA=true)
|
||||
logger.info(" 🔧 DEV mode detected - initializing dev database")
|
||||
dev_db_path = get_db_path(db_path)
|
||||
initialize_dev_database(dev_db_path)
|
||||
log_dev_mode_startup_warning()
|
||||
else:
|
||||
# Ensure production database schema exists
|
||||
logger.info(" 🏭 PROD mode - ensuring database schema exists")
|
||||
initialize_database(db_path)
|
||||
|
||||
logger.info("✅ Database initialized")
|
||||
logger.info("🌐 API server ready to accept requests")
|
||||
|
||||
yield
|
||||
|
||||
# Shutdown (if needed in future)
|
||||
pass
|
||||
logger.info("🛑 FastAPI application shutting down...")
|
||||
|
||||
app = FastAPI(
|
||||
title="AI-Trader Simulation API",
|
||||
|
||||
Reference in New Issue
Block a user