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

fix: report pregate DB stage as not run when a non-superuser test role lacks required extensions (#13134)

Adam Eivy5146c2585abc
diff --git a/scripts/lib/pregateResources.js b/scripts/lib/pregateResources.js
index 914cfa65ce..5860c65cc5 100644
--- a/scripts/lib/pregateResources.js
+++ b/scripts/lib/pregateResources.js
@@ -59,6 +59,18 @@ export function resolveTestDatabaseStageEnv(repoRoot, env = process.env) {
return testDatabaseStageEnv({ env, saved: parseEnvFile(join(repoRoot, '.env')) });
}
+/** Extensions init-db.sql creates; keep in sync with server/scripts/init-db.sql. */
+const REQUIRED_EXTENSIONS = ['vector', 'pgcrypto'];
+
+/**
+ * Names the extensions `setup:db:test` would fail to create: absent from the
+ * database and neither creatable by a superuser nor marked trusted. Pure.
+ */
+export function uncreatableExtensions({ installed, trusted, superuser }) {
+ if (superuser) return [];
+ return REQUIRED_EXTENSIONS.filter((name) => !installed.includes(name) && !trusted.includes(name));
+}
+
/**
* Is a Postgres answering at the DB stage's endpoint? A missing `portos_test`
* is fine (the provisioning stage creates it); an unreachable server, refused
@@ -85,6 +97,25 @@ export async function probeTestDatabase(stageEnv, { repoRoot }) {
try {
await client.connect();
await client.query('SELECT 1');
+ // CREATE EXTENSION IF NOT EXISTS is a no-op for installed extensions, so only
+ // a missing one needs privilege; a non-superuser role cannot supply it (#13134).
+ const [present, available, role] = await Promise.all([
+ client.query('SELECT extname FROM pg_extension WHERE extname = ANY($1)', [REQUIRED_EXTENSIONS]),
+ client.query('SELECT DISTINCT name FROM pg_available_extension_versions WHERE trusted AND name = ANY($1)', [REQUIRED_EXTENSIONS]),
+ client.query('SELECT rolsuper FROM pg_roles WHERE rolname = current_user'),
+ ]);
+ const blocked = uncreatableExtensions({
+ installed: present.rows.map((row) => row.extname),
+ trusted: available.rows.map((row) => row.name),
+ superuser: role.rows[0]?.rolsuper === true,
+ });
+ if (blocked.length > 0) {
+ return {
+ ok: false,
+ endpoint,
+ reason: `Postgres extension(s) ${blocked.join(', ')} missing in ${TEST_DATABASE} and the test role is not a superuser; as a superuser run \`psql -d ${TEST_DATABASE} -c 'CREATE EXTENSION IF NOT EXISTS vector; CREATE EXTENSION IF NOT EXISTS pgcrypto;'\``,
+ };
+ }
return { ok: true, endpoint, provision: false };
} catch (error) {
// 3D000: the database does not exist yet — `npm run setup:db:test` creates it.
diff --git a/scripts/pregate.test.js b/scripts/pregate.test.js
index fff737e8d6..866b8b88f6 100644
--- a/scripts/pregate.test.js
+++ b/scripts/pregate.test.js
@@ -35,7 +35,7 @@ import {
unmergedIndexPaths,
} from './pregate.js';
import { buildPregateReport, PREGATE_REPORT_PATH, validatedTreeHash } from './lib/pregateReport.js';
-import { probeTestDatabase, TEST_DATABASE, testDatabaseStageEnv } from './lib/pregateResources.js';
+import { probeTestDatabase, TEST_DATABASE, uncreatableExtensions, testDatabaseStageEnv } from './lib/pregateResources.js';
import { BROWSER_SUITES } from './lib/browserSuites.js';
import { BROWSER_FIDELITY_FILES } from './lib/browserTestProfiles.js';
@@ -1051,6 +1051,15 @@ describe('testDatabaseStageEnv', () => {
});
});
+describe('uncreatableExtensions', () => {
+ it('flags a missing extension a non-superuser cannot create, and nothing else', () => {
+ expect(uncreatableExtensions({ installed: ['pgcrypto'], trusted: [], superuser: false })).toEqual(['vector']);
+ expect(uncreatableExtensions({ installed: [], trusted: ['vector', 'pgcrypto'], superuser: false })).toEqual([]);
+ expect(uncreatableExtensions({ installed: ['vector', 'pgcrypto'], trusted: [], superuser: false })).toEqual([]);
+ expect(uncreatableExtensions({ installed: [], trusted: [], superuser: true })).toEqual([]);
+ });
+});
+
describe('probeTestDatabase', () => {
it('reports an unreachable server as a missing prerequisite without the endpoint in the reason', async () => {
const { createServer } = await import('node:net');