feat(mcp): добавить контракты, схему БД и миграции MCP

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
DenozordecandClaude Sonnet 5.5 committed 2026-11-15 20:36:08 +07:00
1 parent bd4ce59058
commit 7380f18f15
132 files changed
+12164 -125

No files matched your search

@@ -0,0 +1,155 @@
import assert from "node:assert/strict"
import { randomUUID } from "node:crypto"
import { execFile } from "node:child_process"
import { promisify } from "node:util"
import { basename, dirname, join, resolve } from "node:path"
import { writeFile, unlink, mkdir } from "node:fs/promises"
import type { SchemaInventoryClient } from "./migration-schema-inventory.js"
assert.equal(process.env.MMAPP_OWN_MIGRATION_DATA_INVENTORY, "true")
const databaseUrl = new URL(process.env.DATABASE_URL ?? "")
assert.equal(databaseUrl.hostname, "127.0.0.1"); assert.equal(databaseUrl.pathname, "/postgres")
assert.equal(databaseUrl.username,"mcp_test"); assert(databaseUrl.port&&databaseUrl.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 { pool, closePool } = await import("./index.js")
const { Pool } = await import("pg")
const { applySqlMigrations } = await import("./migrate.js")
const { captureMigrationData, compareMigrationData } = await import("./migration-data-inventory.js")
const execute = promisify(execFile), cases: string[] = []
let restored: InstanceType<typeof Pool> | undefined
const restoredName = "mm_data_restore_" + randomUUID().replaceAll("-", "")
assert(/^mm_data_restore_[a-f0-9]{32}$/.test(restoredName))
const archive = join(resolve(process.env.MCP_STATE_DIR!), "owned-migration-data.dump")
async function capture(connection = pool, options: Parameters<typeof captureMigrationData>[1] = {}, intercept?: (client: SchemaInventoryClient) => SchemaInventoryClient) {
const client = await connection.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(intercept ? intercept(client) : client, options)
} finally { await client.query("ROLLBACK"); client.release(true) }
}
try {
await applySqlMigrations(pool)
// Plain pg_dump/pg_restore does not copy the source database's own comment.
// Match the independently-created target profile before comparing schemas.
await pool.query("COMMENT ON DATABASE postgres IS NULL")
await pool.query("UPDATE traffic_settings SET enabled=false; UPDATE uptime_settings SET enabled=false,resources_enabled=false,ping_enabled=false,speed_enabled=false")
await pool.query("CREATE TABLE mm_data_probe(id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,value text NOT NULL,payload jsonb,amount numeric,at timestamptz)")
await pool.query("INSERT INTO mm_data_probe(value,payload,amount,at) VALUES('OWNED_RAW_VALUE_MUST_NOT_ESCAPE','{\"a\":2,\"b\":[1,true]}',123.4500,'2026-10-25T12:34:56+07:00')")
await pool.query("ALTER TABLE mm_data_probe ADD COLUMN flag integer NOT NULL DEFAULT 7")
await pool.query("CREATE TABLE mm_data_duplicates(value text); INSERT INTO mm_data_duplicates VALUES('same'),('same'),('different')")
await pool.query("CREATE TABLE mm_data_arrays(value text[]); INSERT INTO mm_data_arrays VALUES('[0:1]={left,right}'::text[])")
await pool.query("CREATE TABLE mm_data_partitioned(k integer,value text) PARTITION BY RANGE(k); CREATE TABLE mm_data_partition_a PARTITION OF mm_data_partitioned FOR VALUES FROM(0) TO(10); CREATE TABLE mm_data_partition_b PARTITION OF mm_data_partitioned FOR VALUES FROM(10) TO(20); INSERT INTO mm_data_partitioned VALUES(1,'a'),(11,'b')")
await pool.query("CREATE MATERIALIZED VIEW mm_data_cached AS SELECT value FROM mm_data_duplicates")
await pool.query("CREATE VIEW mm_data_never_execute AS SELECT 1/0 AS forbidden")
await assert.rejects(pool.query("SELECT * FROM mm_data_never_execute"))
const baseline = await capture()
assert.equal(baseline.tables.find(row => row.name === "mm_data_partitioned")!.rowCount, 0)
assert.equal(baseline.tables.find(row => row.name === "mm_data_partition_a")!.rowCount, 1)
assert.equal(baseline.tables.find(row => row.name === "mm_data_partition_b")!.rowCount, 1)
assert.equal(baseline.tables.find(row => row.name === "mm_data_cached")!.rowCount, 3)
assert(!baseline.tables.some(row => row.name === "mm_data_never_execute"))
assert(!JSON.stringify(baseline).includes("OWNED_RAW_VALUE_MUST_NOT_ESCAPE"))
assert.equal(baseline.ready, false); assert.equal(baseline.allReplicaQuiescence, false)
cases.push("actual PG18 captures private row digests, ONLY physical partition rows and materialized data without executing views or exposing values")
await pool.query("DELETE FROM mm_data_duplicates; INSERT INTO mm_data_duplicates VALUES('different'),('same'),('same')")
assert.equal(compareMigrationData(baseline, await capture()).equal, true)
cases.push("actual row insertion order does not change multiset evidence; duplicate multiplicity is retained")
await pool.query("DELETE FROM mm_data_duplicates WHERE ctid=(SELECT ctid FROM mm_data_duplicates WHERE value='same' LIMIT 1)")
await assert.rejects(async () => compareMigrationData(baseline, await capture()))
await pool.query("INSERT INTO mm_data_duplicates VALUES('same')")
cases.push("actual loss of one duplicate changes content evidence despite unchanged logical schema")
await pool.query("UPDATE mm_data_arrays SET value=ARRAY['left','right']")
assert.equal((await pool.query("SELECT to_jsonb('[0:1]={left,right}'::text[])=to_jsonb(ARRAY['left','right']) AS same")).rows[0].same, true)
await assert.rejects(async () => compareMigrationData(baseline, await capture()))
await pool.query("UPDATE mm_data_arrays SET value='[0:1]={left,right}'::text[]")
cases.push("native array lower bounds remain distinct even when JSONB projections are equal")
await assert.rejects(capture(pool, { maxRows: 1 }))
await assert.rejects(capture(pool, { maxBytes: 1 }))
cases.push("actual row and byte overflow refuse a complete snapshot instead of truncating")
await pool.query("INSERT INTO mm_data_duplicates VALUES(repeat('Z',1048577))")
await assert.rejects(capture(), error => error instanceof Error && !error.message.includes("ZZZZZ"))
await pool.query("DELETE FROM mm_data_duplicates WHERE length(value)>1048576")
cases.push("actual oversized row refuses without reflecting its contents")
const sequence = baseline.sequences.find(row => row.name === "mm_data_probe_id_seq")!
await pool.query("SELECT setval('mm_data_probe_id_seq',$1,true)", [String(BigInt(sequence.lastValue) + 1n)])
await assert.rejects(async () => compareMigrationData(baseline, await capture()))
await pool.query("SELECT setval('mm_data_probe_id_seq',$1,$2)", [sequence.lastValue, sequence.isCalled])
cases.push("actual sequence state participates in equivalence independently of table contents")
let wrote = false
const during = await capture(pool, {}, client => ({ async query(text, values) {
const result = await client.query(text, values)
if (!wrote && text.includes("migration-data:declare")) { wrote = true; await pool.query("INSERT INTO mm_data_duplicates VALUES('concurrent-outside-snapshot')") }
return result
} }))
assert(wrote); assert.equal(compareMigrationData(baseline, during).equal, true)
await assert.rejects(async () => compareMigrationData(baseline, await capture()))
await pool.query("DELETE FROM mm_data_duplicates WHERE value='concurrent-outside-snapshot'")
cases.push("actual repeatable-read rows exclude a concurrent committed insert while the next capture observes it; no quiescence grant")
let advanced = false
await assert.rejects(capture(pool, {}, client => ({ async query(text, values) {
const result = await client.query(text, values)
if (!advanced && text.includes("migration-data:sequence") && text.includes('"mm_data_probe_id_seq"')) { advanced = true; await pool.query("SELECT setval('mm_data_probe_id_seq',$1,true)", [String(BigInt(sequence.lastValue) + 10n)]) }
return result
} })))
assert(advanced)
await pool.query("SELECT setval('mm_data_probe_id_seq',$1,$2)", [sequence.lastValue, sequence.isCalled])
cases.push("actual nontransactional sequence change between stability reads refuses complete evidence")
await assert.rejects(capture(pool, {}, client => ({ async query(text, values) {
if (text.includes("migration-data:context")) await client.query("SET LOCAL row_security=on")
return client.query(text, values)
} })))
cases.push("actual unsafe row-security GUC is refused before private row capture")
const beforeRestore = await capture()
const pgEnv = { ...process.env, PGPASSWORD: decodeURIComponent(databaseUrl.password) }
const args = ["--host", databaseUrl.hostname, "--port", databaseUrl.port, "--username", decodeURIComponent(databaseUrl.username), "--no-password"]
const dumped = await execute("pg_dump", [...args, "--format=custom", "--dbname=postgres"], { env: pgEnv, encoding: "buffer", maxBuffer: 16 * 1048576 })
await mkdir(dirname(archive), { recursive: true }); await writeFile(archive, dumped.stdout)
await pool.query('CREATE DATABASE "' + restoredName + '" TEMPLATE template0')
await execute("pg_restore", [...args, "--single-transaction", "--exit-on-error", "--dbname=" + restoredName, archive], { env: pgEnv, maxBuffer: 1048576 })
const cloneUrl = new URL(databaseUrl); cloneUrl.pathname = "/" + restoredName
restored = new Pool({ connectionString: cloneUrl.toString(), max: 2 })
const afterRestore = await capture(restored)
assert.equal(compareMigrationData(beforeRestore, afterRestore).equal, true)
assert.notEqual(JSON.stringify(beforeRestore.schema.observedPhysical), JSON.stringify(afterRestore.schema.observedPhysical))
cases.push("actual pg_dump/pg_restore preserves full logical data and sequence evidence despite fast-default physical-cache differences")
await restored.query("CREATE SCHEMA mmapp_control; CREATE TABLE mmapp_control.owned_data_probe(value text); INSERT INTO mmapp_control.owned_data_probe VALUES('OWNED_CONTROL_VALUE_NOT_APPLICATION_DATA')")
const scope = { applicationNamespaces: ["public"], excludedControlNamespaces: ["mmapp_control"] }
const controlBefore = await capture(restored, { scope })
await restored.query("UPDATE mmapp_control.owned_data_probe SET value='CONTROL_CHANGED_OUTSIDE_APPLICATION'")
const controlAfter = await capture(restored, { scope })
assert.equal(compareMigrationData(controlBefore, controlAfter).equal, true)
assert(!controlAfter.tables.some(row => row.namespace === "mmapp_control"))
assert(!JSON.stringify(controlBefore).includes("OWNED_CONTROL_VALUE_NOT_APPLICATION_DATA"))
cases.push("actual external control table values remain excluded from application data evidence")
await restored.query(`
CREATE SEQUENCE mmapp_control.owned_data_probe_seq;
CREATE FUNCTION mmapp_control.owned_data_guard() RETURNS event_trigger LANGUAGE plpgsql AS $$BEGIN NULL; END$$;
CREATE EVENT TRIGGER owned_data_guard ON ddl_command_start EXECUTE FUNCTION mmapp_control.owned_data_guard();
`)
const scoped = { scope: { applicationNamespaces: ["public", "mmapp_control"] }, dataNamespaces: ["public"], includeControlEventTriggers: true }
const protectedBefore = await capture(restored, scoped)
assert.equal(protectedBefore.schema.eventTriggers?.[0].name, "owned_data_guard")
assert(protectedBefore.schema.sections.sequences.some(row => row.namespace === "mmapp_control"))
assert(protectedBefore.tables.every(row => row.namespace === "public"))
assert(protectedBefore.sequences.every(row => row.namespace === "public"))
await restored.query("UPDATE mmapp_control.owned_data_probe SET value='CONTROL_CHANGED_AGAIN'; SELECT pg_catalog.setval('mmapp_control.owned_data_probe_seq', 42)")
assert.equal(compareMigrationData(protectedBefore, await capture(restored, scoped)).equal, true)
await restored.query("ALTER EVENT TRIGGER owned_data_guard DISABLE")
const protectedAfter = await capture(restored, scoped)
assert.equal(protectedAfter.schema.eventTriggers?.[0].enabled, "D")
assert.throws(() => compareMigrationData(protectedBefore, protectedAfter))
cases.push("actual scoped capture binds control schema and event trigger mode while excluding mutable control rows and sequences")
const altered = structuredClone(afterRestore); altered.tables[0].rowCount++
assert.throws(() => compareMigrationData(beforeRestore, altered))
cases.push("altered evidence vector is refused; unsigned content equality never grants enrollment or READY")
const layout = await (await import("./migration-schema-restore-layout.sql-helper.js")).acceptMigrationSchemaRestoreLayoutSql()
cases.push(...layout.names)
console.log("Owned migration data inventory SQL:", JSON.stringify({ cases: cases.length, names: cases, actualSql: true, actualDumpRestoreDataEquivalent: true, nativeCalls: 0, readyAuthorized: false, allReplicaQuiescence: false, productionDatabaseTouched: false }))
} finally {
if (restored) { await restored.end(); restored = undefined }
const owned = (await pool.query("SELECT datname,pg_get_userbyid(datdba)=current_user AS owned FROM pg_database WHERE datname=$1", [restoredName])).rows
if (owned.length) { assert.equal(owned.length, 1); assert.equal(owned[0].owned, true); assert.equal(owned[0].datname, restoredName); await pool.query('DROP DATABASE "' + restoredName + '"') }
await unlink(archive).catch(error => { if ((error as { code?: string }).code !== "ENOENT") throw error })
await closePool()
}