add auto migrations
This commit is contained in:
parent
c94f7baa88
commit
d33b242a84
|
|
@ -54,3 +54,28 @@ async def init_db() -> None:
|
|||
"""Initialize database tables."""
|
||||
async with engine.begin() as conn:
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
|
||||
|
||||
def run_migrations() -> None:
|
||||
"""Run Alembic migrations."""
|
||||
from pathlib import Path
|
||||
|
||||
from alembic import command
|
||||
from alembic.config import Config
|
||||
from loguru import logger
|
||||
|
||||
# Find alembic.ini (works in both dev and Docker)
|
||||
alembic_ini_path = Path(__file__).parent.parent.parent.parent / "alembic.ini"
|
||||
|
||||
if not alembic_ini_path.exists():
|
||||
logger.warning(f"alembic.ini not found at {alembic_ini_path}, skipping migrations")
|
||||
return
|
||||
|
||||
# Create Alembic config
|
||||
alembic_cfg = Config(str(alembic_ini_path))
|
||||
alembic_cfg.set_main_option("sqlalchemy.url", settings.database_url)
|
||||
|
||||
# Run migrations
|
||||
logger.info("Running Alembic migrations...")
|
||||
command.upgrade(alembic_cfg, "head")
|
||||
logger.info("Migrations completed successfully")
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ from fastapi.middleware.cors import CORSMiddleware
|
|||
|
||||
from app.api.v1 import assets, auth, batch, folders, shares, uploads
|
||||
from app.infra.config import get_settings
|
||||
from app.infra.database import init_db
|
||||
from app.infra.database import init_db, run_migrations
|
||||
|
||||
settings = get_settings()
|
||||
|
||||
|
|
@ -15,7 +15,8 @@ settings = get_settings()
|
|||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
"""Application lifespan handler."""
|
||||
# Startup
|
||||
# Startup: Run migrations first
|
||||
run_migrations()
|
||||
await init_db()
|
||||
yield
|
||||
# Shutdown
|
||||
|
|
|
|||
Loading…
Reference in New Issue