Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Postgres Node): Connection pool of the database object has been destroyed #7074

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,28 @@ export async function execute(
db: PgpDatabase,
): Promise<INodeExecutionData[]> {
items = replaceEmptyStringsByNulls(items, nodeOptions.replaceEmptyStrings as boolean);
const nodeVersion = nodeOptions.typeVersion as number;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code seems to be duplicated a few times. Is there anything we can reduce this code duplication?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added helper to remove duplication


let schema = this.getNodeParameter('schema', 0, undefined, {
extractValue: true,
}) as string;

let table = this.getNodeParameter('table', 0, undefined, {
extractValue: true,
}) as string;

let tableSchema = await getTableSchema(db, schema, table);
let currentSchema = schema;
let currentTable = table;

const queries: QueryWithValues[] = [];

for (let i = 0; i < items.length; i++) {
const schema = this.getNodeParameter('schema', i, undefined, {
schema = this.getNodeParameter('schema', i, undefined, {
extractValue: true,
}) as string;

const table = this.getNodeParameter('table', i, undefined, {
table = this.getNodeParameter('table', i, undefined, {
extractValue: true,
}) as string;

Expand All @@ -183,7 +196,6 @@ export async function execute(
let query = `INSERT INTO $1:name.$2:name($3:name) VALUES($3:csv)${onConflict}`;
let values: QueryValues = [schema, table];

const nodeVersion = this.getNode().typeVersion;
const dataMode =
nodeVersion < 2.2
? (this.getNodeParameter('dataMode', i) as string)
Expand All @@ -209,7 +221,11 @@ export async function execute(
}
}

const tableSchema = await getTableSchema(db, schema, table);
if (currentSchema !== schema || currentTable !== table) {
currentSchema = schema;
currentTable = table;
tableSchema = await getTableSchema(db, schema, table);
}

values.push(checkItemAgainstSchema(this.getNode(), item, tableSchema, i));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,19 +198,31 @@ export async function execute(
db: PgpDatabase,
): Promise<INodeExecutionData[]> {
items = replaceEmptyStringsByNulls(items, nodeOptions.replaceEmptyStrings as boolean);
const nodeVersion = nodeOptions.typeVersion as number;

let schema = this.getNodeParameter('schema', 0, undefined, {
extractValue: true,
}) as string;

let table = this.getNodeParameter('table', 0, undefined, {
extractValue: true,
}) as string;

let tableSchema = await getTableSchema(db, schema, table);
let currentSchema = schema;
let currentTable = table;

const queries: QueryWithValues[] = [];

for (let i = 0; i < items.length; i++) {
const schema = this.getNodeParameter('schema', i, undefined, {
schema = this.getNodeParameter('schema', i, undefined, {
extractValue: true,
}) as string;

const table = this.getNodeParameter('table', i, undefined, {
table = this.getNodeParameter('table', i, undefined, {
extractValue: true,
}) as string;

const nodeVersion = this.getNode().typeVersion;
const columnsToMatchOn: string[] =
nodeVersion < 2.2
? [this.getNodeParameter('columnToMatchOn', i) as string]
Expand Down Expand Up @@ -286,7 +298,11 @@ export async function execute(
}
}

const tableSchema = await getTableSchema(db, schema, table);
if (currentSchema !== schema || currentTable !== table) {
currentSchema = schema;
currentTable = table;
tableSchema = await getTableSchema(db, schema, table);
}

item = checkItemAgainstSchema(this.getNode(), item, tableSchema, i);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,19 +197,31 @@ export async function execute(
db: PgpDatabase,
): Promise<INodeExecutionData[]> {
items = replaceEmptyStringsByNulls(items, nodeOptions.replaceEmptyStrings as boolean);
const nodeVersion = nodeOptions.typeVersion as number;

let schema = this.getNodeParameter('schema', 0, undefined, {
extractValue: true,
}) as string;

let table = this.getNodeParameter('table', 0, undefined, {
extractValue: true,
}) as string;

let tableSchema = await getTableSchema(db, schema, table);
let currentSchema = schema;
let currentTable = table;

const queries: QueryWithValues[] = [];

for (let i = 0; i < items.length; i++) {
const schema = this.getNodeParameter('schema', i, undefined, {
schema = this.getNodeParameter('schema', i, undefined, {
extractValue: true,
}) as string;

const table = this.getNodeParameter('table', i, undefined, {
table = this.getNodeParameter('table', i, undefined, {
extractValue: true,
}) as string;

const nodeVersion = this.getNode().typeVersion;
const columnsToMatchOn: string[] =
nodeVersion < 2.2
? [this.getNodeParameter('columnToMatchOn', i) as string]
Expand Down Expand Up @@ -255,7 +267,11 @@ export async function execute(
);
}

const tableSchema = await getTableSchema(db, schema, table);
if (currentSchema !== schema || currentTable !== table) {
currentSchema = schema;
currentTable = table;
tableSchema = await getTableSchema(db, schema, table);
}

item = checkItemAgainstSchema(this.getNode(), item, tableSchema, i);

Expand Down
3 changes: 2 additions & 1 deletion packages/nodes-base/nodes/Postgres/v2/actions/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ export async function router(this: IExecuteFunctions): Promise<INodeExecutionDat
if (sshClient) {
sshClient.end();
}
pgp.end();

await db.$pool.end();
}

return this.prepareOutputData(returnData);
Expand Down
8 changes: 4 additions & 4 deletions packages/nodes-base/nodes/Postgres/v2/methods/listSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export async function schemaSearch(this: ILoadOptionsFunctions): Promise<INodeLi
const credentials = await this.getCredentials('postgres');
const options = { nodeVersion: this.getNode().typeVersion };

const { db, pgp, sshClient } = await configurePostgres(credentials, options);
const { db, sshClient } = await configurePostgres(credentials, options);

try {
const response = await db.any('SELECT schema_name FROM information_schema.schemata');
Expand All @@ -23,14 +23,14 @@ export async function schemaSearch(this: ILoadOptionsFunctions): Promise<INodeLi
if (sshClient) {
sshClient.end();
}
pgp.end();
await db.$pool.end();
}
}
export async function tableSearch(this: ILoadOptionsFunctions): Promise<INodeListSearchResult> {
const credentials = await this.getCredentials('postgres');
const options = { nodeVersion: this.getNode().typeVersion };

const { db, pgp, sshClient } = await configurePostgres(credentials, options);
const { db, sshClient } = await configurePostgres(credentials, options);

const schema = this.getNodeParameter('schema', 0, {
extractValue: true,
Expand All @@ -54,6 +54,6 @@ export async function tableSearch(this: ILoadOptionsFunctions): Promise<INodeLis
if (sshClient) {
sshClient.end();
}
pgp.end();
await db.$pool.end();
}
}
4 changes: 2 additions & 2 deletions packages/nodes-base/nodes/Postgres/v2/methods/loadOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export async function getColumns(this: ILoadOptionsFunctions): Promise<INodeProp
const credentials = await this.getCredentials('postgres');
const options = { nodeVersion: this.getNode().typeVersion };

const { db, pgp, sshClient } = await configurePostgres(credentials, options);
const { db, sshClient } = await configurePostgres(credentials, options);

const schema = this.getNodeParameter('schema', 0, {
extractValue: true,
Expand All @@ -31,7 +31,7 @@ export async function getColumns(this: ILoadOptionsFunctions): Promise<INodeProp
if (sshClient) {
sshClient.end();
}
pgp.end();
await db.$pool.end();
}
}

Expand Down
Loading