Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

test: preserve provisioned extensions when rebuilding the backup fixture (#13120)

Adam Eivy1d335e84ef39
diff --git a/docs/CONTRIBUTING.md b/docs/CONTRIBUTING.md
index c7e60da5aa..8f32031162 100644
--- a/docs/CONTRIBUTING.md
+++ b/docs/CONTRIBUTING.md
@@ -264,6 +264,22 @@ dump, so `test:db` takes a session-level Postgres advisory lock for the run
(`server/test/dbRunExclusivity.js`). A second run from another worktree prints
`Another DB test run holds portos_test` and waits for the first to finish.
+### Repairing a test database whose extensions were dropped
+
+The application role cannot create extensions. `backup.db.test.js` used to
+rebuild `portos_test` with `DROP SCHEMA public CASCADE`, which dropped `vector`
+and `pgcrypto`; it now removes only application objects and leaves them
+installed, and refuses to start (before changing anything) if they are missing.
+A test database an older checkout already stripped needs a one-time repair by a
+superuser, after which `npm run setup:db:test` works with the ordinary role:
+
+```bash
+psql -d portos_test -c 'CREATE EXTENSION IF NOT EXISTS vector; CREATE EXTENSION IF NOT EXISTS pgcrypto;'
+npm run setup:db:test
+```
+
+Do not grant the application role superuser to avoid this step.
+
Supported full local validation uses the CI worker budget. An unbounded
`npm test` can oversubscribe a loaded machine: real media, prompt-catalog,
and nested-worker cases then hit their existing budgets. A green focused
diff --git a/server/services/backup.db.test.js b/server/services/backup.db.test.js
index 8accf3dd77..52cb36ce49 100644
--- a/server/services/backup.db.test.js
+++ b/server/services/backup.db.test.js
@@ -65,6 +65,62 @@ const feedSequenceValues = async () => Object.fromEntries((await query(
[syncFeedTables.map(syncFeedSequenceName)],
)).rows.map(({ sequencename, v }) => [sequencename, v]));
+const REQUIRED_EXTENSIONS = ['vector', 'pgcrypto'];
+const extensionIds = async () => (await query(
+ 'SELECT extname, oid::text AS oid FROM pg_extension WHERE extname = ANY($1::text[]) ORDER BY extname', [REQUIRED_EXTENSIONS],
+)).rows;
+
+// The application role cannot CREATE EXTENSION (needs a superuser), so a
+// fixture that drops them (DROP SCHEMA public CASCADE) cannot rebuild itself
+// (#13120). Remove only application objects, atomically in one DO block, and
+// leave extension members (types, operators, functions) and the namespace.
+// The run-wide DB advisory lock gives this suite exclusive ownership.
+const DROP_APPLICATION_OBJECTS = `DO $portos_fixture_reset$
+DECLARE r record;
+BEGIN
+ FOR r IN SELECT c.oid::regclass::text AS name, c.relkind FROM pg_class c
+ JOIN pg_namespace n ON n.oid = c.relnamespace
+ WHERE n.nspname = 'public' AND c.relkind IN ('v', 'm', 'r', 'p', 'f')
+ AND NOT EXISTS (SELECT 1 FROM pg_depend d WHERE d.classid = 'pg_class'::regclass
+ AND d.objid = c.oid AND d.deptype = 'e')
+ ORDER BY CASE WHEN c.relkind IN ('v', 'm') THEN 0 ELSE 1 END
+ LOOP
+ EXECUTE format('DROP %s IF EXISTS %s CASCADE',
+ CASE r.relkind WHEN 'v' THEN 'VIEW' WHEN 'm' THEN 'MATERIALIZED VIEW' WHEN 'f' THEN 'FOREIGN TABLE' ELSE 'TABLE' END, r.name);
+ END LOOP;
+ FOR r IN SELECT c.oid::regclass::text AS name FROM pg_class c
+ JOIN pg_namespace n ON n.oid = c.relnamespace
+ WHERE n.nspname = 'public' AND c.relkind = 'S'
+ AND NOT EXISTS (SELECT 1 FROM pg_depend d WHERE d.classid = 'pg_class'::regclass
+ AND d.objid = c.oid AND d.deptype = 'e')
+ LOOP EXECUTE format('DROP SEQUENCE IF EXISTS %s CASCADE', r.name); END LOOP;
+ FOR r IN SELECT p.oid::regprocedure::text AS name FROM pg_proc p
+ JOIN pg_namespace n ON n.oid = p.pronamespace
+ WHERE n.nspname = 'public'
+ AND NOT EXISTS (SELECT 1 FROM pg_depend d WHERE d.classid = 'pg_proc'::regclass
+ AND d.objid = p.oid AND d.deptype = 'e')
+ LOOP EXECUTE format('DROP ROUTINE IF EXISTS %s CASCADE', r.name); END LOOP;
+ FOR r IN SELECT t.oid::regtype::text AS name FROM pg_type t
+ JOIN pg_namespace n ON n.oid = t.typnamespace
+ WHERE n.nspname = 'public' AND t.typtype IN ('d', 'e', 'r', 'm') AND t.typcategory <> 'A'
+ AND NOT EXISTS (SELECT 1 FROM pg_depend d WHERE d.classid = 'pg_type'::regclass
+ AND d.objid = t.oid AND d.deptype = 'e')
+ LOOP EXECUTE format('DROP TYPE IF EXISTS %s CASCADE', r.name); END LOOP;
+END
+$portos_fixture_reset$`;
+
+// Refuse before touching anything when provisioning is missing, and prove the
+// extensions survived the reset by identity (an unchanged oid was not recreated).
+async function resetApplicationObjects() {
+ const before = await extensionIds();
+ if (before.length !== REQUIRED_EXTENSIONS.length) {
+ throw new Error(`${REQUIRED_EXTENSIONS.join(' and ')} must be provisioned by an administrator first `
+ + `(see docs/CONTRIBUTING.md "Repairing a test database whose extensions were dropped"); found ${before.map(e => e.extname).join(', ') || 'none'}`);
+ }
+ await query(DROP_APPLICATION_OBJECTS);
+ expect(await extensionIds()).toEqual(before);
+}
+
const health = await checkHealth();
const ready = requireDbOrSkip('services/backup.db.test',
health.connected && spawnSync('psql', ['--version']).status === 0,
@@ -177,7 +233,25 @@ describe.skipIf(!ready)('restore older database schema', { timeout: RESTORE_CASE
}
}
+ // Fixture setup failure must never leave later DB suites on a half-built
+ // schema: rebuild the base schema, then surface the original failure.
async function prepareFixture() {
+ try {
+ await buildFixture();
+ } catch (error) {
+ try {
+ await resetApplicationObjects();
+ await query(await readFile(new URL('../scripts/init-db.sql', import.meta.url), 'utf8'));
+ await ensureSchema({ force: true });
+ await runDbMigrations();
+ } catch (recoveryError) {
+ error.message += ` (schema recovery also failed: ${recoveryError.message})`;
+ }
+ throw error;
+ }
+ }
+
+ async function buildFixture() {
// Other branches can leave entirely retired functions, not just overloads
// of names current DDL still owns. Model that residue before rebuilding the
// disposable fixture; production restore must continue refusing it (#12526).
@@ -186,8 +260,8 @@ describe.skipIf(!ready)('restore older database schema', { timeout: RESTORE_CASE
// The run-wide DB lock grants this whole-database restore suite exclusive
// ownership. Start from a clean schema instead of dumping another branch's
// objects into every snapshot. Reapply base DDL before its additive upgrades.
- await query('DROP SCHEMA public CASCADE');
- await query('CREATE SCHEMA public');
+ await resetApplicationObjects();
+ expect((await query("SELECT 1 FROM pg_proc WHERE proname = 'restore_retired_fixture_function'")).rowCount).toBe(0);
await query(await readFile(new URL('../scripts/init-db.sql', import.meta.url), 'utf8'));
await ensureSchema({ force: true });
await runDbMigrations();