new media player functionality
This commit is contained in:
+41
-1
@@ -11,6 +11,45 @@ from app.user_models import User
|
||||
from app.api import events, auth, station_config, history, team, community, shows, storage, underwriters
|
||||
|
||||
|
||||
def _migrate_add_missing_columns(sync_conn):
|
||||
"""Add missing columns to existing tables (startup migration — no Alembic).
|
||||
|
||||
Handles both SQLite and PostgreSQL. Idempotent: skips columns that already exist.
|
||||
Receives a sync SQLAlchemy Connection from engine.run_sync().
|
||||
"""
|
||||
import sqlalchemy as sa
|
||||
|
||||
# Column definitions: (table, name, type, default)
|
||||
pending = [
|
||||
("station_config", "stream_url", sa.String(500), ""),
|
||||
("station_config", "stream_metadata_url", sa.String(500), ""),
|
||||
]
|
||||
|
||||
# Determine dialect
|
||||
dialect = sync_conn.dialect.name # "sqlite" | "postgresql"
|
||||
|
||||
inspector = sa.inspect(sync_conn)
|
||||
|
||||
for table_name, col_name, col_type, default in pending:
|
||||
# Check if column already exists
|
||||
existing = inspector.get_columns(table_name)
|
||||
if any(c["name"] == col_name for c in existing):
|
||||
continue
|
||||
|
||||
print(f" → Migrating: adding column {table_name}.{col_name}")
|
||||
|
||||
if dialect == "sqlite":
|
||||
sync_conn.execute(
|
||||
sa.text(f'ALTER TABLE {table_name} ADD COLUMN "{col_name}" TEXT NOT NULL DEFAULT "{default}"')
|
||||
)
|
||||
else:
|
||||
sync_conn.execute(
|
||||
sa.text(f'ALTER TABLE {table_name} ADD COLUMN "{col_name}" VARCHAR(500) NOT NULL DEFAULT "{default}"')
|
||||
)
|
||||
|
||||
print(f" ✓ Added {table_name}.{col_name}")
|
||||
|
||||
|
||||
async def _ensure_station_config(session):
|
||||
"""Ensure the singleton station config row exists (idempotent)."""
|
||||
result = await session.execute(
|
||||
@@ -28,9 +67,10 @@ async def _ensure_station_config(session):
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
# Create tables on startup
|
||||
# Create tables on startup, then patch any missing columns
|
||||
async with engine.begin() as conn:
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
await conn.run_sync(_migrate_add_missing_columns)
|
||||
|
||||
# Auto-seed if database is empty
|
||||
async with async_session() as session:
|
||||
|
||||
Reference in New Issue
Block a user