Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
test(schema-history): restore identity counters in fixtures and pin the dumper with a round-trip test
garrytan88f48292c038
diff --git a/scripts/capture-schema-history-fixture.ts b/scripts/capture-schema-history-fixture.tsindex d21ab6ebf5..09df9f113b 100644--- a/scripts/capture-schema-history-fixture.ts+++ b/scripts/capture-schema-history-fixture.ts@@ -101,9 +101,9 @@ export async function dumpBrainSchema(query: Query, version: number): Promise<stif (String(rows).includes(tag)) throw new Error(`row data of ${name} contains ${tag}`);out.push(`INSERT INTO ${ident(name)} (${t.insertable.join(', ')}) OVERRIDING SYSTEM VALUE\n SELECT ${t.insertable.join(', ')} FROM json_populate_recordset(NULL::${ident(name)}, ${tag}${rows}${tag});`);}- for (const s of sequences) {- const [{ last_value, is_called }] = await query(`SELECT last_value::text AS last_value, is_called FROM ${ident(s.name)}`);- out.push(`SELECT setval('${ident(s.name)}', ${last_value}, ${is_called ? 'true' : 'false'});`);+ for (const s of await sequencePositions(query)) {+ const target = s.table_name && s.column_name ? `pg_get_serial_sequence('${ident(s.table_name)}', '${s.column_name}')` : `'${ident(s.name)}'`;+ out.push(`SELECT setval(${target}, ${s.last_value}, ${s.is_called ? 'true' : 'false'});`);}const constraints = await query(`SELECT t.relname AS table_name, con.conname AS name, con.contype AS type, pg_get_constraintdef(con.oid) AS def@@ -169,6 +169,23 @@ export async function dumpBrainSchema(query: Query, version: number): Promise<streturn out.join('\n\n') + '\n';}+/** Every public sequence with its position, keyed by the column that owns it (serial or identity) or by its own name. */+async function sequencePositions(query: Query): Promise<Array<{ name: string; table_name: string | null; column_name: string | null; last_value: string; is_called: boolean }>> {+ const out = [];+ for (const s of await query(`+ SELECT c.relname AS name, t.relname AS table_name, a.attname AS column_name+ FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace+ LEFT JOIN pg_depend d ON d.classid = 'pg_class'::regclass AND d.objid = c.oid AND d.deptype IN ('a', 'i')+ LEFT JOIN pg_class t ON t.oid = d.refobjid+ LEFT JOIN pg_attribute a ON a.attrelid = d.refobjid AND a.attnum = d.refobjsubid+ WHERE n.nspname = 'public' AND c.relkind = 'S' AND ${NOT_EXTENSION_MEMBER('pg_class', 'c.oid')}+ ORDER BY c.oid`)) {+ const [{ last_value, is_called }] = await query(`SELECT last_value::text AS last_value, is_called FROM ${ident(s.name)}`);+ out.push({ name: s.name, table_name: s.table_name ?? null, column_name: s.column_name ?? null, last_value, is_called: Boolean(is_called) });+ }+ return out;+}+/** Blank every secret a fresh brain generates (e.g. shared_skill_state.token_secret) so no fixture carries one. */async function blankSecrets(query: Query): Promise<void> {for (const c of await query(`@@ -201,6 +218,8 @@ async function fidelity(original: Query, restored: Query): Promise<string[]> {if (catalogDiff.length > 0) problems.push(formatCatalogDiffForFailure(catalogDiff));const [a, b] = [await original(FUNCTION_BODIES_SQL), await restored(FUNCTION_BODIES_SQL)];if (JSON.stringify(a) !== JSON.stringify(b)) problems.push('function bodies differ');+ const positions = async (q: Query) => JSON.stringify((await sequencePositions(q)).map(({ table_name, column_name, last_value, is_called }) => ({ table_name, column_name, last_value, is_called })));+ if (await positions(original) !== await positions(restored)) problems.push('sequence positions differ');const [ra, rb] = [await tableRows(original), await tableRows(restored)];for (const name of new Set([...Object.keys(ra), ...Object.keys(rb)])) {if (ra[name] !== rb[name]) problems.push(`rows of ${name} differ`);diff --git a/test/fixtures/schema-history/pglite/v260.sql.gz b/test/fixtures/schema-history/pglite/v260.sql.gzindex d16c705123..acd4db8533 100644Binary files a/test/fixtures/schema-history/pglite/v260.sql.gz and b/test/fixtures/schema-history/pglite/v260.sql.gz differdiff --git a/test/fixtures/schema-history/pglite/v261.sql.gz b/test/fixtures/schema-history/pglite/v261.sql.gzindex b39e6edad6..efbe83d31d 100644Binary files a/test/fixtures/schema-history/pglite/v261.sql.gz and b/test/fixtures/schema-history/pglite/v261.sql.gz differdiff --git a/test/fixtures/schema-history/pglite/v262.sql.gz b/test/fixtures/schema-history/pglite/v262.sql.gzindex 48c11848d1..392ab4d495 100644Binary files a/test/fixtures/schema-history/pglite/v262.sql.gz and b/test/fixtures/schema-history/pglite/v262.sql.gz differdiff --git a/test/fixtures/schema-history/pglite/v263.sql.gz b/test/fixtures/schema-history/pglite/v263.sql.gzindex d9c2d4706d..4b045a28fc 100644Binary files a/test/fixtures/schema-history/pglite/v263.sql.gz and b/test/fixtures/schema-history/pglite/v263.sql.gz differdiff --git a/test/fixtures/schema-history/pglite/v264.sql.gz b/test/fixtures/schema-history/pglite/v264.sql.gzindex 911025de5a..587947bf27 100644Binary files a/test/fixtures/schema-history/pglite/v264.sql.gz and b/test/fixtures/schema-history/pglite/v264.sql.gz differdiff --git a/test/fixtures/schema-history/pglite/v265.sql.gz b/test/fixtures/schema-history/pglite/v265.sql.gzindex 0152c41c5c..ed1701406f 100644Binary files a/test/fixtures/schema-history/pglite/v265.sql.gz and b/test/fixtures/schema-history/pglite/v265.sql.gz differdiff --git a/test/fixtures/schema-history/pglite/v266.sql.gz b/test/fixtures/schema-history/pglite/v266.sql.gzindex 71fd2580d7..7750429d56 100644Binary files a/test/fixtures/schema-history/pglite/v266.sql.gz and b/test/fixtures/schema-history/pglite/v266.sql.gz differdiff --git a/test/fixtures/schema-history/pglite/v267.sql.gz b/test/fixtures/schema-history/pglite/v267.sql.gzindex e850ef249a..5cc7de211a 100644Binary files a/test/fixtures/schema-history/pglite/v267.sql.gz and b/test/fixtures/schema-history/pglite/v267.sql.gz differdiff --git a/test/fixtures/schema-history/pglite/v268.sql.gz b/test/fixtures/schema-history/pglite/v268.sql.gzindex 8b9420381e..4024644bd7 100644Binary files a/test/fixtures/schema-history/pglite/v268.sql.gz and b/test/fixtures/schema-history/pglite/v268.sql.gz differdiff --git a/test/fixtures/schema-history/pglite/v269.sql.gz b/test/fixtures/schema-history/pglite/v269.sql.gzindex 91f0063a5d..1fd944ba99 100644Binary files a/test/fixtures/schema-history/pglite/v269.sql.gz and b/test/fixtures/schema-history/pglite/v269.sql.gz differdiff --git a/test/fixtures/schema-history/pglite/v270.sql.gz b/test/fixtures/schema-history/pglite/v270.sql.gzindex 89f1c695db..5e01deef0d 100644Binary files a/test/fixtures/schema-history/pglite/v270.sql.gz and b/test/fixtures/schema-history/pglite/v270.sql.gz differdiff --git a/test/fixtures/schema-history/pglite/v271.sql.gz b/test/fixtures/schema-history/pglite/v271.sql.gzindex 5e33c109b9..7c8a8d6cff 100644Binary files a/test/fixtures/schema-history/pglite/v271.sql.gz and b/test/fixtures/schema-history/pglite/v271.sql.gz differdiff --git a/test/fixtures/schema-history/pglite/v272.sql.gz b/test/fixtures/schema-history/pglite/v272.sql.gzindex 2b6c1ed24f..1b0153fb2c 100644Binary files a/test/fixtures/schema-history/pglite/v272.sql.gz and b/test/fixtures/schema-history/pglite/v272.sql.gz differdiff --git a/test/fixtures/schema-history/pglite/v273.sql.gz b/test/fixtures/schema-history/pglite/v273.sql.gzindex 54a411ebbc..89d6cad17b 100644Binary files a/test/fixtures/schema-history/pglite/v273.sql.gz and b/test/fixtures/schema-history/pglite/v273.sql.gz differdiff --git a/test/fixtures/schema-history/pglite/v274.sql.gz b/test/fixtures/schema-history/pglite/v274.sql.gzindex 081c0d1bec..95e22b899c 100644Binary files a/test/fixtures/schema-history/pglite/v274.sql.gz and b/test/fixtures/schema-history/pglite/v274.sql.gz differdiff --git a/test/fixtures/schema-history/pglite/v275.sql.gz b/test/fixtures/schema-history/pglite/v275.sql.gzindex d6a68ed1d1..89269c4e96 100644Binary files a/test/fixtures/schema-history/pglite/v275.sql.gz and b/test/fixtures/schema-history/pglite/v275.sql.gz differdiff --git a/test/fixtures/schema-history/postgres/v260.sql.gz b/test/fixtures/schema-history/postgres/v260.sql.gzindex 40d3199a4e..76dedaf0e6 100644Binary files a/test/fixtures/schema-history/postgres/v260.sql.gz and b/test/fixtures/schema-history/postgres/v260.sql.gz differdiff --git a/test/fixtures/schema-history/postgres/v261.sql.gz b/test/fixtures/schema-history/postgres/v261.sql.gzindex 8acb12d8ef..2f8e2787cd 100644Binary files a/test/fixtures/schema-history/postgres/v261.sql.gz and b/test/fixtures/schema-history/postgres/v261.sql.gz differdiff --git a/test/fixtures/schema-history/postgres/v262.sql.gz b/test/fixtures/schema-history/postgres/v262.sql.gzindex c0ff673c1e..db39aafb05 100644Binary files a/test/fixtures/schema-history/postgres/v262.sql.gz and b/test/fixtures/schema-history/postgres/v262.sql.gz differdiff --git a/test/fixtures/schema-history/postgres/v263.sql.gz b/test/fixtures/schema-history/postgres/v263.sql.gzindex fed8dc4faa..0b936fdf1a 100644Binary files a/test/fixtures/schema-history/postgres/v263.sql.gz and b/test/fixtures/schema-history/postgres/v263.sql.gz differdiff --git a/test/fixtures/schema-history/postgres/v264.sql.gz b/test/fixtures/schema-history/postgres/v264.sql.gzindex a2918634dd..d4fa004236 100644Binary files a/test/fixtures/schema-history/postgres/v264.sql.gz and b/test/fixtures/schema-history/postgres/v264.sql.gz differdiff --git a/test/fixtures/schema-history/postgres/v265.sql.gz b/test/fixtures/schema-history/postgres/v265.sql.gzindex 09180efeb6..d4b019fd08 100644Binary files a/test/fixtures/schema-history/postgres/v265.sql.gz and b/test/fixtures/schema-history/postgres/v265.sql.gz differdiff --git a/test/fixtures/schema-history/postgres/v266.sql.gz b/test/fixtures/schema-history/postgres/v266.sql.gzindex 241286d223..6b6328c024 100644Binary files a/test/fixtures/schema-history/postgres/v266.sql.gz and b/test/fixtures/schema-history/postgres/v266.sql.gz differdiff --git a/test/fixtures/schema-history/postgres/v267.sql.gz b/test/fixtures/schema-history/postgres/v267.sql.gzindex 557fa36c4c..e06eac229e 100644Binary files a/test/fixtures/schema-history/postgres/v267.sql.gz and b/test/fixtures/schema-history/postgres/v267.sql.gz differdiff --git a/test/fixtures/schema-history/postgres/v268.sql.gz b/test/fixtures/schema-history/postgres/v268.sql.gzindex e30f7d5063..23ef9063c0 100644Binary files a/test/fixtures/schema-history/postgres/v268.sql.gz and b/test/fixtures/schema-history/postgres/v268.sql.gz differdiff --git a/test/fixtures/schema-history/postgres/v269.sql.gz b/test/fixtures/schema-history/postgres/v269.sql.gzindex 50377a2cc2..c63e6b226d 100644Binary files a/test/fixtures/schema-history/postgres/v269.sql.gz and b/test/fixtures/schema-history/postgres/v269.sql.gz differdiff --git a/test/fixtures/schema-history/postgres/v270.sql.gz b/test/fixtures/schema-history/postgres/v270.sql.gzindex dfb67a8b87..cfacdc9e50 100644Binary files a/test/fixtures/schema-history/postgres/v270.sql.gz and b/test/fixtures/schema-history/postgres/v270.sql.gz differdiff --git a/test/fixtures/schema-history/postgres/v271.sql.gz b/test/fixtures/schema-history/postgres/v271.sql.gzindex 5925e7d0dd..6ed4c0e790 100644Binary files a/test/fixtures/schema-history/postgres/v271.sql.gz and b/test/fixtures/schema-history/postgres/v271.sql.gz differdiff --git a/test/fixtures/schema-history/postgres/v272.sql.gz b/test/fixtures/schema-history/postgres/v272.sql.gzindex 984c65fd37..47f0a90071 100644Binary files a/test/fixtures/schema-history/postgres/v272.sql.gz and b/test/fixtures/schema-history/postgres/v272.sql.gz differdiff --git a/test/fixtures/schema-history/postgres/v273.sql.gz b/test/fixtures/schema-history/postgres/v273.sql.gzindex f557aad47f..9079027a6a 100644Binary files a/test/fixtures/schema-history/postgres/v273.sql.gz and b/test/fixtures/schema-history/postgres/v273.sql.gz differdiff --git a/test/fixtures/schema-history/postgres/v274.sql.gz b/test/fixtures/schema-history/postgres/v274.sql.gzindex 7baa495108..ad06996c55 100644Binary files a/test/fixtures/schema-history/postgres/v274.sql.gz and b/test/fixtures/schema-history/postgres/v274.sql.gz differdiff --git a/test/fixtures/schema-history/postgres/v275.sql.gz b/test/fixtures/schema-history/postgres/v275.sql.gzindex d16149b612..1cf9d1d96c 100644Binary files a/test/fixtures/schema-history/postgres/v275.sql.gz and b/test/fixtures/schema-history/postgres/v275.sql.gz differdiff --git a/test/scripts/capture-schema-history-fixture.test.ts b/test/scripts/capture-schema-history-fixture.test.tsnew file mode 100644index 0000000000..d2b55e930a--- /dev/null+++ b/test/scripts/capture-schema-history-fixture.test.ts@@ -0,0 +1,73 @@+/**+ * scripts/capture-schema-history-fixture.ts: the dump a schema-history fixture+ * is made of must restore to the same catalog, function bodies and rows. A+ * small schema with every object kind the dumper writes (identity and serial+ * columns, a generated column, a partial index, a foreign key to a unique+ * index, a check, a constraint trigger with a WHEN clause, a view, row level+ * security with a policy) is dumped from one engine and restored into another.+ */+import { afterAll, beforeAll, expect, test } from 'bun:test';+import { PGLiteEngine } from '../../src/core/pglite-engine.ts';+import { dumpBrainSchema } from '../../scripts/capture-schema-history-fixture.ts';+import { FUNCTION_BODIES_SQL, pgliteCatalogQuery } from '../helpers/schema-catalog.ts';+import { diffCatalogSnapshots, snapshotCatalog } from '../helpers/schema-diff.ts';+import { withColdPglite } from '../helpers/with-snapshot.ts';++let original: PGLiteEngine;+let restored: PGLiteEngine;+let dump: string;++beforeAll(async () => {+ original = new PGLiteEngine();+ restored = new PGLiteEngine();+ await withColdPglite(async () => {+ await original.connect({});+ await restored.connect({});+ });+ await original.db.exec(`+ CREATE TABLE owners (id SERIAL PRIMARY KEY, handle TEXT NOT NULL);+ CREATE UNIQUE INDEX owners_handle ON owners (handle);+ CREATE TABLE items (+ id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,+ owner_handle TEXT NOT NULL REFERENCES owners (handle),+ title TEXT NOT NULL CHECK (title <> ''),+ pending BOOLEAN NOT NULL DEFAULT false,+ title_length INTEGER GENERATED ALWAYS AS (length(title)) STORED,+ meta JSONB NOT NULL DEFAULT '{}'+ );+ CREATE INDEX items_pending ON items (owner_handle) WHERE pending;+ CREATE FUNCTION items_touch() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN RETURN NULL; END $$;+ CREATE CONSTRAINT TRIGGER items_touch AFTER UPDATE ON items DEFERRABLE INITIALLY DEFERRED FOR EACH ROW+ WHEN ((old.title IS DISTINCT FROM new.title) OR (old.pending IS DISTINCT FROM new.pending)) EXECUTE FUNCTION items_touch();+ CREATE VIEW pending_items AS SELECT id, title FROM items WHERE pending;+ ALTER TABLE items ENABLE ROW LEVEL SECURITY;+ CREATE POLICY items_all ON items FOR ALL TO PUBLIC USING (true);+ INSERT INTO owners (handle) VALUES ('alice-example'), ('bob-example');+ INSERT INTO items (owner_handle, title, pending, meta) VALUES ('alice-example', 'first', true, '{"k": "it''s"}'), ('bob-example', 'second', false, '{}');+ `);+ dump = await dumpBrainSchema(async (sql) => (await original.db.query(sql)).rows as Array<Record<string, any>>, 1);+ await restored.db.exec(dump);+}, 60_000);++afterAll(async () => {+ await original.disconnect();+ await restored.disconnect();+});++test('a dumped brain restores to the same catalog, function bodies and rows', async () => {+ const unparenthesized = (value: unknown) => JSON.stringify(value).replace(/[()]/g, '');+ const diff = diffCatalogSnapshots(await snapshotCatalog(pgliteCatalogQuery(original)), await snapshotCatalog(pgliteCatalogQuery(restored)), { compareOrdinals: true })+ .filter((d) => !(d.section === 'triggers' && d.kind === 'changed' && unparenthesized(d.expected) === unparenthesized(d.actual)));+ expect(diff).toEqual([]);+ expect((await restored.db.query(FUNCTION_BODIES_SQL)).rows).toEqual((await original.db.query(FUNCTION_BODIES_SQL)).rows);+ for (const table of ['owners', 'items']) {+ const rows = async (engine: PGLiteEngine) => (await engine.db.query(`SELECT * FROM ${table} ORDER BY id`)).rows;+ expect(await rows(restored)).toEqual(await rows(original));+ }+});++test('identity and serial counters continue where the original left off', async () => {+ await restored.db.exec(`INSERT INTO owners (handle) VALUES ('carol-example'); INSERT INTO items (owner_handle, title) VALUES ('carol-example', 'third')`);+ expect((await restored.db.query(`SELECT max(id)::int AS id FROM owners`)).rows).toEqual([{ id: 3 }]);+ expect((await restored.db.query(`SELECT max(id)::int AS id FROM items`)).rows).toEqual([{ id: 3 }]);+});