forked from mtusi2026/mtusi2026lab6
61 lines
2.1 KiB
Python
61 lines
2.1 KiB
Python
"""Версионируемые миграции."""
|
|
import hashlib
|
|
import logging
|
|
import sys
|
|
import time
|
|
from pathlib import Path
|
|
|
|
import psycopg
|
|
|
|
from . import config
|
|
|
|
LOCK_ID = 727_002_001
|
|
log = logging.getLogger("migrate")
|
|
|
|
|
|
def migration_files():
|
|
return sorted((Path(__file__).parent / "migrations").glob("[0-9][0-9][0-9][0-9]_*.sql"))
|
|
|
|
|
|
def apply_all(conninfo):
|
|
files = migration_files()
|
|
with psycopg.connect(conninfo, connect_timeout=3) as conn:
|
|
conn.execute("SELECT pg_advisory_xact_lock(%s)", (LOCK_ID,))
|
|
conn.execute("""CREATE TABLE IF NOT EXISTS schema_migrations (
|
|
version text PRIMARY KEY,
|
|
checksum text NOT NULL,
|
|
applied_at timestamptz NOT NULL DEFAULT now())""")
|
|
applied = dict(conn.execute("SELECT version, checksum FROM schema_migrations").fetchall())
|
|
done = []
|
|
for path in files:
|
|
sql = path.read_text(encoding="utf-8")
|
|
checksum = hashlib.sha256(sql.encode()).hexdigest()
|
|
if path.stem in applied:
|
|
if applied[path.stem] != checksum:
|
|
raise RuntimeError("migration %s was modified after being applied" % path.stem)
|
|
continue
|
|
conn.execute(sql)
|
|
conn.execute("INSERT INTO schema_migrations (version, checksum) VALUES (%s, %s)", (path.stem, checksum))
|
|
done.append(path.stem)
|
|
return done
|
|
|
|
|
|
def main():
|
|
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(name)s %(message)s")
|
|
if not config.DATABASE_URL:
|
|
sys.exit("DATABASE_URL(_FILE) is not configured")
|
|
deadline = time.monotonic() + 60
|
|
while True:
|
|
try:
|
|
done = apply_all(config.DATABASE_URL)
|
|
break
|
|
except psycopg.OperationalError as exc:
|
|
if time.monotonic() > deadline:
|
|
raise
|
|
log.info("database not ready (%s), retrying", type(exc).__name__)
|
|
time.sleep(1)
|
|
log.info("applied: %s", ", ".join(done) if done else "nothing to do (schema is up to date)")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |