Browse Source

Merge pull request #5796 from Budibase/fix/pg-connection-issues

more strategic connection closing in pg
master
Martin McKeaveney 4 years ago
committed by GitHub
parent
commit
61b97b68de
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 82
      packages/server/src/integrations/postgres.ts

82
packages/server/src/integrations/postgres.ts

@ -147,7 +147,7 @@ module PostgresModule {
return parts.join(" || ") return parts.join(" || ")
} }
async internalQuery(query: SqlQuery) { async internalQuery(query: SqlQuery, close: boolean = true) {
const client = this.client const client = this.client
this.index = 1 this.index = 1
// need to handle a specific issue with json data types in postgres, // need to handle a specific issue with json data types in postgres,
@ -164,10 +164,11 @@ module PostgresModule {
try { try {
return await client.query(query.sql, query.bindings || []) return await client.query(query.sql, query.bindings || [])
} catch (err) { } catch (err) {
await this.client.end()
// @ts-ignore // @ts-ignore
throw new Error(err) throw new Error(err)
} finally { } finally {
await this.client.end() if (close) await this.client.end()
} }
} }
@ -204,48 +205,54 @@ module PostgresModule {
} }
} catch (err) { } catch (err) {
tableKeys = {} tableKeys = {}
} finally {
await this.client.close()
} }
const columnsResponse = await this.client.query(this.COLUMNS_SQL) try {
const tables: { [key: string]: Table } = {} const columnsResponse = await this.client.query(this.COLUMNS_SQL)
const tables: { [key: string]: Table } = {}
for (let column of columnsResponse.rows) { for (let column of columnsResponse.rows) {
const tableName: string = column.table_name const tableName: string = column.table_name
const columnName: string = column.column_name const columnName: string = column.column_name
// table key doesn't exist yet // table key doesn't exist yet
if (!tables[tableName] || !tables[tableName].schema) { if (!tables[tableName] || !tables[tableName].schema) {
tables[tableName] = { tables[tableName] = {
_id: buildExternalTableId(datasourceId, tableName), _id: buildExternalTableId(datasourceId, tableName),
primary: tableKeys[tableName] || [], primary: tableKeys[tableName] || [],
name: tableName, name: tableName,
schema: {}, schema: {},
}
} }
}
const identity = !!( const identity = !!(
column.identity_generation || column.identity_generation ||
column.identity_start || column.identity_start ||
column.identity_increment column.identity_increment
) )
const hasDefault = const hasDefault =
typeof column.column_default === "string" && typeof column.column_default === "string" &&
column.column_default.startsWith("nextval") column.column_default.startsWith("nextval")
const isGenerated = const isGenerated =
column.is_generated && column.is_generated !== "NEVER" column.is_generated && column.is_generated !== "NEVER"
const isAuto: boolean = hasDefault || identity || isGenerated const isAuto: boolean = hasDefault || identity || isGenerated
tables[tableName].schema[columnName] = { tables[tableName].schema[columnName] = {
autocolumn: isAuto, autocolumn: isAuto,
name: columnName, name: columnName,
...convertSqlType(column.data_type), ...convertSqlType(column.data_type),
}
} }
}
const final = finaliseExternalTables(tables, entities) const final = finaliseExternalTables(tables, entities)
this.tables = final.tables this.tables = final.tables
this.schemaErrors = final.errors this.schemaErrors = final.errors
} catch (err) {
// @ts-ignore
throw new Error(err)
} finally {
await this.client.end()
}
} }
async create(query: SqlQuery | string) { async create(query: SqlQuery | string) {
@ -274,8 +281,9 @@ module PostgresModule {
if (Array.isArray(input)) { if (Array.isArray(input)) {
const responses = [] const responses = []
for (let query of input) { for (let query of input) {
responses.push(await this.internalQuery(query)) responses.push(await this.internalQuery(query, false))
} }
await this.client.end()
return responses return responses
} else { } else {
const response = await this.internalQuery(input) const response = await this.internalQuery(input)

Loading…
Cancel
Save