Browse Source

Add a schemaExists check

The schemaExists check doesn't need `CREATE ON DATABASE` permissions. 

So it avoids throwing an error in case where both: 
- The schema already exists
- The user doesn't have  `CREATE ON DATABASE` permission
pull/5399/head
Dhanushka 2 years ago committed by GitHub
parent
commit
2d371d60f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 21
      packages/nocodb/src/lib/db/sql-client/lib/pg/PgClient.ts

21
packages/nocodb/src/lib/db/sql-client/lib/pg/PgClient.ts

@ -474,17 +474,22 @@ class PGClient extends KnexClient {
]);
}
// if (this.connectionConfig.searchPath && this.connectionConfig.searchPath[0]) {
const schemaName = this.connectionConfig.searchPath?.[0] || 'public'
// Check schemaExists because `CREATE SCHEMA IF NOT EXISTS` requires permissions of `CREATE ON DATABASE`
const schemaExists = !!(
await this.sqlClient.raw(
`SELECT schema_name FROM information_schema.schemata WHERE schema_name = ?`,
[schemaName]
)
).rows?.[0];
if(!schemaExists) {
await this.sqlClient.raw(
`CREATE SCHEMA IF NOT EXISTS ?? AUTHORIZATION ?? `,
[
(this.connectionConfig.searchPath &&
this.connectionConfig.searchPath[0]) ||
'public',
this.connectionConfig.connection.user,
]
[schemaName, this.connectionConfig.connection.user]
);
// }
}
// this.sqlClient = knex(this.connectionConfig);
} catch (e) {

Loading…
Cancel
Save