Merge pull request #10665 from Budibase/budi-6829/primary-key-pg-issue

Fix - read only access to PG primary keys
This commit is contained in:
Michael Drury 2023-05-23 12:41:36 +01:00 committed by GitHub
commit bec8ee65c2
2 changed files with 14 additions and 9 deletions

View File

@ -25,6 +25,8 @@ export function createDatasourcesStore() {
store.update(state => ({ store.update(state => ({
...state, ...state,
selectedDatasourceId: id, selectedDatasourceId: id,
// Remove any possible schema error
schemaError: null,
})) }))
} }

View File

@ -126,14 +126,15 @@ class PostgresIntegration extends Sql implements DatasourcePlus {
COLUMNS_SQL!: string COLUMNS_SQL!: string
PRIMARY_KEYS_SQL = ` PRIMARY_KEYS_SQL = () => `
select tc.table_schema, tc.table_name, kc.column_name as primary_key SELECT pg_namespace.nspname table_schema
from information_schema.table_constraints tc , pg_class.relname table_name
join , pg_attribute.attname primary_key
information_schema.key_column_usage kc on kc.table_name = tc.table_name FROM pg_class
and kc.table_schema = tc.table_schema JOIN pg_index ON pg_class.oid = pg_index.indrelid AND pg_index.indisprimary
and kc.constraint_name = tc.constraint_name JOIN pg_attribute ON pg_attribute.attrelid = pg_class.oid AND pg_attribute.attnum = ANY(pg_index.indkey)
where tc.constraint_type = 'PRIMARY KEY'; JOIN pg_namespace ON pg_namespace.oid = pg_class.relnamespace
WHERE pg_namespace.nspname = '${this.config.schema}';
` `
constructor(config: PostgresConfig) { constructor(config: PostgresConfig) {
@ -239,7 +240,9 @@ class PostgresIntegration extends Sql implements DatasourcePlus {
let tableKeys: { [key: string]: string[] } = {} let tableKeys: { [key: string]: string[] } = {}
await this.openConnection() await this.openConnection()
try { try {
const primaryKeysResponse = await this.client.query(this.PRIMARY_KEYS_SQL) const primaryKeysResponse = await this.client.query(
this.PRIMARY_KEYS_SQL()
)
for (let table of primaryKeysResponse.rows) { for (let table of primaryKeysResponse.rows) {
const tableName = table.table_name const tableName = table.table_name
if (!tableKeys[tableName]) { if (!tableKeys[tableName]) {