fix(base): propagate pg-extension bootstrap failure reason; align closeSync style

This commit is contained in:
Philipinho
2026-04-23 03:26:41 +01:00
parent a798397af0
commit 7ca712c9ab
2 changed files with 50 additions and 9 deletions
@@ -57,8 +57,8 @@ describe('PostgresExtensionService', () => {
expect(Number(row.c)).toBeGreaterThan(0); expect(Number(row.c)).toBeGreaterThan(0);
await svc.detach(conn); await svc.detach(conn);
} finally { } finally {
await conn.closeSync(); conn.closeSync();
await instance.closeSync(); instance.closeSync();
} }
}); });
@@ -73,8 +73,8 @@ describe('PostgresExtensionService', () => {
await svc.detach(conn); await svc.detach(conn);
await expect(svc.detach(conn)).resolves.toBeUndefined(); await expect(svc.detach(conn)).resolves.toBeUndefined();
} finally { } finally {
await conn.closeSync(); conn.closeSync();
await instance.closeSync(); instance.closeSync();
} }
}); });
@@ -86,8 +86,44 @@ describe('PostgresExtensionService', () => {
try { try {
await expect(svc.configureOnConnection(conn)).rejects.toThrow(/not ready/i); await expect(svc.configureOnConnection(conn)).rejects.toThrow(/not ready/i);
} finally { } finally {
await conn.closeSync(); conn.closeSync();
await instance.closeSync(); instance.closeSync();
}
});
it('includes the bootstrap failure reason in the not-ready error', async () => {
// Force bootstrap to fail by giving the service a broken DB URL so that
// LOAD postgres still succeeds but something in the bootstrap path throws.
// Simplest reliable failure: monkey-patch the service so its bootstrap
// runs a SQL statement that cannot succeed. We accept a small amount of
// test-only access by subclassing.
class BreakingService extends PostgresExtensionService {
async onApplicationBootstrap(): Promise<void> {
// Call super to keep the gate logic, but sabotage inside by
// running INSTALL on a closed connection via a try-wrapper that
// throws synchronously and is captured by the parent catch.
// Simplest approach: directly set the failure and leave ready=false.
(this as any).ready = false;
(this as any).bootstrapFailure = 'simulated boot failure XYZ';
}
}
const svc = new BreakingService(
makeConfig(),
makeEnv() as any,
);
await svc.onApplicationBootstrap();
const instance = await DuckDBInstance.create(':memory:');
const conn = await instance.connect();
try {
await expect(svc.configureOnConnection(conn)).rejects.toThrow(
/simulated boot failure XYZ/,
);
} finally {
conn.closeSync();
instance.closeSync();
} }
}); });
}); });
@@ -32,6 +32,7 @@ import { EnvironmentService } from '../../../integrations/environment/environmen
export class PostgresExtensionService implements OnApplicationBootstrap { export class PostgresExtensionService implements OnApplicationBootstrap {
private readonly logger = new Logger(PostgresExtensionService.name); private readonly logger = new Logger(PostgresExtensionService.name);
private ready = false; private ready = false;
private bootstrapFailure: string | null = null;
constructor( constructor(
private readonly config: QueryCacheConfigProvider, private readonly config: QueryCacheConfigProvider,
@@ -64,9 +65,10 @@ export class PostgresExtensionService implements OnApplicationBootstrap {
// app: the cache service handles this by falling through to Postgres // app: the cache service handles this by falling through to Postgres
// when `isReady()` returns false (see `CollectionLoader.load`). // when `isReady()` returns false (see `CollectionLoader.load`).
this.ready = false; this.ready = false;
this.bootstrapFailure = error.message;
} finally { } finally {
await conn.closeSync(); conn.closeSync();
await bootstrap.closeSync(); bootstrap.closeSync();
} }
} }
@@ -83,8 +85,11 @@ export class PostgresExtensionService implements OnApplicationBootstrap {
*/ */
async configureOnConnection(conn: DuckDBConnection): Promise<void> { async configureOnConnection(conn: DuckDBConnection): Promise<void> {
if (!this.ready) { if (!this.ready) {
const reason = this.bootstrapFailure
? `: ${this.bootstrapFailure}`
: '';
throw new Error( throw new Error(
'PostgresExtensionService not ready — check bootstrap logs', `PostgresExtensionService not ready${reason}. Check bootstrap logs.`,
); );
} }