Support multiple searchPaths while preserving case-sensitive feature … (#2340)

* Support multiple searchPaths while preserving case-sensitive feature (by using Array syntax). Fixes #2335

* ESLint

* Added changelog text
This commit is contained in:
wubzz 2017-11-18 17:09:05 +01:00 committed by GitHub
parent e405d669a6
commit 95e5cf8e1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 70 additions and 3 deletions

View File

@ -1,5 +1,8 @@
# Master (Unreleased)
### Bug fixes:
- Fix support for multiple schema names in in postgres `searchPath` #2340
# 0.14.0 - 6 Nov, 2017

View File

@ -1,10 +1,11 @@
// PostgreSQL
// -------
import { assign, map, extend } from 'lodash'
import { assign, map, extend, isArray, isString, includes } from 'lodash'
import inherits from 'inherits';
import Client from '../../client';
import Promise from 'bluebird';
import {warn} from '../../helpers';
import QueryCompiler from './query/compiler';
import ColumnCompiler from './schema/columncompiler';
@ -159,12 +160,30 @@ assign(Client_PG.prototype, {
},
setSchemaSearchPath(connection, searchPath) {
const path = (searchPath || this.searchPath);
let path = (searchPath || this.searchPath);
if (!path) return Promise.resolve(true);
if(!isArray(path) && !isString(path)) {
throw new TypeError(`knex: Expected searchPath to be Array/String, got: ${typeof path}`);
}
if(isString(path)) {
if(includes(path, ',')) {
const parts = path.split(',');
const arraySyntax = `[${map(parts, (searchPath) => `'${searchPath}'`).join(', ')}]`;
warn(
`Detected comma in searchPath "${path}".`
+
`If you are trying to specify multiple schemas, use Array syntax: ${arraySyntax}`);
}
path = [path];
}
path = map(path, (schemaName) => `"${schemaName}"`).join(',');
return new Promise(function(resolver, rejecter) {
connection.query(`set search_path to "${path}"`, function(err) {
connection.query(`set search_path to ${path}`, function(err) {
if (err) return rejecter(err);
resolver(true);
});

View File

@ -46,6 +46,10 @@ if (config.oracledb) {
});
}
if(config.postgres) {
require('./unit/dialects/postgres');
}
describe('Docker Integration Tests', function() {
this.timeout(process.env.KNEX_TEST_TIMEOUT || 15000);
require('./docker')

View File

@ -0,0 +1,41 @@
var knex = require('../../../knex');
describe("Postgres Unit Tests", function() {
it('Validates searchPath as Array/String', function() {
var knexInstance = knex({
client: 'pg',
});
expect(function() {
knexInstance.client.setSchemaSearchPath(null, {});
}).to.throw(TypeError);
expect(function() {
knexInstance.client.setSchemaSearchPath(null, 4);
}).to.throw(TypeError);
var fakeQueryFn = function(expectedSearchPath) {
return {
query: function(sql, callback) {
try {
expect(sql).to.equal('set search_path to ' + expectedSearchPath);
callback(null);
} catch(error) {
callback(error);
}
}
};
};
return knexInstance.client.setSchemaSearchPath(fakeQueryFn('"public,knex"'), 'public,knex')
.then(function() {
return knexInstance.client.setSchemaSearchPath(fakeQueryFn('"public","knex"'), ['public', 'knex']);
})
.then(function() {
return knexInstance.client.setSchemaSearchPath(fakeQueryFn('"public"'), 'public')
});
});
});