113 lines
3.4 KiB
Python
113 lines
3.4 KiB
Python
"""Однократные миграции. Запускается отдельным контейнером, не каждой репликой API."""
|
|
import logging
|
|
import os
|
|
import sys
|
|
import time
|
|
from pathlib import Path
|
|
|
|
import psycopg
|
|
from psycopg.rows import dict_row
|
|
|
|
LOGGER = logging.getLogger("migrate")
|
|
MIGRATION_LOCK = 421003
|
|
CONNECT_TIMEOUT = 3
|
|
|
|
|
|
def read_secret_file(path):
|
|
with open(path, encoding="utf-8") as handle:
|
|
return handle.read().strip()
|
|
|
|
|
|
def database_url():
|
|
path = os.environ.get("DATABASE_URL_FILE")
|
|
if path:
|
|
return read_secret_file(path)
|
|
url = os.environ.get("DATABASE_URL", "").strip()
|
|
if not url:
|
|
raise RuntimeError("DATABASE_URL_FILE")
|
|
return url
|
|
|
|
|
|
def split_sql(script):
|
|
parts = []
|
|
buf = []
|
|
for line in script.splitlines():
|
|
if line.strip().startswith("--"):
|
|
continue
|
|
buf.append(line)
|
|
if line.strip().endswith(";"):
|
|
statement = "\n".join(buf).strip()
|
|
if statement.endswith(";"):
|
|
statement = statement[:-1].strip()
|
|
if statement:
|
|
parts.append(statement)
|
|
buf = []
|
|
tail = "\n".join(buf).strip().rstrip(";").strip()
|
|
if tail:
|
|
parts.append(tail)
|
|
return parts
|
|
|
|
|
|
def wait_postgres(conninfo):
|
|
last = None
|
|
for _ in range(60):
|
|
try:
|
|
with psycopg.connect(conninfo, connect_timeout=CONNECT_TIMEOUT) as conn:
|
|
conn.execute("SELECT 1")
|
|
return
|
|
except psycopg.OperationalError as exc:
|
|
last = type(exc).__name__
|
|
time.sleep(1)
|
|
LOGGER.error("postgres not ready: %s", last)
|
|
raise SystemExit(1)
|
|
|
|
|
|
def apply(conninfo):
|
|
directory = Path(__file__).resolve().parents[1] / "migrations"
|
|
with psycopg.connect(conninfo, connect_timeout=CONNECT_TIMEOUT, row_factory=dict_row) as conn:
|
|
conn.execute("SELECT pg_advisory_lock(%s)", (MIGRATION_LOCK,))
|
|
conn.commit()
|
|
try:
|
|
conn.execute(
|
|
"""
|
|
CREATE TABLE IF NOT EXISTS schema_migrations (
|
|
version text PRIMARY KEY,
|
|
applied_at timestamptz NOT NULL DEFAULT now()
|
|
)
|
|
"""
|
|
)
|
|
conn.commit()
|
|
for path in sorted(directory.glob("*.sql")):
|
|
version = path.stem
|
|
found = conn.execute(
|
|
"SELECT 1 FROM schema_migrations WHERE version = %s",
|
|
(version,),
|
|
).fetchone()
|
|
conn.rollback()
|
|
if found:
|
|
LOGGER.info("migration %s already applied", version)
|
|
continue
|
|
with conn.transaction():
|
|
for statement in split_sql(path.read_text(encoding="utf-8")):
|
|
conn.execute(statement)
|
|
conn.execute(
|
|
"INSERT INTO schema_migrations (version) VALUES (%s)",
|
|
(version,),
|
|
)
|
|
LOGGER.info("migration %s applied", version)
|
|
finally:
|
|
conn.execute("SELECT pg_advisory_unlock(%s)", (MIGRATION_LOCK,))
|
|
conn.commit()
|
|
|
|
|
|
def main():
|
|
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s %(message)s")
|
|
conninfo = database_url()
|
|
wait_postgres(conninfo)
|
|
apply(conninfo)
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|