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. 27
      packages/nocodb/src/lib/db/sql-client/lib/pg/PgClient.ts

27
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'
await this.sqlClient.raw(
` CREATE SCHEMA IF NOT EXISTS ?? AUTHORIZATION ?? `, // Check schemaExists because `CREATE SCHEMA IF NOT EXISTS` requires permissions of `CREATE ON DATABASE`
[ const schemaExists = !!(
(this.connectionConfig.searchPath && await this.sqlClient.raw(
this.connectionConfig.searchPath[0]) || `SELECT schema_name FROM information_schema.schemata WHERE schema_name = ?`,
'public', [schemaName]
this.connectionConfig.connection.user, )
] ).rows?.[0];
);
// } if(!schemaExists) {
await this.sqlClient.raw(
`CREATE SCHEMA IF NOT EXISTS ?? AUTHORIZATION ?? `,
[schemaName, this.connectionConfig.connection.user]
);
}
// this.sqlClient = knex(this.connectionConfig); // this.sqlClient = knex(this.connectionConfig);
} catch (e) { } catch (e) {

Loading…
Cancel
Save