17 lines
944 B
Python
17 lines
944 B
Python
#!/usr/bin/env python3
|
|
"""Миграция с нуля и повторно, в изолированной схеме с ROLLBACK."""
|
|
from pathlib import Path
|
|
import subprocess
|
|
from uuid import uuid4
|
|
|
|
schema = 'audit_' + uuid4().hex
|
|
migration = Path('db/migrations/001_initial.sql').read_text()
|
|
sql = f'BEGIN; CREATE SCHEMA {schema}; SET LOCAL search_path TO {schema};\n'
|
|
sql += migration + '\n' + migration
|
|
sql += "\nSELECT count(*) = 5 AS five_tables FROM information_schema.tables WHERE table_schema = '" + schema + "';\nROLLBACK;\n"
|
|
result = subprocess.run(['docker', 'compose', 'exec', '-T', 'postgres', 'psql',
|
|
'-v', 'ON_ERROR_STOP=1', '-U', 'messenger', '-d', 'messenger', '-At'],
|
|
input=sql, text=True, capture_output=True, check=True)
|
|
assert 't' in result.stdout.splitlines(), result.stdout
|
|
print('PASS: migration creates five tables from zero and reapplies without errors; rolled back')
|