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

Commit e2d17106 by Jason Jung Committed by Matt Broadstone

transaction fixes + findOrCreate test fix

1 parent 274beb7e
...@@ -183,13 +183,16 @@ ConnectionManager.prototype.getConnection = function(options) { ...@@ -183,13 +183,16 @@ ConnectionManager.prototype.getConnection = function(options) {
var self = this; var self = this;
options = options || {}; options = options || {};
self.pool.isTransaction = options.transaction; self.pool.isTransaction = options.transaction;
//TODO: dialect check
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
self.pool.acquire(function(err, connection) { resolve(self.$connect(self.config));
if (err) return reject(err);
resolve(connection);
}, options.priority, options.type);
}); });
// return new Promise(function (resolve, reject) {
// self.pool.acquire(function(err, connection) {
// if (err) return reject(err);
// resolve(connection);
// }, options.priority, options.type);
// });
}; };
ConnectionManager.prototype.releaseConnection = function(connection) { ConnectionManager.prototype.releaseConnection = function(connection) {
var self = this; var self = this;
......
...@@ -47,7 +47,10 @@ ConnectionManager.prototype.connect = function(config, isTransaction) { ...@@ -47,7 +47,10 @@ ConnectionManager.prototype.connect = function(config, isTransaction) {
config: config, config: config,
lib: self.lib lib: self.lib
}; };
var conn; var conn;
self.lib._transaction = null;
if (isTransaction) { if (isTransaction) {
conn = new self.lib.Connection(config, function (err){ conn = new self.lib.Connection(config, function (err){
var trans = new self.lib.Transaction(conn); var trans = new self.lib.Transaction(conn);
......
...@@ -35,27 +35,15 @@ module.exports = (function() { ...@@ -35,27 +35,15 @@ module.exports = (function() {
var promise = new Utils.Promise(function(resolve, reject) { var promise = new Utils.Promise(function(resolve, reject) {
//console.log(self.sql); if (!sql) {
resolve();
// self return promise;
// .connection }
// .lib
// .step('',{ query: self.sql })
// .error(function (err) {
// console.log('err:', err);
// err.sql = sql;
// reject(self.formatError(err));
// })
// .end(function (result) {
// console.log('res', result.query);
// resolve(self.formatResults(result.query));
// });
// });
var request, transCommand; var request, transCommand;
if (self.connection._transaction) { if (self.connection.lib._transaction) {
request = new self.connection.lib.Request(self.connection.context._transaction); request = new self.connection.lib.Request(self.connection.lib._transaction);
if (self.sql === 'COMMIT TRANSACTION;') { if (self.sql === 'COMMIT TRANSACTION;') {
transCommand = 'commit'; transCommand = 'commit';
...@@ -65,7 +53,7 @@ module.exports = (function() { ...@@ -65,7 +53,7 @@ module.exports = (function() {
if (self.sql === 'COMMIT TRANSACTION;' || if (self.sql === 'COMMIT TRANSACTION;' ||
self.sql === 'ROLLBACK TRANSACTION;') { self.sql === 'ROLLBACK TRANSACTION;') {
self.connection.context._transaction[transCommand](function (err, result) { self.connection.lib._transaction[transCommand](function (err, result) {
if (err) { if (err) {
console.log(err.message); console.log(err.message);
reject(self.formatError(err)); reject(self.formatError(err));
...@@ -73,6 +61,7 @@ module.exports = (function() { ...@@ -73,6 +61,7 @@ module.exports = (function() {
resolve(self.formatResults(result)); resolve(self.formatResults(result));
} }
}); });
return promise; return promise;
} }
} else { } else {
...@@ -80,7 +69,6 @@ module.exports = (function() { ...@@ -80,7 +69,6 @@ module.exports = (function() {
} }
request.query(self.sql, function(err, recordset) { request.query(self.sql, function(err, recordset) {
promise.emit('sql', self.sql, self.connection.uuid);
if(err){ if(err){
console.log(err.message); console.log(err.message);
reject(self.formatError(err)); reject(self.formatError(err));
...@@ -89,6 +77,7 @@ module.exports = (function() { ...@@ -89,6 +77,7 @@ module.exports = (function() {
} }
}); });
}); });
return promise; return promise;
}; };
......
...@@ -113,10 +113,10 @@ Transaction.prototype.rollback = function() { ...@@ -113,10 +113,10 @@ Transaction.prototype.rollback = function() {
Transaction.prototype.prepareEnvironment = function() { Transaction.prototype.prepareEnvironment = function() {
var self = this; var self = this;
var conn = self.options.transaction ? self.options.transaction.connection : self.sequelize.connectionManager.getConnection({ uuid: self.id, transaction: true });
return Utils.Promise.resolve( return Utils.Promise.resolve(conn)
self.options.transaction ? self.options.transaction.connection : self.sequelize.connectionManager.getConnection({ uuid: self.id, transaction: true }) .then(function (connection) {
).then(function (connection) {
self.connection = connection; self.connection = connection;
self.connection.uuid = self.id; self.connection.uuid = self.id;
}) })
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!