#!/usr/bin/env python3 """Verify row counts and sampled content against the manifest restored from backup.""" import hashlib import json import os import psycopg from psycopg.rows import dict_row def main(): with psycopg.connect(os.environ["DATABASE_URL"], row_factory=dict_row) as conn: manifests = conn.execute( "SELECT table_name, row_count, sample_ids, sample_sha256 " "FROM track2_lab.manifest ORDER BY table_name" ).fetchall() if len(manifests) != 2: raise SystemExit("Expected both benchmark variants in restored manifest") verified = [] for manifest in manifests: table = manifest["table_name"] if table not in {"messages_chat_only", "messages_composite"}: raise SystemExit("Unexpected table in restore manifest") count = conn.execute( "SELECT count(*) AS count FROM track2_lab.{}".format(table) ).fetchone()["count"] if count != manifest["row_count"]: raise SystemExit("Row count mismatch for {}".format(table)) rows = conn.execute( "SELECT id, chat_id::text AS chat_id, created_at::text AS created_at, body " "FROM track2_lab.{} WHERE id = ANY(%s) ORDER BY id".format(table), (manifest["sample_ids"],), ).fetchall() digest = hashlib.sha256( json.dumps(rows, sort_keys=True, default=str).encode("utf-8") ).hexdigest() if digest != manifest["sample_sha256"]: raise SystemExit("Sample checksum mismatch for {}".format(table)) verified.append({"table": table, "rows": count, "sample_sha256": digest}) print(json.dumps({"restored": verified, "result": "PASS"}, indent=2)) if __name__ == "__main__": main()