buildable docker compose

This commit is contained in:
2026-06-28 13:23:00 -07:00
parent de70005b39
commit 621e8bd7b6
3 changed files with 27 additions and 3 deletions
+24
View File
@@ -13,6 +13,29 @@ from app.user_models import User
from app.api import events, auth, station_config, history, team, community, shows, storage, underwriters, stats
def _cleanup_orphaned_sequences(sync_conn):
"""Drop model sequences whose backing table no longer exists.
Prevents 'duplicate key value violates unique constraint' errors when
create_all() tries to recreate a SERIAL-backed sequence for a table
whose original sequence was left behind after a drop/recreate cycle.
Only runs on PostgreSQL; no-op for SQLite.
"""
import sqlalchemy as sa
if sync_conn.dialect.name != "postgresql":
return
inspector = sa.inspect(sync_conn)
existing_tables = set(inspector.get_table_names())
for table in Base.metadata.tables.values():
expected_seq = f"{table.name}_id_seq"
if table.name not in existing_tables:
sync_conn.execute(sa.text(f"DROP SEQUENCE IF EXISTS {expected_seq}"))
print(f" → Dropped orphaned sequence {expected_seq}")
def _migrate_add_missing_columns(sync_conn):
"""Add missing columns to existing tables (startup migration — no Alembic).
@@ -82,6 +105,7 @@ async def _ensure_station_config(session):
async def lifespan(app: FastAPI):
# Create tables on startup, then patch any missing columns
async with engine.begin() as conn:
await conn.run_sync(_cleanup_orphaned_sequences)
await conn.run_sync(Base.metadata.create_all)
await conn.run_sync(_migrate_add_missing_columns)