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: await pregate database probes in sequence on one client (#13151)

Adam Eivydb25bb6335f8
diff --git a/scripts/lib/pregateResources.js b/scripts/lib/pregateResources.js
index 5860c65cc5..3bd9cb6a3b 100644
--- a/scripts/lib/pregateResources.js
+++ b/scripts/lib/pregateResources.js
@@ -78,7 +78,7 @@ export function uncreatableExtensions({ installed, trusted, superuser }) {
* host and port so it can travel in the receipt; the endpoint is printed
* separately for the operator.
*/
-export async function probeTestDatabase(stageEnv, { repoRoot }) {
+export async function probeTestDatabase(stageEnv, { repoRoot, createClient }) {
const endpoint = `${stageEnv.PGHOST}:${stageEnv.PGPORT}/${TEST_DATABASE}`;
let pg;
try {
@@ -86,7 +86,8 @@ export async function probeTestDatabase(stageEnv, { repoRoot }) {
} catch (error) {
return { ok: false, endpoint, reason: `pg package unavailable (${error.code ?? error.message})` };
}
- const client = new pg.Client({
+ // One connection cannot run queries concurrently (pg queues them and warns, pg@9 removes that), so the probes are awaited in sequence.
+ const client = (createClient ?? ((config) => new pg.Client(config)))({
host: stageEnv.PGHOST,
port: Number(stageEnv.PGPORT),
user: stageEnv.PGUSER,
@@ -99,11 +100,9 @@ export async function probeTestDatabase(stageEnv, { repoRoot }) {
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 present = await client.query('SELECT extname FROM pg_extension WHERE extname = ANY($1)', [REQUIRED_EXTENSIONS]);
+ const available = await client.query('SELECT DISTINCT name FROM pg_available_extension_versions WHERE trusted AND name = ANY($1)', [REQUIRED_EXTENSIONS]);
+ const role = await 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),
diff --git a/scripts/pregate.test.js b/scripts/pregate.test.js
index 866b8b88f6..a6837a097d 100644
--- a/scripts/pregate.test.js
+++ b/scripts/pregate.test.js
@@ -1080,6 +1080,44 @@ describe('probeTestDatabase', () => {
expect(result.reason).not.toContain(String(port));
expect(result.endpoint).toBe(`127.0.0.1:${port}/portos_test`);
});
+
+ // A single pg connection queues overlapping queries (and pg@9 drops that), so the probe must await them in turn.
+ const fakeClient = ({ installed = [], trusted = [], superuser = true }) => {
+ let inFlight = false;
+ const respond = (sql) => {
+ if (sql.includes('pg_extension')) return { rows: installed.map((extname) => ({ extname })) };
+ if (sql.includes('pg_available_extension_versions')) return { rows: trusted.map((name) => ({ name })) };
+ if (sql.includes('pg_roles')) return { rows: [{ rolsuper: superuser }] };
+ return { rows: [] };
+ };
+ return {
+ connect: async () => {},
+ end: async () => {},
+ query(sql) {
+ if (inFlight) throw new Error('overlapping query on one client');
+ inFlight = true;
+ return Promise.resolve().then(() => {
+ inFlight = false;
+ return respond(sql);
+ });
+ },
+ };
+ };
+ const probeWith = (opts) => probeTestDatabase(
+ { PGHOST: '127.0.0.1', PGPORT: '5432', PGUSER: 'portos', PGPASSWORD: 'portos' },
+ { repoRoot: REPO_ROOT, createClient: () => fakeClient(opts) },
+ );
+
+ it('awaits its extension and role probes in turn on the single client', async () => {
+ const result = await probeWith({ installed: ['vector', 'pgcrypto'] });
+ expect(result).toEqual({ ok: true, endpoint: `127.0.0.1:5432/${TEST_DATABASE}`, provision: false });
+ });
+
+ it('still names a missing extension a non-superuser cannot create', async () => {
+ const result = await probeWith({ installed: ['pgcrypto'], superuser: false });
+ expect(result.ok).toBe(false);
+ expect(result.reason).toMatch(/^Postgres extension\(s\) vector missing/);
+ });
});
describe('buildPregateReport', () => {