Skip to content

Commit

Permalink
fix(core): Pg-promise de-initialization fix (#7417)
Browse files Browse the repository at this point in the history
Github issue / Community forum post (link here to close automatically):

https://community.n8n.io/t/postgres-node-called-end-on-pool-more-than-once/30585/1
  • Loading branch information
michael-radency authored and elsmr committed Oct 19, 2023
1 parent a69dc03 commit dd01532
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 20 deletions.
6 changes: 3 additions & 3 deletions packages/nodes-base/nodes/CrateDb/CrateDb.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,15 +378,15 @@ export class CrateDb implements INodeType {
returnItems = this.helpers.returnJsonArray(getItemsCopy(items, columns));
}
} else {
pgp.end();
await db.$pool.end();
throw new NodeOperationError(
this.getNode(),
`The operation "${operation}" is not supported!`,
);
}

// Close the connection
pgp.end();
// shuts down the connection pool associated with the db object to allow the process to finish
await db.$pool.end();

return [returnItems];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ export class MicrosoftSql implements INodeType {
}
}

// Close the connection
// shuts down the connection pool associated with the db object to allow the process to finish
await pool.close();

const itemData = generatePairedItemData(items.length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,19 @@ export async function initDB(this: ITriggerFunctions | ILoadOptionsFunctions) {
}

export async function searchSchema(this: ILoadOptionsFunctions): Promise<INodeListSearchResult> {
const { db, pgp } = await initDB.call(this);
const { db } = await initDB.call(this);
const schemaList = await db.any('SELECT schema_name FROM information_schema.schemata');
const results: INodeListSearchItems[] = (schemaList as IDataObject[]).map((s) => ({
name: s.schema_name as string,
value: s.schema_name as string,
}));
pgp.end();
await db.$pool.end();
return { results };
}

export async function searchTables(this: ILoadOptionsFunctions): Promise<INodeListSearchResult> {
const schema = this.getNodeParameter('schema', 0) as IDataObject;
const { db, pgp } = await initDB.call(this);
const { db } = await initDB.call(this);
let tableList = [];
try {
tableList = await db.any(
Expand All @@ -138,6 +138,6 @@ export async function searchTables(this: ILoadOptionsFunctions): Promise<INodeLi
name: s.table_name as string,
value: s.table_name as string,
}));
pgp.end();
await db.$pool.end();
return { results };
}
4 changes: 2 additions & 2 deletions packages/nodes-base/nodes/Postgres/PostgresTrigger.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ export class PostgresTrigger implements INodeType {
const additionalFields = this.getNodeParameter('additionalFields', 0) as IDataObject;

// initialize and connect to database
const { db, pgp } = await initDB.call(this);
const { db } = await initDB.call(this);
const connection = await db.connect({ direct: true });

// prepare and set up listener
Expand Down Expand Up @@ -284,7 +284,7 @@ export class PostgresTrigger implements INodeType {
`Postgres Trigger Error: ${(error as Error).message}`,
);
} finally {
pgp.end();
await db.$pool.end();
}
};

Expand Down
8 changes: 4 additions & 4 deletions packages/nodes-base/nodes/Postgres/v1/PostgresV1.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ export class PostgresV1 implements INodeType {

const db = pgp(config);
await db.connect();
pgp.end();
await db.$pool.end();
} catch (error) {
return {
status: 'Error',
Expand Down Expand Up @@ -412,15 +412,15 @@ export class PostgresV1 implements INodeType {

returnItems = wrapData(updateItems);
} else {
pgp.end();
await db.$pool.end();
throw new NodeOperationError(
this.getNode(),
`The operation "${operation}" is not supported!`,
);
}

// Close the connection
pgp.end();
// shuts down the connection pool associated with the db object to allow the process to finish
await db.$pool.end();

return [returnItems];
}
Expand Down
6 changes: 3 additions & 3 deletions packages/nodes-base/nodes/QuestDb/QuestDb.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,15 +256,15 @@ export class QuestDb implements INodeType {

returnItems = this.helpers.returnJsonArray(insertData);
} else {
pgp.end();
await db.$pool.end();
throw new NodeOperationError(
this.getNode(),
`The operation "${operation}" is not supported!`,
);
}

// Close the connection
pgp.end();
// shuts down the connection pool associated with the db object to allow the process to finish
await db.$pool.end();

return [returnItems];
}
Expand Down
6 changes: 3 additions & 3 deletions packages/nodes-base/nodes/TimescaleDb/TimescaleDb.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,15 +320,15 @@ export class TimescaleDb implements INodeType {

returnItems = this.helpers.returnJsonArray(updateItems);
} else {
pgp.end();
await db.$pool.end();
throw new NodeOperationError(
this.getNode(),
`The operation "${operation}" is not supported!`,
);
}

// Close the connection
pgp.end();
// shuts down the connection pool associated with the db object to allow the process to finish
await db.$pool.end();

return [returnItems];
}
Expand Down

0 comments on commit dd01532

Please sign in to comment.