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

Commit 546b90be by Mick Hansen

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

1 parent 2c0416d4
......@@ -42,6 +42,10 @@ ConnectionManager = function(sequelize) {
var reads = 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)
config.replication.write = _.extend({}, config.replication.write);
config.replication.read = config.replication.read.map(function (read) {
......@@ -89,14 +93,16 @@ ConnectionManager = function(sequelize) {
},
read: Pooling.Pool({
name: 'sequelize-connection-read',
create: function(done) {
create: function(callback) {
if (reads >= config.replication.read.length) {
reads = 0;
}
// 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';
}).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) {
self.$disconnect(connection);
......@@ -108,10 +114,12 @@ ConnectionManager = function(sequelize) {
}),
write: Pooling.Pool({
name: 'sequelize-connection-write',
create: function(done) {
self.$connect(config.replication.write).then(function (connection) {
create: function(callback) {
self.$connect(config.replication.write).tap(function (connection) {
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) {
self.$disconnect(connection);
......@@ -125,8 +133,10 @@ ConnectionManager = function(sequelize) {
} else {
this.pool = Pooling.Pool({
name: 'sequelize-connection',
create: function(done) {
self.$connect(config).nodeify(done);
create: function(callback) {
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) {
self.$disconnect(connection);
......
......@@ -4,6 +4,7 @@ var ConnectionManager
ConnectionManager = function(dialect, sequelize) {
this.sequelize = sequelize;
this.sequelize.config.port = this.sequelize.config.port || 5432;
try {
this.lib = sequelize.config.native ? require(sequelize.config.dialectModulePath || 'pg').native : require(sequelize.config.dialectModulePath || 'pg');
} catch (err) {
......@@ -13,6 +14,7 @@ ConnectionManager = function(dialect, sequelize) {
ConnectionManager.prototype.connect = function(config) {
var self = this;
return new Promise(function (resolve, reject) {
var connectionString = self.sequelize.getQueryInterface().QueryGenerator.databaseConnectionUri(config)
, connection = new self.lib.Client(connectionString)
......
......@@ -74,7 +74,7 @@ describe(Support.getTestDialectTeaser("Configuration"), function() {
var sequelize = new Sequelize('dbname', 'root', 'pass', {
dialect: dialect
})
, config = sequelize.connectorManager.config
, config = sequelize.config
, port
if (Support.dialectIsMySQL()) {
......
......@@ -91,6 +91,7 @@ describe(Support.getTestDialectTeaser("Sequelize"), function () {
})
it('triggers the actual adapter error', function(done) {
this
.sequelizeWithInvalidConnection
.authenticate()
......@@ -100,14 +101,16 @@ describe(Support.getTestDialectTeaser("Sequelize"), function () {
} else if (dialect === 'postgres') {
// When the test is run with only it produces:
// 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:
// Error: invalid port number: "99999"
expect(err.message).to.match(/invalid port number/)
//expect(err.message).to.match(/invalid port number/)
} else {
expect(err.message).to.match(/Failed to authenticate/)
}
done()
})
})
})
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!