不要怂,就是干,撸起袖子干!

Commit 546b90be by Mick Hansen

refactor(connections): fix a few weird bugs and tests

1 parent 2c0416d4
...@@ -42,6 +42,10 @@ ConnectionManager = function(sequelize) { ...@@ -42,6 +42,10 @@ ConnectionManager = function(sequelize) {
var reads = 0 var reads = 0
, writes = 0; , writes = 0;
if (!Array.isArray(config.replication.read)) {
config.replication.read = [config.replication.read];
}
// Make sure we don't modify the existing config object (user might re-use it) // Make sure we don't modify the existing config object (user might re-use it)
config.replication.write = _.extend({}, config.replication.write); config.replication.write = _.extend({}, config.replication.write);
config.replication.read = config.replication.read.map(function (read) { config.replication.read = config.replication.read.map(function (read) {
...@@ -89,14 +93,16 @@ ConnectionManager = function(sequelize) { ...@@ -89,14 +93,16 @@ ConnectionManager = function(sequelize) {
}, },
read: Pooling.Pool({ read: Pooling.Pool({
name: 'sequelize-connection-read', name: 'sequelize-connection-read',
create: function(done) { create: function(callback) {
if (reads >= config.replication.read.length) { if (reads >= config.replication.read.length) {
reads = 0; reads = 0;
} }
// Simple round robin config // Simple round robin config
self.$connect(config.replication.read[reads++]).then(function (connection) { self.$connect(config.replication.read[reads++]).tap(function (connection) {
connection.queryType = 'read'; connection.queryType = 'read';
}).nodeify(done); }).nodeify(function (err, connection) {
callback(err, connection); // For some reason this is needed, else generic-pool things err is a connection or some shit
});
}, },
destroy: function(connection) { destroy: function(connection) {
self.$disconnect(connection); self.$disconnect(connection);
...@@ -108,10 +114,12 @@ ConnectionManager = function(sequelize) { ...@@ -108,10 +114,12 @@ ConnectionManager = function(sequelize) {
}), }),
write: Pooling.Pool({ write: Pooling.Pool({
name: 'sequelize-connection-write', name: 'sequelize-connection-write',
create: function(done) { create: function(callback) {
self.$connect(config.replication.write).then(function (connection) { self.$connect(config.replication.write).tap(function (connection) {
connection.queryType = 'write'; connection.queryType = 'write';
}).nodeify(done); }).nodeify(function (err, connection) {
callback(err, connection); // For some reason this is needed, else generic-pool things err is a connection or some shit
});
}, },
destroy: function(connection) { destroy: function(connection) {
self.$disconnect(connection); self.$disconnect(connection);
...@@ -125,8 +133,10 @@ ConnectionManager = function(sequelize) { ...@@ -125,8 +133,10 @@ ConnectionManager = function(sequelize) {
} else { } else {
this.pool = Pooling.Pool({ this.pool = Pooling.Pool({
name: 'sequelize-connection', name: 'sequelize-connection',
create: function(done) { create: function(callback) {
self.$connect(config).nodeify(done); self.$connect(config).nodeify(function (err, connection) {
callback(err, connection); // For some reason this is needed, else generic-pool things err is a connection or some shit
});
}, },
destroy: function(connection) { destroy: function(connection) {
self.$disconnect(connection); self.$disconnect(connection);
......
...@@ -4,6 +4,7 @@ var ConnectionManager ...@@ -4,6 +4,7 @@ var ConnectionManager
ConnectionManager = function(dialect, sequelize) { ConnectionManager = function(dialect, sequelize) {
this.sequelize = sequelize; this.sequelize = sequelize;
this.sequelize.config.port = this.sequelize.config.port || 5432;
try { try {
this.lib = sequelize.config.native ? require(sequelize.config.dialectModulePath || 'pg').native : require(sequelize.config.dialectModulePath || 'pg'); this.lib = sequelize.config.native ? require(sequelize.config.dialectModulePath || 'pg').native : require(sequelize.config.dialectModulePath || 'pg');
} catch (err) { } catch (err) {
...@@ -13,6 +14,7 @@ ConnectionManager = function(dialect, sequelize) { ...@@ -13,6 +14,7 @@ ConnectionManager = function(dialect, sequelize) {
ConnectionManager.prototype.connect = function(config) { ConnectionManager.prototype.connect = function(config) {
var self = this; var self = this;
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
var connectionString = self.sequelize.getQueryInterface().QueryGenerator.databaseConnectionUri(config) var connectionString = self.sequelize.getQueryInterface().QueryGenerator.databaseConnectionUri(config)
, connection = new self.lib.Client(connectionString) , connection = new self.lib.Client(connectionString)
......
...@@ -74,7 +74,7 @@ describe(Support.getTestDialectTeaser("Configuration"), function() { ...@@ -74,7 +74,7 @@ describe(Support.getTestDialectTeaser("Configuration"), function() {
var sequelize = new Sequelize('dbname', 'root', 'pass', { var sequelize = new Sequelize('dbname', 'root', 'pass', {
dialect: dialect dialect: dialect
}) })
, config = sequelize.connectorManager.config , config = sequelize.config
, port , port
if (Support.dialectIsMySQL()) { if (Support.dialectIsMySQL()) {
......
...@@ -91,6 +91,7 @@ describe(Support.getTestDialectTeaser("Sequelize"), function () { ...@@ -91,6 +91,7 @@ describe(Support.getTestDialectTeaser("Sequelize"), function () {
}) })
it('triggers the actual adapter error', function(done) { it('triggers the actual adapter error', function(done) {
this this
.sequelizeWithInvalidConnection .sequelizeWithInvalidConnection
.authenticate() .authenticate()
...@@ -100,14 +101,16 @@ describe(Support.getTestDialectTeaser("Sequelize"), function () { ...@@ -100,14 +101,16 @@ describe(Support.getTestDialectTeaser("Sequelize"), function () {
} else if (dialect === 'postgres') { } else if (dialect === 'postgres') {
// When the test is run with only it produces: // When the test is run with only it produces:
// Error: Error: Failed to authenticate for PostgresSQL. Please double check your settings. // Error: Error: Failed to authenticate for PostgresSQL. Please double check your settings.
expect(err.message).to.match(/Failed to authenticate for PostgresSQL/)
// When its run with all the other tests it produces: // When its run with all the other tests it produces:
// Error: invalid port number: "99999" // Error: invalid port number: "99999"
expect(err.message).to.match(/invalid port number/) //expect(err.message).to.match(/invalid port number/)
} else { } else {
expect(err.message).to.match(/Failed to authenticate/) expect(err.message).to.match(/Failed to authenticate/)
} }
done() done()
}) })
}) })
}) })
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!