Skip to content

Commit

Permalink
Merge pull request #506 from vitaly-t/schema
Browse files Browse the repository at this point in the history
Schema
  • Loading branch information
vitaly-t committed Apr 17, 2018
2 parents a550a0e + 1756a8e commit 892bd52
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 5 deletions.
1 change: 1 addition & 0 deletions .github/CHANGELOG.md
@@ -1,5 +1,6 @@
### Release History

* 8.3.0 Adding initialization option `schema` for dynamic schema change. Released: April 17, 2018.
* 8.2.0 Adding [Conditional Tasks], plus query callbacks. Released: March 10, 2018.
* 8.1.0 Adding new method [utils.taskArgs]. Released: Feb 26, 2018
* 8.0.0 Major changes for task and transaction interfaces. Released: Feb 24, 2018
Expand Down
3 changes: 2 additions & 1 deletion jsdoc/fixLinks.js
Expand Up @@ -37,7 +37,8 @@ const links = {
'Long Stack Traces': 'http://bluebirdjs.com/docs/api/promise.config.html',
'Symbol': 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol',
'Library de-initialization': 'https://github.com/vitaly-t/pg-promise#library-de-initialization',
'Nested Transactions': 'https://github.com/vitaly-t/pg-promise#nested-transactions'
'Nested Transactions': 'https://github.com/vitaly-t/pg-promise#nested-transactions',
'changing the database or the role': 'https://stackoverflow.com/questions/2875610/permanently-set-postgresql-schema-path'
};

function fixLinks(source) {
Expand Down
12 changes: 11 additions & 1 deletion lib/connect.js
Expand Up @@ -13,7 +13,8 @@ const npm = {
con: require('manakin').local,
utils: require('./utils'),
events: require('./events'),
text: require('./text')
text: require('./text'),
formatting: require('./formatting')
};

function poolConnect(ctx, db, config) {
Expand All @@ -40,6 +41,7 @@ function poolConnect(ctx, db, config) {
if (isFresh) {
npm.utils.addReadProp(client, '$used', true, true);
hidePassword(client); // See: https://github.com/brianc/node-postgres/issues/1568
setSchema(client, ctx);
}
setCtx(client, ctx);
const end = lockClientEnd(client);
Expand Down Expand Up @@ -71,6 +73,7 @@ function directConnect(ctx, config) {
reject(err);
} else {
hidePassword(client); // See: https://github.com/brianc/node-postgres/issues/1568
setSchema(client, ctx);
setCtx(client, ctx);
const end = lockClientEnd(client);
client.on('error', onError);
Expand Down Expand Up @@ -144,6 +147,13 @@ function hideProperty(obj, prop) {
Object.defineProperty(obj, prop, {value: obj[prop], enumerable: false});
}

function setSchema(client, ctx) {
const s = ctx.options.schema;
if (s) {
client.query(npm.formatting.as.format('SET search_path TO $1:name', [s]));
}
}

module.exports = config => ({
pool: (ctx, db) => poolConnect(ctx, db, config),
direct: ctx => directConnect(ctx, config)
Expand Down
14 changes: 13 additions & 1 deletion lib/main.js
Expand Up @@ -107,6 +107,18 @@ const npm = {
*
* This option is dynamic (can be set before or after initialization).
*
* @param {string|Array<string>} [options.schema]
* **Added in v8.3.0**
*
* Forces change of the default database schema(s) for every fresh connection, i.e.
* the library will execute `SET search_path TO schema_1, schema_2, ...` in the background
* whenever a fresh physical connection is allocated.
*
* Normally, one changes the default schema(s) by $[changing the database or the role], but sometimes you
* may want to switch the default schema(s) without persisting the change, and use this option instead.
*
* This option is dynamic (can be set before or after initialization).
*
* @param {boolean} [options.noWarnings=false]
* Disables all diagnostic warnings in the library (it is ill-advised).
*
Expand Down Expand Up @@ -177,7 +189,7 @@ function $main(options) {

// list of supported initialization options:
const validOptions = ['pgFormatting', 'pgNative', 'promiseLib', 'noLocking', 'capSQL', 'noWarnings',
'connect', 'disconnect', 'query', 'receive', 'task', 'transact', 'error', 'extend'];
'connect', 'disconnect', 'query', 'receive', 'task', 'transact', 'error', 'extend', 'schema'];

if (!options.noWarnings) {
for (const prop in options) {
Expand Down
4 changes: 2 additions & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "pg-promise",
"version": "8.2.3",
"version": "8.3.0",
"description": "Promises interface for PostgreSQL",
"main": "lib/index.js",
"typings": "typescript/pg-promise.d.ts",
Expand Down Expand Up @@ -46,7 +46,7 @@
"spex": "2.0.2"
},
"devDependencies": {
"@types/node": "9.6.2",
"@types/node": "9.6.5",
"JSONStream": "1.3.2",
"bluebird": "3.5.1",
"coveralls": "3.0.0",
Expand Down
32 changes: 32 additions & 0 deletions test/dbSpec.js
Expand Up @@ -2266,6 +2266,38 @@ describe('Multi-result queries', () => {
});
});

describe('Dynamic Schema', () => {
describe('for a single schema', () => {
let result;
beforeEach(done => {
const innerDb = header({schema: 'test', noWarnings: true, promiseLib: promise}).db;
innerDb.any('select * from users')
.catch(error => {
result = error;
})
.finally(done);
});
it('must change the default schema', () => {
expect(result && result.message).toBe('relation "users" does not exist');
});
});
describe('for multiple schemas', () => {
let result;
beforeEach(done => {
const innerDb = header({schema: ['first', 'second', 'public'], noWarnings: true, promiseLib: promise}).db;
innerDb.any('select * from users')
.then(data => {
result = data;
})
.finally(done);
});
it('must change the default schemas', () => {
expect(result && result.length).toBeGreaterThan(0);
});
});

});

if (jasmine.Runner) {
const _finishCallback = jasmine.Runner.prototype.finishCallback;
jasmine.Runner.prototype.finishCallback = function () {
Expand Down

0 comments on commit 892bd52

Please sign in to comment.