Files
MikrotikManager/backend/src/db/migration-schema-restore-layout.sql-helper.ts
T

115 lines
9.7 KiB
TypeScript

import assert from "node:assert/strict"
import { randomUUID } from "node:crypto"
import { execFile } from "node:child_process"
import { promisify } from "node:util"
import { join, basename, dirname, resolve } from "node:path"
import { writeFile, unlink } from "node:fs/promises"
import { Pool } from "pg"
import { captureMigrationData, compareMigrationData } from "./migration-data-inventory.js"
import { compareMigrationSchemas } from "./migration-schema-inventory.js"
/** Test-only owned-cluster regression. Never called by an application entrypoint. */
export async function acceptMigrationSchemaRestoreLayoutSql() {
assert.equal(process.env.MMAPP_OWN_MIGRATION_DATA_INVENTORY, "true")
const url = new URL(process.env.DATABASE_URL ?? "")
assert.equal(url.hostname, "127.0.0.1"); assert.equal(url.pathname, "/postgres")
assert.equal(url.username,"mcp_test"); assert(url.port&&url.port!=="5432"); assert.equal(process.env.NODE_ENV,"test")
assert(process.env.MCP_STATE_DIR && basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-"))
const source = new Pool({ connectionString: url.toString(), max: 3 }), names: string[] = [], cases: string[] = []
const pools: Pool[] = []
const archive = join(process.env.MCP_STATE_DIR!, "owned-v3-tombstone.dump")
async function database(label: string, template = "template0") {
const name = "mm_v3_" + label + "_" + randomUUID().replaceAll("-", "")
assert(/^mm_v3_[a-z_]+_[a-f0-9]{32}$/.test(name)); assert(template === "template0" || names.includes(template))
await source.query('CREATE DATABASE "' + name + '" TEMPLATE "' + template + '"'); names.push(name)
const target = new URL(url); target.pathname = "/" + name
const pool = new Pool({ connectionString: target.toString(), max: 2 }); pools.push(pool)
return { name, pool }
}
async function snapshot(pool: Pool) {
const client = await pool.connect()
try {
await client.query("BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY")
for (const query of ["SET LOCAL search_path=pg_catalog", "SET LOCAL TimeZone='UTC'", "SET LOCAL DateStyle='ISO, YMD'", "SET LOCAL standard_conforming_strings=on", "SET LOCAL statement_timeout='5s'", "SET LOCAL row_security=off", "SET LOCAL extra_float_digits=3", "SET LOCAL bytea_output=hex", "SET LOCAL IntervalStyle=postgres", "SET LOCAL lc_monetary='C'", "SET LOCAL lc_numeric='C'"]) await client.query(query)
return await captureMigrationData(client)
} finally { try { await client.query("ROLLBACK") } finally { client.release(true) } }
}
const execute = promisify(execFile)
try {
await source.query("COMMENT ON DATABASE postgres IS NULL")
await source.query(`CREATE TABLE public.layout_parent(id integer PRIMARY KEY,other integer UNIQUE); INSERT INTO public.layout_parent VALUES(1,1);
CREATE TABLE public.layout_probe(
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,doomed text,
keep text COLLATE "C" NOT NULL DEFAULT 'owned',optional integer,
calc integer GENERATED ALWAYS AS(optional+1) STORED,spare text,tail text,
CONSTRAINT layout_fk FOREIGN KEY(optional) REFERENCES public.layout_parent(id),
CONSTRAINT layout_nullable CHECK((optional IS NULL OR optional>=0) AND keep<>'blocked'));
INSERT INTO public.layout_probe(doomed,optional,spare,tail) VALUES('discarded',1,'spare-preserved','tail-preserved');
ALTER TABLE public.layout_probe DROP COLUMN doomed;
CREATE INDEX layout_rich_idx ON public.layout_probe(keep COLLATE "C",(optional+2)) INCLUDE(calc) WHERE optional IS NOT NULL;
CREATE FUNCTION public.layout_guard_fn() RETURNS trigger LANGUAGE plpgsql AS 'BEGIN RETURN NEW; END';
CREATE TRIGGER layout_guard BEFORE UPDATE OF keep ON public.layout_probe FOR EACH ROW WHEN(NEW.keep IS DISTINCT FROM OLD.keep) EXECUTE FUNCTION public.layout_guard_fn()`)
const before = await snapshot(source)
const args = ["--host", url.hostname, "--port", url.port, "--username", decodeURIComponent(url.username), "--no-password"]
const env = { ...process.env, PGPASSWORD: decodeURIComponent(url.password) }
const dump = await execute("pg_dump", [...args, "--format=custom", "--dbname=postgres"], { env, encoding: "buffer", timeout: 30000, maxBuffer: 16777216 })
await writeFile(archive, dump.stdout)
const template = await database("restored")
await execute("pg_restore", [...args, "--single-transaction", "--exit-on-error", "--dbname=" + template.name, archive], { env, timeout: 30000, maxBuffer: 1048576 })
const restored = await snapshot(template.pool)
assert.equal(compareMigrationSchemas(before.schema, restored.schema).equal, true); assert.equal(compareMigrationData(before, restored).equal, true)
assert.notEqual(before.schema.observedPhysical.sha256, restored.schema.observedPhysical.sha256)
assert.notEqual(before.schema.observedRaw.sha256, restored.schema.observedRaw.sha256)
assert.notEqual(before.schema.sections.indexes.find(r => r.name === "layout_rich_idx")!.keys, restored.schema.sections.indexes.find(r => r.name === "layout_rich_idx")!.keys)
assert(before.schema.sections.columns.some(r => r.dropped)); assert(!restored.schema.sections.columns.some(r => r.dropped))
await template.pool.end()
cases.push("real pg_dump/restore compacts tombstone and indkey/INCLUDE attnums, keeps logical schema and actual rows/sequences equal, preserves distinct raw/physical fingerprints")
const mismatches: [string, string][] = [
["same_middle_name_readded", "ALTER TABLE public.layout_probe DROP COLUMN spare; ALTER TABLE public.layout_probe ADD COLUMN spare text"],
["type", "ALTER TABLE public.layout_probe ALTER COLUMN spare TYPE varchar(50)"],
["collation", 'ALTER TABLE public.layout_probe ALTER COLUMN spare TYPE text COLLATE "C"'],
["default", "ALTER TABLE public.layout_probe ALTER COLUMN keep SET DEFAULT 'changed'"],
["generated", "ALTER TABLE public.layout_probe ALTER COLUMN calc SET EXPRESSION AS(optional+9)"],
["identity", "ALTER TABLE public.layout_probe ALTER COLUMN id SET GENERATED BY DEFAULT"],
["include_reference", 'DROP INDEX public.layout_rich_idx; CREATE INDEX layout_rich_idx ON public.layout_probe(keep COLLATE "C",(optional+2)) INCLUDE(tail) WHERE optional IS NOT NULL'],
["index_expression", 'DROP INDEX public.layout_rich_idx; CREATE INDEX layout_rich_idx ON public.layout_probe(keep COLLATE "C",(optional+3)) INCLUDE(calc) WHERE optional IS NOT NULL'],
["and_or_grouping", "ALTER TABLE public.layout_probe DROP CONSTRAINT layout_nullable; ALTER TABLE public.layout_probe ADD CONSTRAINT layout_nullable CHECK(optional IS NULL OR(optional>=0 AND keep<>'blocked'))"],
["comparison_operator", "ALTER TABLE public.layout_probe DROP CONSTRAINT layout_nullable; ALTER TABLE public.layout_probe ADD CONSTRAINT layout_nullable CHECK((optional IS NULL OR optional>0) AND keep<>'blocked')"],
["null_semantics", "ALTER TABLE public.layout_probe DROP CONSTRAINT layout_nullable; ALTER TABLE public.layout_probe ADD CONSTRAINT layout_nullable CHECK((optional IS NOT NULL OR optional>=0) AND keep<>'blocked')"],
["not_valid", "ALTER TABLE public.layout_probe DROP CONSTRAINT layout_nullable; ALTER TABLE public.layout_probe ADD CONSTRAINT layout_nullable CHECK((optional IS NULL OR optional>=0) AND keep<>'blocked') NOT VALID"],
["fk_reference", "ALTER TABLE public.layout_probe DROP CONSTRAINT layout_fk; ALTER TABLE public.layout_probe ADD CONSTRAINT layout_fk FOREIGN KEY(optional) REFERENCES public.layout_parent(other)"],
["trigger_state", "ALTER TABLE public.layout_probe DISABLE TRIGGER layout_guard"],
["function_security", "ALTER FUNCTION public.layout_guard_fn() SECURITY DEFINER"],
]
for (const [label, sql] of mismatches) {
const target = await database(label, template.name)
try {
await target.pool.query(sql)
const changed = await snapshot(target.pool)
assert.throws(() => compareMigrationSchemas(restored.schema, changed.schema), label)
cases.push("actual " + label + " drift remains refused by logical v3")
} finally { await target.pool.end() }
}
const last = await database("same_last_name_readded", template.name)
await last.pool.query("ALTER TABLE public.layout_probe DROP COLUMN tail; ALTER TABLE public.layout_probe ADD COLUMN tail text")
const lost = await snapshot(last.pool)
// Identical final live DDL cannot prove historical data preservation. The
// raw fingerprint changes and actual data comparison must detect row loss.
assert.equal(compareMigrationSchemas(restored.schema, lost.schema).equal, true)
assert.notEqual(restored.schema.observedPhysical.sha256, lost.schema.observedPhysical.sha256)
assert.throws(() => compareMigrationData(restored, lost))
await last.pool.end(); cases.push("same last-column name/type/order readd is not mistaken for data preservation: actual lost values refuse data equality despite equal live DDL")
const unsafe = await database("unsupported_policy", template.name)
await unsafe.pool.query("ALTER TABLE public.layout_probe ENABLE ROW LEVEL SECURITY; CREATE POLICY layout_policy ON public.layout_probe USING(true)")
await assert.rejects(snapshot(unsafe.pool)); await unsafe.pool.end(); cases.push("unsupported actual RLS policy still refuses capture")
return { cases: cases.length, names: cases, actualSql: true, ownedDatabases: names.length, actualDumpRestore: true,
logicalSchemaFormat: "pg18-application-schema-v3", rawAndPhysicalDifferencesRetained: true, dataPreservationNotInferredFromDdl: true,
allWriterDrainAuthenticated: false, sourceGenerationAuthenticated: false, readyAuthorized: false, productionDatabaseTouched: false }
} finally {
await Promise.all(pools.map(p => p.end().catch(() => {})))
for (const name of [...names].reverse()) await source.query('DROP DATABASE "' + name + '" WITH(FORCE)')
await source.end(); await unlink(archive).catch(e => { if (e.code !== "ENOENT") throw e })
}
}