forked from mtusi2026/mtusi2026lab5
19 lines
801 B
Python
19 lines
801 B
Python
import os
|
|
from pathlib import Path
|
|
import psycopg
|
|
|
|
def setting(name):
|
|
file = os.getenv(name + '_FILE')
|
|
return Path(file).read_text().strip() if file else os.environ[name]
|
|
|
|
def migrate():
|
|
with psycopg.connect(setting('DATABASE_URL'), connect_timeout=3) as c:
|
|
c.execute('SELECT pg_advisory_xact_lock(202602)')
|
|
c.execute('CREATE TABLE IF NOT EXISTS schema_migrations(version text PRIMARY KEY, applied_at timestamptz NOT NULL DEFAULT now())')
|
|
for p in sorted(Path('migrations').glob('*.sql')):
|
|
if not c.execute('SELECT 1 FROM schema_migrations WHERE version=%s',(p.name,)).fetchone():
|
|
c.execute(p.read_text())
|
|
c.execute('INSERT INTO schema_migrations(version) VALUES (%s)',(p.name,))
|
|
if __name__ == '__main__':
|
|
migrate()
|