108 lines
3.9 KiB
Python
108 lines
3.9 KiB
Python
"""SQL migrations with a checksum ledger, then a runtime role without DDL."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
from pathlib import Path
|
|
|
|
import psycopg
|
|
from psycopg import ClientCursor, sql
|
|
|
|
from app.core.settings import Settings, get_settings
|
|
|
|
MIGRATIONS = Path(__file__).resolve().parent.parent.parent / "migrations"
|
|
|
|
|
|
def _checksum(path: Path) -> str:
|
|
return hashlib.sha256(path.read_bytes()).hexdigest()
|
|
|
|
|
|
def migrate(settings: Settings | None = None) -> list[str]:
|
|
settings = settings or get_settings()
|
|
applied: list[str] = []
|
|
with psycopg.connect(settings.dsn(), autocommit=True, cursor_factory=ClientCursor) as conn:
|
|
conn.execute(
|
|
"""
|
|
CREATE TABLE IF NOT EXISTS schema_migrations (
|
|
version text PRIMARY KEY,
|
|
checksum text NOT NULL,
|
|
applied_at timestamptz NOT NULL DEFAULT now()
|
|
)
|
|
"""
|
|
)
|
|
for path in sorted(MIGRATIONS.glob("*.sql")):
|
|
version = path.name
|
|
checksum = _checksum(path)
|
|
row = conn.execute(
|
|
"SELECT checksum FROM schema_migrations WHERE version = %s",
|
|
(version,),
|
|
).fetchone()
|
|
if row is not None:
|
|
if row[0] != checksum:
|
|
raise RuntimeError(f"checksum mismatch for migration {version}")
|
|
continue
|
|
with conn.transaction():
|
|
conn.execute(path.read_text(encoding="utf-8"))
|
|
conn.execute(
|
|
"INSERT INTO schema_migrations (version, checksum) VALUES (%s, %s)",
|
|
(version, checksum),
|
|
)
|
|
applied.append(version)
|
|
_ensure_runtime_role(conn, settings)
|
|
_prepare_source_storage(settings)
|
|
return applied
|
|
|
|
|
|
def _prepare_source_storage(settings: Settings) -> None:
|
|
from psycopg.rows import dict_row
|
|
|
|
from app.media.port import ensure_legacy_instance
|
|
from app.sourcebind import backfill_source_bindings
|
|
from app.sources import _install_sources
|
|
|
|
# The process that migrates has not imported the application yet.
|
|
# Without the modules, a legacy reference would stay unresolved.
|
|
_install_sources()
|
|
with psycopg.connect(settings.dsn(), row_factory=dict_row) as conn:
|
|
backfill_source_bindings(conn)
|
|
ensure_legacy_instance(conn, settings.media_root)
|
|
conn.commit()
|
|
|
|
|
|
def _ensure_runtime_role(conn: psycopg.Connection, settings: Settings) -> None:
|
|
role = sql.Identifier(settings.db_runtime_user)
|
|
database = sql.Identifier(settings.db_name)
|
|
owner = sql.Identifier(settings.db_user)
|
|
password = sql.Literal(settings.db_password)
|
|
exists = conn.execute(
|
|
"SELECT 1 FROM pg_roles WHERE rolname = %s",
|
|
(settings.db_runtime_user,),
|
|
).fetchone()
|
|
if exists is None:
|
|
conn.execute(
|
|
sql.SQL(
|
|
"CREATE ROLE {} LOGIN NOSUPERUSER NOCREATEDB NOCREATEROLE NOINHERIT PASSWORD {}"
|
|
).format(role, password)
|
|
)
|
|
else:
|
|
conn.execute(
|
|
sql.SQL(
|
|
"ALTER ROLE {} WITH LOGIN NOSUPERUSER NOCREATEDB NOCREATEROLE NOINHERIT PASSWORD {}"
|
|
).format(role, password)
|
|
)
|
|
conn.execute(sql.SQL("GRANT CONNECT ON DATABASE {} TO {}").format(database, role))
|
|
conn.execute(sql.SQL("GRANT USAGE ON SCHEMA public TO {}").format(role))
|
|
conn.execute(sql.SQL("REVOKE CREATE ON SCHEMA public FROM {}").format(role))
|
|
conn.execute(
|
|
sql.SQL(
|
|
"GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO {}"
|
|
).format(role)
|
|
)
|
|
conn.execute(sql.SQL("REVOKE ALL ON TABLE schema_migrations FROM {}").format(role))
|
|
conn.execute(
|
|
sql.SQL(
|
|
"ALTER DEFAULT PRIVILEGES FOR ROLE {} IN SCHEMA public "
|
|
"GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO {}"
|
|
).format(owner, role)
|
|
)
|