#!/usr/bin/env python3 """REAL pg_dump -> checksum -> pg_restore into dedicated empty DB CI smoke. Must only run in isolated disposable CI PG server as administrator and with SITEFORGE_ALLOW_PG_DR_SMOKE=1. Do NOT point at any production database. """ from __future__ import annotations # Allow direct `python scripts/name.py` from a fresh source checkout. import sys as _sys from pathlib import Path as _Path _sys.path.insert(0, str(_Path(__file__).resolve().parents[1])) import os import re import tempfile import uuid from pathlib import Path from urllib.parse import urlsplit, urlunsplit from app import db, pg_compat, pg_disaster_recovery as disaster src_url = os.environ.get('SITEFORGE_DATABASE_URL','') parsed=urlsplit(src_url) src_name=parsed.path.lstrip('/') if (os.environ.get('SITEFORGE_ALLOW_PG_DR_SMOKE')!='1' or not db.POSTGRES or not re.fullmatch('[a-z][a-z0-9_]*_ci',src_name)): raise SystemExit('Requires isolated PG _ci database and explicit DR-smoke flag') dest_name=src_name[:-3]+'_restore_ci' dest_url=urlunsplit((parsed.scheme,parsed.netloc,'/'+dest_name,parsed.query,parsed.fragment)) env=disaster.connection_env(src_url) marker='restore-check-'+uuid.uuid4().hex[:8] created=False try: with db.connect() as conn: conn.execute('INSERT INTO sites(id,name,created_at,updated_at) VALUES (?,?,?,?)', (marker,'Backup Restore Sample',db.now(),db.now())) # pg_dump is a snapshot, but cross-volume coherence still requires no writers; # CI runs in isolation and uses a private data directory. with tempfile.TemporaryDirectory(prefix='sf-v818-pg-dr-') as root: root=Path(root) data=root/'original';asset=data/'media'/marker/'file.txt' asset.parent.mkdir(parents=True) asset.write_text('SiteForge backup fixture',encoding='utf-8') archive=root/'backup.zip' disaster.backup(src_url,data,archive,confirmed_offline=True) assert disaster.verify(archive)['files']['database.dump']['size']>0 # Admin must have CREATE DATABASE in this isolated CI service. disaster.run_pg(['psql','--no-psqlrc','--command',f'CREATE DATABASE {dest_name}'],env,40) created=True new_storage=root/'restored' disaster.restore_new(dest_url,archive,new_storage,confirmed_offline=True) assert (new_storage/'media'/marker/'file.txt').read_text(encoding='utf-8')=='SiteForge backup fixture' with pg_compat.connect(dest_url) as conn: row=conn.execute('SELECT name FROM sites WHERE id=?',(marker,)).fetchone() assert row and row['name']=='Backup Restore Sample' print('PASS: Real PG dump, manifest integrity, restore to empty DB and media recovery') finally: with db.connect() as conn: conn.execute('DELETE FROM sites WHERE id=?',(marker,)) if created: disaster.run_pg(['psql','--no-psqlrc','--command',f'DROP DATABASE {dest_name} WITH (FORCE)'],env,40)