import assert from "node:assert/strict" import { test } from "node:test" import { partitionRestoreAdmissionSql, partitionRestorePrelude, restorePartitionedDatabase } from "./system-database-partition-restore.js" const toc = "1; 1259 1 TABLE public servers mmapp\n2; 1259 2 TABLE public schema_migrations mmapp\n3; 1259 3 TABLE public server_snapshots mmapp\n" test("preparation covers only known application parents present in the archive", () => { const sql = partitionRestorePrelude(toc + "4; 1259 4 TABLE other flow_buckets owner\n5; 1259 5 TABLE public custom_table owner\n") assert.ok(sql.includes("IN ('server_snapshots')")) assert.ok(sql.includes("c.conparentid = 0")) assert.ok(sql.includes("format('ALTER TABLE %I.%I DROP CONSTRAINT %I CASCADE'")) assert.equal(sql.includes("flow_buckets"), false) assert.equal(sql.includes("custom_table"), false) assert.throws(() => partitionRestorePrelude("1; 1259 1 TABLE public unrelated owner\n"), /обязательные таблицы/) }) test("archive decoding precedes one transactional psql execution with fail-fast SQL", async () => { const calls: string[] = [] await restorePartitionedDatabase("fixture.dump", "fixture-db", { prepareSql: async file => { assert.equal(file, "fixture.dump.sql"); calls.push("prepare") }, command: async (command, args) => { calls.push(command) if (command === "pg_restore" && args[0] === "--list") return Buffer.from(toc) if (command === "psql") { assert.deepEqual(args.slice(0, 7), ["-X", "--no-password", "--single-transaction", "--set", "ON_ERROR_STOP=on", "-d", "fixture-db"]) assert.equal(args[7], "-c"); assert.equal(args[8], partitionRestoreAdmissionSql) assert.match(args[8], /pg_advisory_xact_lock_shared\(8723110::bigint\)/) assert.match(args[8], /SET LOCAL lock_timeout='10s'/) assert.equal(args[9], "-c"); assert.ok(args[10].includes("DROP CONSTRAINT")) assert.deepEqual(args.slice(11), ["-f", "fixture.dump.sql"]) } return Buffer.alloc(0) }, }) assert.deepEqual(calls, ["pg_restore", "prepare", "pg_restore", "psql"]) }) test("psql admission failure is not automatically retried", async () => { let attempts = 0 await assert.rejects(restorePartitionedDatabase("fixture with spaces.dump", "fixture-db", { prepareSql: async () => {}, command: async (command, args) => { if (command === "psql") { attempts++; assert.equal(args[8], partitionRestoreAdmissionSql); throw Error("owned admission refused") } return Buffer.from(toc) }, }), /owned admission refused/) assert.equal(attempts, 1) }) test("approved restore preserves the existing public schema and restores its other entries", async () => { const entries = `8; 2615 2200 SCHEMA - public installation_writer\r\n9; 0 0 ACL - SCHEMA public installation_writer\r\n${toc}` let selected = false await restorePartitionedDatabase("fixture.dump", "fixture-db", { prepareSql: async () => {}, writePublicSchemaPreservingList: async (file, list) => { assert.equal(file, "fixture.dump.list") assert.equal(list, `;${entries}`) selected = true }, command: async (command, args) => { if (command === "pg_restore" && args[0] === "--list") return Buffer.from(entries) assert.ok(selected) if (command === "pg_restore") assert.deepEqual(args, ["--clean", "--if-exists", "--use-list", "fixture.dump.list", "--file", "fixture.dump.sql", "fixture.dump"]) else assert.equal(command, "psql") return Buffer.alloc(0) }, }) assert.ok(selected) }) test("decoded archive cannot reach psql after an asynchronous quarantine failure", async () => { const calls: string[] = [] await assert.rejects(restorePartitionedDatabase("fixture.dump", "fixture-db", { prepareSql: async () => {}, beforeTargetReplacement: async () => { await Promise.resolve() calls.push("quarantine") throw Error("quarantine denied") }, command: async command => { calls.push(command) return Buffer.from(toc) }, }), /quarantine denied/) assert.deepEqual(calls, ["pg_restore", "pg_restore", "quarantine"]) }) test("the target witness brackets the decode on the same psql transaction", async () => { const proof = "SELECT 'owned target witness'" await restorePartitionedDatabase("fixture.dump", "fixture-db", { prepareSql: async () => {}, targetCheckSql: proof, command: async (command, args) => { if (command === "psql") { assert.deepEqual(args.slice(7, 11), ["-c", proof, "-c", partitionRestoreAdmissionSql]) assert.deepEqual(args.slice(-4), ["-f", "fixture.dump.sql", "-c", proof]) } return Buffer.from(toc) }, }) }) test("quarantined restore takes the exclusive writer lock and rechecks the witness before DDL", async () => { const proof = "SELECT 'owned maintenance witness'" await restorePartitionedDatabase("fixture.dump", "fixture-db", { prepareSql: async () => {}, exclusivePartitionWriters: true, targetCheckSql: proof, command: async (command, args) => { if (command === "psql") { assert.deepEqual(args.slice(7, 10), ["-c", proof, "-c"]) assert.match(args[10], /pg_catalog\.pg_advisory_xact_lock\(8723110::bigint\)/) assert.deepEqual(args.slice(11, 14), ["-c", proof, "-c"]) assert.match(args[14], /DROP CONSTRAINT/) assert.deepEqual(args.slice(-4), ["-f", "fixture.dump.sql", "-c", proof]) } return Buffer.from(toc) }, }) }) test("protected restore repairs native inherited primary keys before the final target check", async () => { const proof = "SELECT 'target witness'" await restorePartitionedDatabase("fixture.dump", "fixture-db", { prepareSql: async () => {}, writePublicSchemaPreservingList: async () => {}, exclusivePartitionWriters: true, targetCheckSql: proof, command: async (command, args) => { if (command === "psql") { const script = args.indexOf("-f"), repair = args[script + 3] assert.deepEqual(args.slice(script, script + 3), ["-f", "fixture.dump.sql", "-c"]) assert.match(repair, /p\.relname IN \('server_snapshots'\)/) assert.match(repair, /DROP CONSTRAINT %I RESTRICT/) assert.match(repair, /NOT \(c\.connoinherit OR c\.conislocal\)/) assert.deepEqual(args.slice(-2), ["-c", proof]) } return Buffer.from(toc) }, }) }) test("invalid or undecodable archive never starts target SQL", async () => { for (const phase of ["list", "decode"]) { let executions = 0 await assert.rejects(restorePartitionedDatabase("fixture.dump", "fixture-db", { prepareSql: async () => {}, command: async (command, args) => { if (command === "psql") executions++ if ((phase === "list" && args[0] === "--list") || (phase === "decode" && args[0] === "--clean")) throw Error("archive failed") return Buffer.from(toc) }, }), /archive failed/) assert.equal(executions, 0) } }) test("control namespace archive refuses immediately after TOC before decoding or target replacement", async () => { for (const entry of ["SCHEMA - mmapp_control owner", "TABLE mmapp_control database_schema_state owner", "COMMENT - SCHEMA mmapp_control owner", 'ACL - SCHEMA "MMAPP_CONTROL" owner']) { const calls: string[] = [] await assert.rejects(restorePartitionedDatabase("fixture.dump", "fixture-db", { prepareSql: async () => { calls.push("prepare") }, beforeTargetReplacement: () => { calls.push("replacement") }, command: async (command, args) => { calls.push(command + " " + args[0]); return Buffer.from(toc + "4; 2615 4 " + entry + "\n") }, }), /служебное пространство/) assert.deepEqual(calls, ["pg_restore --list"]) } })