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

Commit 8512db66 by Mick Hansen

fix transaction error logic

1 parent bc3094a8
...@@ -752,8 +752,10 @@ module.exports = (function() { ...@@ -752,8 +752,10 @@ module.exports = (function() {
return transaction.prepareEnvironment().then(function () { return transaction.prepareEnvironment().then(function () {
wantsError ? callback(null, transaction) : callback && callback(transaction); wantsError ? callback(null, transaction) : callback && callback(transaction);
return transaction;
}).catch(function (err) { }).catch(function (err) {
if (wantsError) callback(err); if (wantsError) callback(err);
else throw err;
}); });
}; };
......
...@@ -69,7 +69,7 @@ Transaction.prototype.commit = function() { ...@@ -69,7 +69,7 @@ Transaction.prototype.commit = function() {
.sequelize .sequelize
.getQueryInterface() .getQueryInterface()
.commitTransaction(this, {}) .commitTransaction(this, {})
.done(this.cleanup.bind(this)); .finally(this.cleanup.bind(this));
}; };
...@@ -83,7 +83,7 @@ Transaction.prototype.rollback = function() { ...@@ -83,7 +83,7 @@ Transaction.prototype.rollback = function() {
.sequelize .sequelize
.getQueryInterface() .getQueryInterface()
.rollbackTransaction(this, {}) .rollbackTransaction(this, {})
.done(this.cleanup.bind(this)); .finally(this.cleanup.bind(this));
}; };
Transaction.prototype.prepareEnvironment = function() { Transaction.prototype.prepareEnvironment = function() {
......
var chai = require('chai') var chai = require('chai')
, expect = chai.expect , expect = chai.expect
, Support = require(__dirname + '/support') , Support = require(__dirname + '/support')
, Promise = require(__dirname + '/../lib/promise')
, Transaction = require(__dirname + '/../lib/transaction') , Transaction = require(__dirname + '/../lib/transaction')
describe(Support.getTestDialectTeaser("Sequelize#transaction"), function () { describe(Support.getTestDialectTeaser("Sequelize#transaction"), function () {
this.timeout(4000) this.timeout(4000);
describe('success', function() { describe('success', function() {
it("gets triggered once a transaction has been successfully committed", function(done) { it("gets triggered once a transaction has been successfully committed", function(done) {
this this
.sequelize .sequelize
.transaction(function(t) { t.commit() }) .transaction(function(t) { t.commit(); })
.success(function() { done() }) .success(function() { done(); });
}) });
it("gets triggered once a transaction has been successfully rollbacked", function(done) { it("gets triggered once a transaction has been successfully rollbacked", function(done) {
this this
.sequelize .sequelize
.transaction(function(t) { t.rollback() }) .transaction(function(t) { t.rollback(); })
.success(function() { done() }) .success(function() { done(); });
}) });
it('works for long running transactions', function(done) { it('works for long running transactions', function(done) {
Support.prepareTransactionTest(this.sequelize, function(sequelize) { Support.prepareTransactionTest(this.sequelize, function(sequelize) {
var User = sequelize.define('User', { var User = sequelize.define('User', {
name: Support.Sequelize.STRING name: Support.Sequelize.STRING
}, { timestamps: false }) }, { timestamps: false });
sequelize.sync({ force: true }).success(function() { return sequelize.sync({ force: true }).success(function() {
sequelize return sequelize.transaction();
.transaction(function(t) { }).then(function (t) {
var query = 'select sleep(2);' expect(t).to.be.ok;
var query = 'select sleep(2);';
switch(Support.getTestDialect()) { switch(Support.getTestDialect()) {
case 'postgres': case 'postgres':
query = 'select pg_sleep(2);' query = 'select pg_sleep(2);';
break break;
case 'sqlite': case 'sqlite':
query = 'select sqlite3_sleep(2);' query = 'select sqlite3_sleep(2);';
break break;
default: default:
break break;
} }
sequelize.query(query, null, { return sequelize.query(query, null, {
raw: true, raw: true,
plain: true, plain: true,
transaction: t transaction: t
}).done(function() { }).then(function() {
var dao = User.build({ name: 'foo' }) var dao = User.build({ name: 'foo' });
// this.QueryGenerator.insertQuery(tableName, values, dao.daoFactory.rawAttributes) // this.QueryGenerator.insertQuery(tableName, values, dao.daoFactory.rawAttributes)
query = sequelize return query = sequelize
.getQueryInterface() .getQueryInterface()
.QueryGenerator .QueryGenerator
.insertQuery(User.tableName, dao.values, User.rawAttributes) .insertQuery(User.tableName, dao.values, User.rawAttributes);
}).then(function () {
setTimeout(function() { return Promise.delay(1000);
sequelize.query(query, null, { }).then(function () {
return sequelize.query(query, null, {
raw: true, raw: true,
plain: true, plain: true,
transaction: t transaction: t
}).done(function(err, res) { });
t.commit() }).then(function () {
}) return t.commit();
}, 1000) }).catch(function (err) {
}) return t.rollback();
}) });
.success(function() { }).then(function() {
User.all().success(function(users) { return User.all().success(function(users) {
expect(users.length).to.equal(1) expect(users.length).to.equal(1);
expect(users[0].name).to.equal('foo') expect(users[0].name).to.equal('foo');
done();
done() });
}) }).catch(done);
}) });
}) });
}) });
})
})
describe('error', function() { describe('error', function() {
if (Support.getTestDialect() === 'sqlite') { if (Support.getTestDialect() === 'sqlite') {
...@@ -86,37 +87,37 @@ describe(Support.getTestDialectTeaser("Sequelize#transaction"), function () { ...@@ -86,37 +87,37 @@ describe(Support.getTestDialectTeaser("Sequelize#transaction"), function () {
// how could we enforce an authentication error in sqlite? // how could we enforce an authentication error in sqlite?
} else { } else {
it("gets triggered once an error occurs", function(done) { it("gets triggered once an error occurs", function(done) {
var sequelize = Support.createSequelizeInstance({ dialect: Support.getTestDialect() }) var sequelize = Support.createSequelizeInstance({ dialect: Support.getTestDialect() });
// lets overwrite the host to get an error // lets overwrite the host to get an error
sequelize.config.username = 'foobarbaz' sequelize.config.username = 'foobarbaz';
sequelize sequelize
.transaction(function() {}) .transaction(function() {})
.error(function(err) { .catch(function(err) {
expect(err).to.not.be.undefined expect(err).to.not.be.undefined;
done() done();
}) });
}) });
} }
}) });
describe('callback', function() { describe('callback', function() {
it("receives the transaction if only one argument is passed", function(done) { it("receives the transaction if only one argument is passed", function(done) {
this.sequelize.transaction(function(t) { this.sequelize.transaction(function(t) {
expect(t).to.be.instanceOf(Transaction) expect(t).to.be.instanceOf(Transaction);
t.commit() t.commit();
}).done(done) }).done(done);
}) });
it("receives an error and the transaction if two arguments are passed", function(done) { it("receives an error and the transaction if two arguments are passed", function(done) {
this.sequelize.transaction(function(err, t) { this.sequelize.transaction(function(err, t) {
expect(err).to.not.be.instanceOf(Transaction) expect(err).to.not.be.instanceOf(Transaction);
expect(t).to.be.instanceOf(Transaction) expect(t).to.be.instanceOf(Transaction);
t.commit() t.commit();
}).done(done) }).done(done);
}) });
}) });
describe('complex long running example', function() { describe('complex long running example', function() {
it("works with promise syntax", function(done) { it("works with promise syntax", function(done) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!