fix: harden DEV runtime migration transactions

This commit is contained in:
Code Queue Review
2026-05-23 00:14:13 +00:00
parent 63d8f7b5a6
commit 852ec3dbc7
2 changed files with 115 additions and 6 deletions
+32 -6
View File
@@ -250,7 +250,14 @@ async function verifyRuntimeReadiness(report, env, options) {
queryClient: options.queryClient,
pgModuleLoader: options.pgModuleLoader
});
const runtime = await runtimeStore.readiness();
let runtime;
try {
runtime = await runtimeStore.readiness();
} finally {
if (!options.queryClient && typeof runtimeStore.pool?.end === "function") {
await runtimeStore.pool.end();
}
}
report.runtime = summarizeRuntime(runtime);
report.actions.readinessVerified = runtime.ready === true;
report.gates = gatesFromRuntime(runtime);
@@ -277,11 +284,9 @@ async function applyMigration(report, sql, env, options) {
if (options.queryClient) {
client = options.queryClient;
} else {
const pool = await createPgPool(env, options);
client = pool;
closeClient = async () => {
await pool.end();
};
const migrationClient = await createMigrationClient(env, options);
client = migrationClient.client;
closeClient = migrationClient.close;
}
await client.query("BEGIN");
await client.query(sql);
@@ -300,6 +305,27 @@ async function applyMigration(report, sql, env, options) {
}
}
async function createMigrationClient(env, options) {
const pool = await createPgPool(env, options);
let client;
try {
client = await pool.connect();
} catch (error) {
await pool.end();
throw error;
}
return {
client,
close: async () => {
try {
client.release();
} finally {
await pool.end();
}
}
};
}
async function createPgPool(env, options) {
let pg;
try {