Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript
2.5 KB
Raw Blame
-- down-mutate-pre-v0.18.sql
-- Strip the v0.18+ forward-referenced state from a fresh-LATEST brain to
-- simulate a pre-v0.18 brain shape. Covers issues #366, #375, #378 —
-- pre-v0.18 brains crashed on `column "source_id" does not exist`.
--
-- After this runs, `gbrain doctor` MUST walk forward via the bootstrap
-- (postgres-engine.ts:applyForwardReferenceBootstrap) and reach LATEST
-- without wedging. Bootstrap re-creates `sources` table + seeds 'default',
-- re-adds `pages.source_id`, then SCHEMA_SQL replay + migrations run clean.
--
-- Pattern mirrored from test/bootstrap.test.ts:102-139 (PGLite side).
-- Run via psql against $DATABASE_URL.

BEGIN;

-- Restore the pre-v0.18 unique constraint shape on pages.slug (v0.18 widened
-- this to (source_id, slug)).
ALTER TABLE pages DROP CONSTRAINT IF EXISTS pages_source_slug_key;
ALTER TABLE pages ADD CONSTRAINT pages_slug_key UNIQUE (slug);

-- v230's trust-generation triggers fire on pages.source_id changes, so they
-- depend on the column. Pre-v0.18 brains had no such triggers; the walk
-- forward re-creates them when v230 runs again.
DO $$
DECLARE r record;
BEGIN
  FOR r IN SELECT tgname, tgrelid::regclass AS tbl FROM pg_trigger
            WHERE tgname LIKE 'trust\_generation\_%' AND NOT tgisinternal LOOP
    EXECUTE format('DROP TRIGGER %I ON %s', r.tgname, r.tbl);
  END LOOP;
END $$;

-- Any other trigger that names pages.source_id (for example the per-folder access stamp, #8134) depends on the column too.
-- Pre-v0.18 brains had none of them; the walk forward re-creates each one when its migration runs again.
DO $$
DECLARE r record;
BEGIN
  FOR r IN SELECT DISTINCT t.tgname FROM pg_trigger t
             JOIN pg_depend d ON d.classid = 'pg_trigger'::regclass AND d.objid = t.oid
             JOIN pg_attribute a ON a.attrelid = d.refobjid AND a.attnum = d.refobjsubid
            WHERE d.refclassid = 'pg_class'::regclass AND d.refobjid = 'pages'::regclass AND a.attname = 'source_id' AND NOT t.tgisinternal LOOP
    EXECUTE format('DROP TRIGGER %I ON pages', r.tgname);
  END LOOP;
END $$;

DROP INDEX IF EXISTS idx_pages_source_id;
ALTER TABLE pages DROP COLUMN IF EXISTS source_id;

-- DROP TABLE sources with CASCADE so any dangling FKs go too.
DROP TABLE IF EXISTS sources CASCADE;

-- Pre-v0.18 didn't have resolution_type on links either.
ALTER TABLE links DROP CONSTRAINT IF EXISTS links_resolution_type_check;
ALTER TABLE links DROP COLUMN IF EXISTS resolution_type;

-- Mark the brain at a pre-v0.18 version so the migration runner walks forward.
UPDATE config SET value = '20' WHERE key = 'version';

COMMIT;