completed lab4
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
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()
|
||||
Reference in New Issue
Block a user