From 1ffab7f5d6ba3892f67fed4b0fd3921af5b46063 Mon Sep 17 00:00:00 2001 From: Rory Powell Date: Wed, 27 Oct 2021 16:21:19 +0100 Subject: [PATCH 1/3] Postgres: Use another schema other than 'public' --- packages/server/src/integrations/postgres.ts | 21 ++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/packages/server/src/integrations/postgres.ts b/packages/server/src/integrations/postgres.ts index abd1fa9286..3cf1bdb3e0 100644 --- a/packages/server/src/integrations/postgres.ts +++ b/packages/server/src/integrations/postgres.ts @@ -27,6 +27,7 @@ module PostgresModule { database: string user: string password: string + schema: string ssl?: boolean ca?: string rejectUnauthorized?: boolean @@ -64,6 +65,11 @@ module PostgresModule { default: "root", required: true, }, + schema: { + type: DatasourceFieldTypes.STRING, + default: "public", + required: true, + }, ssl: { type: DatasourceFieldTypes.BOOLEAN, default: false, @@ -137,8 +143,7 @@ module PostgresModule { private readonly client: any private readonly config: PostgresConfig - COLUMNS_SQL = - "select * from information_schema.columns where not table_schema = 'information_schema' and not table_schema = 'pg_catalog'" + COLUMNS_SQL!: string PRIMARY_KEYS_SQL = ` select tc.table_schema, tc.table_name, kc.column_name as primary_key @@ -168,6 +173,18 @@ module PostgresModule { } this.client = this.pool + this.setSchema() + + } + + setSchema() { + if (!this.config.schema) { + this.config.schema = 'public' + } + this.client.on('connect', (client: any) => { + client.query(`SET search_path TO ${this.config.schema}`); + }); + this.COLUMNS_SQL = `select * from information_schema.columns where table_schema = '${this.config.schema}'` } /** From 60f8bc0450a1a7d0be3a7104024dfae424a0471b Mon Sep 17 00:00:00 2001 From: Rory Powell Date: Wed, 27 Oct 2021 17:21:29 +0100 Subject: [PATCH 2/3] Update tests --- packages/server/__mocks__/pg.ts | 3 +++ packages/server/src/integrations/tests/postgres.spec.js | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/packages/server/__mocks__/pg.ts b/packages/server/__mocks__/pg.ts index 0ec823ea39..ba7803f9a8 100644 --- a/packages/server/__mocks__/pg.ts +++ b/packages/server/__mocks__/pg.ts @@ -21,15 +21,18 @@ module PgMock { function Pool() { } + const on = jest.fn() Pool.prototype.query = query Pool.prototype.connect = jest.fn(() => { // @ts-ignore return new Client() }) + Pool.prototype.on = on pg.Client = Client pg.Pool = Pool pg.queryMock = query + pg.on = on module.exports = pg } diff --git a/packages/server/src/integrations/tests/postgres.spec.js b/packages/server/src/integrations/tests/postgres.spec.js index 4ce5f12e96..5c0d086ce0 100644 --- a/packages/server/src/integrations/tests/postgres.spec.js +++ b/packages/server/src/integrations/tests/postgres.spec.js @@ -15,6 +15,10 @@ describe("Postgres Integration", () => { config = new TestConfiguration() }) + it("calls the connection callback", async () => { + expect(pg.on).toHaveBeenCalledWith('connect', expect.anything()) + }) + it("calls the create method with the correct params", async () => { const sql = "insert into users (name, age) values ('Joe', 123);" await config.integration.create({ From 5b06aa082cc29acf5b30e6dd0b47f6b44067f6cf Mon Sep 17 00:00:00 2001 From: Rory Powell Date: Wed, 27 Oct 2021 17:23:52 +0100 Subject: [PATCH 3/3] Remove whitespace --- packages/server/src/integrations/postgres.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/server/src/integrations/postgres.ts b/packages/server/src/integrations/postgres.ts index 3cf1bdb3e0..237ffc4c69 100644 --- a/packages/server/src/integrations/postgres.ts +++ b/packages/server/src/integrations/postgres.ts @@ -174,7 +174,6 @@ module PostgresModule { this.client = this.pool this.setSchema() - } setSchema() {