Skip to content

Upgrade to pg@7. #296

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 1 addition & 33 deletions lib/postgresql.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,38 +50,6 @@ exports.initialize = function initializeDataSource(dataSource, callback) {
}
};

// there is a bug in the generic pool where it will not recreate
// destroyed workers (even if there is waiting work to do) unless
// there is a min specified. Make sure we keep some connections
// SEE: https://github.com/coopernurse/node-pool/pull/186
// SEE: https://github.com/brianc/node-pg-pool/issues/48
// SEE: https://github.com/strongloop/loopback-connector-postgresql/issues/231
function _ensureMinimum() {
var i, diff, waiting;
if (this._draining) return;
waiting = this._waitingClients.size();
if (this._factory.min > 0) { // we have positive specified minimum
diff = this._factory.min - this._count;
} else if (waiting > 0) { // we have no minimum, but we do have work to do
diff = Math.min(waiting, this._factory.max - this._count);
}
for (i = 0; i < diff; i++) {
this._createResource();
}
};
function makePool(options) {
var pg = new postgresql.Pool(options);
// Monkey patch to ensure we always finish our work
// - There is a bug where callbacks go uncalled if min is not set
// - We might still not want a connection to *always* exist
// - but we do want to create up to max connections if we have work
// - still waiting
// This should be safe till the version of pg-pool is upgraded
// SEE: https://github.com/coopernurse/node-pool/pull/186
pg.pool._ensureMinimum = _ensureMinimum;
return pg;
}

/**
* PostgreSQL connector constructor
*
Expand Down Expand Up @@ -110,7 +78,7 @@ function PostgreSQL(postgresql, settings) {
this.clientConfig.connectionString = settings.url;
}
this.clientConfig.Promise = Promise;
this.pg = makePool(this.clientConfig);
this.pg = new postgresql.Pool(this.clientConfig);

this.settings = settings;
debug('Settings %j', settings);
Expand Down
7 changes: 1 addition & 6 deletions lib/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,7 @@ function mixinTransaction(PostgreSQL) {
connection.autorelease(err);
connection.autorelease = null;
} else {
var pool = this.pg;
if (err) {
pool.pool.destroy(connection);
} else {
pool.pool.release(connection);
}
connection.release();
}
};
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"bluebird": "^3.4.6",
"debug": "^2.1.1",
"loopback-connector": "^4.2.2",
"pg": "^6.0.0",
"pg": "^7.0.0",
"strong-globalize": "^2.6.2",
"uuid": "^3.0.1"
},
Expand Down
17 changes: 8 additions & 9 deletions test/postgresql.initialization.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ function newConfig(withURL) {
describe('initialization', function() {
it('honours user-defined pg-pool settings', function() {
var dataSource = new DataSource(connector, newConfig());
var pool = dataSource.connector.pg.pool;
pool._factory.max.should.not.equal(999);
var pool = dataSource.connector.pg;
pool.options.max.should.not.equal(999);

var settings = newConfig();
settings.max = 999; // non-default value
var dataSource = new DataSource(connector, settings);
var pool = dataSource.connector.pg.pool;
pool._factory.max.should.equal(999);
var pool = dataSource.connector.pg;
pool.options.max.should.equal(999);
});

it('honours user-defined url settings', function() {
Expand All @@ -45,8 +45,8 @@ describe('initialization', function() {
var urlOnly = {url: newConfig(true).url, max: 999};

var dataSource = new DataSource(connector, urlOnly);
var pool = dataSource.connector.pg.pool;
pool._factory.max.should.equal(999);
var pool = dataSource.connector.pg;
pool.options.max.should.equal(999);

var clientConfig = dataSource.connector.clientConfig;
clientConfig.connectionString.should.equal(urlOnly.url);
Expand All @@ -57,9 +57,8 @@ describe('postgresql connector errors', function() {
it('Should complete these 4 queries without dying', function(done) {
var dataSource = getDataSource();
var db = dataSource.connector;
var pool = db.pg.pool;
pool._factory.max = 5;
pool._factory.min = null;
var pool = db.pg;
pool.options.max = 5;
var errors = 0;
var shouldGet = 0;
function runErrorQuery() {
Expand Down