Files
cms-SiteForge/scripts/verify_pg_backup_restore.py
T
浅念 601451f22b upgrade: adopt V8.21.3 (V8.13→8.21.3: P5 SFTP/S3, P6 multilingual, editor overhaul, enterprise hardening)
Overlay ~150 upstream items onto our V8.12 base, preserving trade-pro
theme, registry tests, and design docs. Re-applied our patch set
(discovery/env_for/blocks.css/type-mapping/theme_color/frontend entries,
test_theme_pro_v3 mods) — all anchors survived upstream evolution.

New platform enhancement (fixes upgrade-introduced regression):
multilingual.site_locale now honors settings_json.locale, plus
PUT /api/i18n/sites/{id}/locale — standalone English sites get
<html lang='en'> under the V8.15 locale-rewrite pipeline.

compose: adopt V8.21.3 (image 8.21.3-dev.1, /api/ready probes, MySQL/S3
env) with project name pinned to siteforge-v8-10 (data volumes).

Tests: 356 passed / 11 failed — all 11 confirmed pre-existing in
pristine V8.21.3 on this host (10 upstream Windows env + our build test
now green after locale fix). Patch registry updated in plan doc.

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-10-23 12:31:03 +08:00

63 lines
2.9 KiB
Python

#!/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)