From fb77e756c5ca9e766d1c575c322a886a3f6bb52c Mon Sep 17 00:00:00 2001 From: Emmanuel Gomez Date: Tue, 4 Jan 2022 23:11:43 -0800 Subject: [PATCH 1/2] fix(redshift-driver): Use Redshift schema query. Fixes #3876. --- .../cubejs-redshift-driver/src/RedshiftDriver.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/packages/cubejs-redshift-driver/src/RedshiftDriver.ts b/packages/cubejs-redshift-driver/src/RedshiftDriver.ts index 6dba0a2dfcde2..e13820fa4a56d 100644 --- a/packages/cubejs-redshift-driver/src/RedshiftDriver.ts +++ b/packages/cubejs-redshift-driver/src/RedshiftDriver.ts @@ -22,6 +22,19 @@ export class RedshiftDriver extends PostgresDriver super(options); } + /** + * @param {string} schemaName + * @return {Promise>} + */ + async createSchemaIfNotExists(schemaName: string) { + const schemaExistsQuery = `SELECT nspname FROM pg_namespace where nspname = ${this.param(0)}`; + const schemas = await this.query(schemaExistsQuery, [schemaName]) + if (schemas.length === 0) { + return this.query(`CREATE SCHEMA IF NOT EXISTS ${schemaName}`); + } + return null; + } + protected getInitialConfiguration(): Partial { return { // @todo It's not possible to support UNLOAD in readOnly mode, because we need column types (CREATE TABLE?) From e8020f7e9b495ade3b6f5f6aafb011c2dc883259 Mon Sep 17 00:00:00 2001 From: Konstantin Burkalev Date: Wed, 19 Mar 2025 15:30:39 +0200 Subject: [PATCH 2/2] fix(redshift-driver): Use Redshift-specific schema query --- .../cubejs-redshift-driver/src/RedshiftDriver.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/packages/cubejs-redshift-driver/src/RedshiftDriver.ts b/packages/cubejs-redshift-driver/src/RedshiftDriver.ts index 7cf19a5a61376..0b7e2ca0a4e8e 100644 --- a/packages/cubejs-redshift-driver/src/RedshiftDriver.ts +++ b/packages/cubejs-redshift-driver/src/RedshiftDriver.ts @@ -116,6 +116,20 @@ export class RedshiftDriver extends PostgresDriver `; } + /** + * In Redshift schemas not owned by the current user are not shown in regular information_schema, + * so it needs to be queried through the pg_namespace table. Because user might be granted specific + * permissions on the concrete schema (like CREATE tables in already existing but not owned pre-aggregation schema). + * @override + */ + public override async createSchemaIfNotExists(schemaName: string): Promise { + const schemaExistsQuery = `SELECT nspname FROM pg_namespace where nspname = ${this.param(0)}`; + const schemas = await this.query(schemaExistsQuery, [schemaName]); + if (schemas.length === 0) { + await this.query(`CREATE SCHEMA IF NOT EXISTS ${schemaName}`, []); + } + } + /** * In Redshift external tables are not shown in regular Postgres information_schema, * so it needs to be queried separately.