Merge pull request #3178 from Budibase/postgres-schema

Postgres: Use another schema other than 'public'
This commit is contained in:
Rory Powell 2021-11-10 10:03:25 +00:00 committed by GitHub
commit edb012fcc7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 2 deletions

View File

@ -21,15 +21,18 @@ module PgMock {
function Pool() { function Pool() {
} }
const on = jest.fn()
Pool.prototype.query = query Pool.prototype.query = query
Pool.prototype.connect = jest.fn(() => { Pool.prototype.connect = jest.fn(() => {
// @ts-ignore // @ts-ignore
return new Client() return new Client()
}) })
Pool.prototype.on = on
pg.Client = Client pg.Client = Client
pg.Pool = Pool pg.Pool = Pool
pg.queryMock = query pg.queryMock = query
pg.on = on
module.exports = pg module.exports = pg
} }

View File

@ -28,6 +28,7 @@ module PostgresModule {
database: string database: string
user: string user: string
password: string password: string
schema: string
ssl?: boolean ssl?: boolean
ca?: string ca?: string
rejectUnauthorized?: boolean rejectUnauthorized?: boolean
@ -65,6 +66,11 @@ module PostgresModule {
default: "root", default: "root",
required: true, required: true,
}, },
schema: {
type: DatasourceFieldTypes.STRING,
default: "public",
required: true,
},
ssl: { ssl: {
type: DatasourceFieldTypes.BOOLEAN, type: DatasourceFieldTypes.BOOLEAN,
default: false, default: false,
@ -124,8 +130,7 @@ module PostgresModule {
public tables: Record<string, Table> = {} public tables: Record<string, Table> = {}
public schemaErrors: Record<string, string> = {} public schemaErrors: Record<string, string> = {}
COLUMNS_SQL = COLUMNS_SQL!: string
"select * from information_schema.columns where not table_schema = 'information_schema' and not table_schema = 'pg_catalog'"
PRIMARY_KEYS_SQL = ` PRIMARY_KEYS_SQL = `
select tc.table_schema, tc.table_name, kc.column_name as primary_key select tc.table_schema, tc.table_name, kc.column_name as primary_key
@ -155,6 +160,17 @@ module PostgresModule {
} }
this.client = this.pool 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}'`
} }
/** /**

View File

@ -15,6 +15,10 @@ describe("Postgres Integration", () => {
config = new TestConfiguration() 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 () => { it("calls the create method with the correct params", async () => {
const sql = "insert into users (name, age) values ('Joe', 123);" const sql = "insert into users (name, age) values ('Joe', 123);"
await config.integration.create({ await config.integration.create({