Database migrations incorrectly ran before the tables where created. This didn't cause problems when the database was already set up but would absolutely bork a fresh install.

This commit is contained in:
Jonas Linter
2025-10-20 08:39:26 +02:00
parent b0cb4e555c
commit 7bcbe70392
2 changed files with 8 additions and 7 deletions

View File

@@ -275,17 +275,18 @@ async def lifespan(app: FastAPI):
elif hotel_id and not push_endpoint:
_LOGGER.info("Hotel %s has no push_endpoint configured", hotel_id)
# Run database migrations first (only primary worker to avoid race conditions)
# Create tables first (all workers)
# This ensures tables exist before migrations try to alter them
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
_LOGGER.info("Database tables checked/created at startup.")
# Run migrations after tables exist (only primary worker for race conditions)
if is_primary:
await run_all_migrations(engine)
else:
_LOGGER.info("Skipping migrations (non-primary worker)")
# Create tables (all workers)
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
_LOGGER.info("Database tables checked/created at startup.")
# Hash any existing customers (only in primary worker to avoid race conditions)
if is_primary:
async with AsyncSessionLocal() as session: