#!/usr/bin/env python3"""A schema rise's preservation proof: compare a file before and after its in-place move.Usage: scripts/dev/rise-preservation.py <before.db> <after.db>Both files are opened read-only. For every table of BEFORE (search shadow tablesand sqlite_ internals aside) it asserts that AFTER still has the table, everycolumn it had, and every row under the same rowid with every original cellbyte-equal (but _meta.schema_version, which the move restamps), including thereceipts in _writes and the queued _outbox. It thenlists what AFTER added: new tables (with their row counts) and new columns (withthe distinct values the old rows now hold, which must be the declared default).Last, PRAGMA integrity_check and foreign_key_check on AFTER, and its user_version.Exit 0 with a PASS line when every old cell survived; nonzero, naming the firstdifference of each table, otherwise."""import sqlite3import sysdef open_ro(path):return sqlite3.connect(f"file:{path}?mode=ro&immutable=1", uri=True)def tables(db):return [r[0] for r in db.execute("SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ""AND substr(name,1,4) != 'fts_' ORDER BY name")]def columns(db, table):return [r[1] for r in db.execute(f'PRAGMA table_info("{table}")')]def has_rowid(db, table): sql = db.execute("SELECT sql FROM sqlite_master WHERE name=?", (table,)).fetchone()[0] or""return"WITHOUT ROWID"notin sql.upper()def main(before_path, after_path): before, after = open_ro(before_path), open_ro(after_path) failures, kept_cells = [], 0 after_tables = set(tables(after))for table in tables(before):if table notin after_tables: failures.append(f"{table}: table is gone")continue old_cols, new_cols = columns(before, table), columns(after, table) missing = [c for c in old_cols if c notin new_cols]if missing: failures.append(f"{table}: columns gone {missing}")continue# The one cell a move must change: the file's own schema stamp. compared = [c for c in old_cols ifnot (table == "_meta"and c == "schema_version")] quoted = ",".join(f'"{c}"'for c in compared) key = "rowid,"if has_rowid(before, table) else"" order = "rowid"if key else",".join(f'"{c}"'for c in old_cols) query = f'SELECT {key}{quoted} FROM "{table}" ORDER BY {order}' old_rows, new_rows = before.execute(query).fetchall(), after.execute(query).fetchall()if old_rows != new_rows: first = next((i for i, (a, b) in enumerate(zip(old_rows, new_rows)) if a != b), min(len(old_rows), len(new_rows))) failures.append(f"{table}: rows differ ({len(old_rows)} → {len(new_rows)}; first at {first})")continue kept_cells += len(old_rows) * len(compared) added = [c for c in new_cols if c notin old_cols]for column in added: values = [r[0] for r in after.execute(f'SELECT DISTINCT "{column}" FROM "{table}" LIMIT 3')] print(f"added column {table}.{column}: old rows hold {values if old_rows else '(no rows)'}")for table in sorted(after_tables - set(tables(before))): count = after.execute(f'SELECT count(*) FROM "{table}"').fetchone()[0] print(f"added table {table}: {count} rows") integrity = after.execute("PRAGMA integrity_check").fetchone()[0] foreign = after.execute("PRAGMA foreign_key_check").fetchall() versions = (before.execute("PRAGMA user_version").fetchone()[0], after.execute("PRAGMA user_version").fetchone()[0])if integrity != "ok": failures.append(f"integrity_check: {integrity}")if foreign: failures.append(f"foreign_key_check: {len(foreign)} rows")for failure in failures: print(f"FAIL {failure}") verdict = "FAIL"if failures else"PASS" print(f"{verdict} {before_path} v{versions[0]} → {after_path} v{versions[1]}: " f"{kept_cells} original cells under their rowids, integrity {integrity}, foreign keys {'ok' if not foreign else 'broken'}")return1if failures else0if __name__ == "__main__":if len(sys.argv) != 3: sys.exit(__doc__) sys.exit(main(sys.argv[1], sys.argv[2]))