Files
compras/scripts/prove-client-registry-recovery.py
T
nala 47649bd46c
tests / ci (push) Canceled after 0s
commit compras1 3011
2026-11-30 09:33:36 +02:00

45 lines
2.5 KiB
Python

"""Reproduce and prove the corrected migration isolation without writing the active database."""
from pathlib import Path
import json
import subprocess
import uuid
root = Path('/var/www/compras-deploy/current').resolve()
assert root == Path('/var/www/compras-deploy/releases/presentation-20261129')
dump = Path('/var/backups/compras-presentation-20261129/client-hiring/database.dump')
restore = 'compras_client_restore_'+uuid.uuid4().hex[:12]
def php(*args):
result = subprocess.run(['runuser', '-u', 'www-data', '--', 'php', 'scripts/upgrade-client-registry.php', *args], cwd=root, capture_output=True)
if result.returncode:
diagnostic = Path('/var/backups/compras-presentation-20261129/client-registry-isolation-failure.private.log')
diagnostic.write_bytes(result.stdout+result.stderr)
diagnostic.chmod(0o600)
raise RuntimeError('Native verification failed; see private diagnostic.')
return json.loads(result.stdout)
before = php('--inspect-client-registry')
created = False
try:
subprocess.run(['runuser', '-u', 'postgres', '--', 'createdb', '--owner=compras_demo_app_20261129', restore], check=True)
created = True
with dump.open('rb') as stream:
subprocess.run(['runuser', '-u', 'postgres', '--', 'pg_restore', '--exit-on-error', '-d', restore], stdin=stream, check=True)
initial = php('--inspect-client-registry', restore)
assert initial['database'] == restore and not initial['exists'] and not initial['migration_recorded']
result = php(restore)
recovered = php('--inspect-client-registry', restore)
assert recovered['database'] == restore and recovered['exists'] and recovered['migration_recorded'] and recovered['client_count'] == 0
after = php('--inspect-client-registry')
assert after == before, 'Active registry must remain identical during recovery migration'
assert recovered['schema_sha256'] == before['schema_sha256'], 'Active empty structure must match the native migration exactly'
finally:
if created:
subprocess.run(['runuser', '-u', 'postgres', '--', 'dropdb', restore], check=True)
proof = {'state': 'passed', 'migration_connection_name': 'registry_upgrade',
'restore_initially_without_registry': initial, 'recovered_registry': recovered,
'active_registry_unchanged': before == after, 'active_empty_schema_matches_native_migration': True,
'recovery_database_removed': True, 'recovery_upgrade': result}
Path('/var/backups/compras-presentation-20261129/client-registry-isolation.json').write_text(json.dumps(proof, indent=2)+'\n')
print(json.dumps(proof))