Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions graphql/codegen/src/core/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import path from 'node:path';
import { buildClientSchema, printSchema } from 'graphql';

import { PgpmPackage } from '@pgpmjs/core';
import { pgCache } from 'pg-cache';
import { createEphemeralDb, type EphemeralDbResult } from 'pgsql-client';
import { deployPgpm } from 'pgsql-seed';

Expand Down Expand Up @@ -840,6 +841,10 @@ export async function generateMulti(
} finally {
for (const shared of sharedSources.values()) {
const keepDb = Object.values(configs).some((c) => c.db?.keepDb);
// Release pg-cache pool for this ephemeral database before dropping
// deployPgpm() caches connections that must be closed first
pgCache.delete(shared.ephemeralDb.config.database);
await pgCache.waitForDisposals();
shared.ephemeralDb.teardown({ keepDb });
}
}
Expand Down
7 changes: 6 additions & 1 deletion graphql/codegen/src/core/introspect/source/pgpm-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
import { PgpmPackage } from '@pgpmjs/core';
import { buildSchema, introspectionFromSchema } from 'graphql';
import { getPgPool } from 'pg-cache';
import { getPgPool, pgCache } from 'pg-cache';
import { createEphemeralDb, type EphemeralDbResult } from 'pgsql-client';
import { deployPgpm } from 'pgsql-seed';

Expand Down Expand Up @@ -251,6 +251,11 @@ export class PgpmModuleSchemaSource implements SchemaSource {

return { introspection };
} finally {
// Release pg-cache pool for this ephemeral database before dropping
// deployPgpm() and getPgPool() cache connections that must be closed first
pgCache.delete(dbConfig.database);
await pgCache.waitForDisposals();

// Clean up the ephemeral database
teardown({ keepDb });

Expand Down
17 changes: 15 additions & 2 deletions postgres/pgsql-client/src/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,22 @@ export class DbAdmin {
try {
this.run(`dropdb "${name}"`);
} catch (err: any) {
if (!err.message.includes('does not exist')) {
log.warn(`Could not drop database ${name}: ${err.message}`);
if (err.message.includes('does not exist')) {
return;
}
if (err.message.includes('is being accessed by other users')) {
log.warn(`Database ${name} has active sessions, terminating backends and retrying drop...`);
try {
this.run(
`psql -c "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = '${name}' AND pid <> pg_backend_pid();"`
);
this.run(`dropdb "${name}"`);
} catch (retryErr: any) {
log.warn(`Could not drop database ${name} after terminating backends: ${retryErr.message}`);
}
return;
}
log.warn(`Could not drop database ${name}: ${err.message}`);
}
}

Expand Down
Loading