import logging from dataclasses import dataclass from typing import Callable, List, Set from sqlalchemy import inspect, text from sqlalchemy.engine import Connection @dataclass(frozen=True) class Migration: id: str description: str upgrade: Callable[[Connection], None] def _ensure_migrations_table(connection: Connection) -> None: connection.execute( text( """ CREATE TABLE IF NOT EXISTS schema_migrations ( id VARCHAR(255) PRIMARY KEY, applied_at TIMESTAMPTZ NOT NULL DEFAULT NOW() ) """ ) ) def _migration_0001_add_channel_subscription_fields(connection: Connection) -> None: inspector = inspect(connection) columns: Set[str] = {col["name"] for col in inspector.get_columns("users")} statements: List[str] = [] if "channel_subscription_verified" not in columns: statements.append( "ALTER TABLE users ADD COLUMN channel_subscription_verified BOOLEAN" ) if "channel_subscription_checked_at" not in columns: statements.append( "ALTER TABLE users ADD COLUMN channel_subscription_checked_at TIMESTAMPTZ" ) if "channel_subscription_verified_for" not in columns: statements.append( "ALTER TABLE users ADD COLUMN channel_subscription_verified_for BIGINT" ) for stmt in statements: connection.execute(text(stmt)) MIGRATIONS: List[Migration] = [ Migration( id="0001_add_channel_subscription_fields", description="Add columns to track required channel subscription verification", upgrade=_migration_0001_add_channel_subscription_fields, ), ] def run_database_migrations(connection: Connection) -> None: """ Apply pending migrations sequentially. Already applied revisions are skipped. """ _ensure_migrations_table(connection) applied_revisions: Set[str] = { row[0] for row in connection.execute( text("SELECT id FROM schema_migrations") ) } for migration in MIGRATIONS: if migration.id in applied_revisions: continue logging.info( "Migrator: applying %s – %s", migration.id, migration.description ) try: with connection.begin_nested(): migration.upgrade(connection) connection.execute( text( "INSERT INTO schema_migrations (id) VALUES (:revision)" ), {"revision": migration.id}, ) except Exception as exc: logging.error( "Migrator: failed to apply %s (%s)", migration.id, migration.description, exc_info=True, ) raise exc else: logging.info("Migrator: migration %s applied successfully", migration.id)