fix(backup): refuse another schema before reading the dump; tests read settings directly (#5815)
- restore_in_place checks the backup's migration heads before pg_restore writes anything, so a backup from another schema is refused at once. - The integration tests read their own setting rows directly: get_all_settings knows only Steward's own keys. - check.py: drop an import left unused by the move to pgdata. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -35,7 +35,7 @@ from sqlalchemy.ext.asyncio import create_async_engine
|
||||
|
||||
from .contents import (ACTIVITY, ACTIVITY_EVERYWHERE, AREAS, EXCLUDED, HAND_SET_ONLY,
|
||||
STATE_SETTINGS)
|
||||
from .dump import BackupError, read_backup
|
||||
from .dump import read_backup
|
||||
from .pgdata import async_url as _async_url
|
||||
from .pgdata import copy_rows, restore_script, script_tables
|
||||
|
||||
|
||||
@@ -321,17 +321,20 @@ async def restore_in_place(path: Path, database_url: str, *,
|
||||
from .contents import ACTIVITY, STATE_SETTINGS
|
||||
from .pgdata import restore_script, script_blocks
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
info, dump, key = await asyncio.to_thread(read_backup, path, Path(tmp))
|
||||
blocks = script_blocks(await restore_script(dump))
|
||||
engine = create_async_engine(_async_url(database_url))
|
||||
try:
|
||||
async with engine.connect() as conn:
|
||||
heads = sorted((await conn.execute(text("SELECT version_num FROM alembic_version"))).scalars())
|
||||
# The schema first: a backup that can't go in place is refused before
|
||||
# anything else is read.
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
info, dump, key = await asyncio.to_thread(read_backup, path, Path(tmp))
|
||||
async with engine.connect() as conn:
|
||||
heads = sorted((await conn.execute(text("SELECT version_num FROM alembic_version"))).scalars())
|
||||
backup_heads = sorted(info.get("migration_heads") or [])
|
||||
schema = schema_of(backup_heads, heads, known_revisions(plugin_dirs))
|
||||
if schema != "same":
|
||||
raise NotSameSchema(schema, backup_heads, heads)
|
||||
blocks = script_blocks(await restore_script(dump))
|
||||
async with engine.connect() as conn:
|
||||
present = set((await conn.execute(text(
|
||||
"SELECT tablename FROM pg_tables WHERE schemaname = 'public'"))).scalars())
|
||||
tables = sorted(t for t in blocks if t not in EXCLUDED and t in present)
|
||||
|
||||
Reference in New Issue
Block a user