from sqlalchemy import inspect, text from sqlalchemy.engine import Engine def migrate_schema(engine: Engine) -> None: """Apply lightweight schema updates for existing deployments.""" inspector = inspect(engine) tables = set(inspector.get_table_names()) statements: list[str] = [] if "parse_jobs" in tables: columns = {col["name"] for col in inspector.get_columns("parse_jobs")} if "interval_seconds" not in columns: statements.append( "ALTER TABLE parse_jobs ADD COLUMN interval_seconds INTEGER NOT NULL DEFAULT 3600" ) if "is_active" not in columns: statements.append( "ALTER TABLE parse_jobs ADD COLUMN is_active BOOLEAN NOT NULL DEFAULT TRUE" ) if "profile_id" not in columns: statements.append("ALTER TABLE parse_jobs ADD COLUMN profile_id INTEGER") if "channel_id" not in columns: statements.append("ALTER TABLE parse_jobs ADD COLUMN channel_id INTEGER") # create_all handles new tables; FKs on existing DBs may need indexes if "parse_jobs" in tables: # Re-inspect after potential adds is not needed for FK constraints here — # create_all + nullable FKs are enough for MVP; optional constraints below. pass if not statements: return with engine.begin() as conn: for stmt in statements: conn.execute(text(stmt))