forked from mtusi2026/mtusi2026lab2
16 lines
729 B
Python
16 lines
729 B
Python
import os
|
|
from pathlib import Path
|
|
import psycopg
|
|
|
|
url = os.environ["DATABASE_URL"]
|
|
with psycopg.connect(url, connect_timeout=5) as conn:
|
|
conn.execute("CREATE TABLE IF NOT EXISTS schema_migrations (version TEXT PRIMARY KEY, applied_at TIMESTAMPTZ NOT NULL DEFAULT now())")
|
|
for path in sorted((Path(__file__).resolve().parents[1] / "migrations").glob("*.sql")):
|
|
version = path.name
|
|
if conn.execute("SELECT 1 FROM schema_migrations WHERE version=%s", (version,)).fetchone():
|
|
continue
|
|
with conn.transaction():
|
|
conn.execute(path.read_text(encoding="utf-8"))
|
|
conn.execute("INSERT INTO schema_migrations(version) VALUES (%s)", (version,))
|
|
print("Migrations applied")
|