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

You need to sign in or sign up before continuing.
Commit d4e2cd22 by Sascha Depold

transactions for find / findAll

1 parent 9248772e
...@@ -352,7 +352,7 @@ module.exports = (function() { ...@@ -352,7 +352,7 @@ module.exports = (function() {
return this.QueryInterface.select(this, this.tableName, options, Utils._.defaults({ return this.QueryInterface.select(this, this.tableName, options, Utils._.defaults({
type: 'SELECT', type: 'SELECT',
hasJoin: hasJoin hasJoin: hasJoin
}, queryOptions)) }, queryOptions, { transaction: options.transaction }))
} }
//right now, the caller (has-many-double-linked) is in charge of the where clause //right now, the caller (has-many-double-linked) is in charge of the where clause
...@@ -589,6 +589,8 @@ module.exports = (function() { ...@@ -589,6 +589,8 @@ module.exports = (function() {
return new Utils.CustomEventEmitter(function (emitter) { return new Utils.CustomEventEmitter(function (emitter) {
self.find({ self.find({
where: params where: params
}, {
transaction: options.transaction
}).success(function (instance) { }).success(function (instance) {
if (instance === null) { if (instance === null) {
for (var attrname in defaults) { for (var attrname in defaults) {
......
...@@ -406,7 +406,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -406,7 +406,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}) })
describe('findOrCreate', function () { describe('findOrCreate', function () {
it("Returns instance if already existent. Single find field.", function(done) { it("returns instance if already existent. Single find field.", function(done) {
var self = this, var self = this,
data = { data = {
username: 'Username' username: 'Username'
...@@ -461,20 +461,28 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -461,20 +461,28 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
it("supports transactions", function(done) { it("supports transactions", function(done) {
var self = this var self = this
this.sequelize.transaction(function(t) { Support.prepareTransactionTest(dialect, this.sequelize, function(sequelize) {
self.User.findOrCreate({ username: 'Username' }, { data: 'some data' }, { transaction: t }).complete(function(err) { var User = sequelize.define('user_with_transaction', { username: Sequelize.STRING, data: Sequelize.STRING })
expect(err).to.be.null
self.User.count().success(function(count) { User
expect(count).to.equal(0) .sync({ force: true })
t.commit().success(function() { .success(function() {
self.User.count().success(function(count) { sequelize.transaction(function(t) {
expect(count).to.equal(1) User.findOrCreate({ username: 'Username' }, { data: 'some data' }, { transaction: t }).complete(function(err) {
done() expect(err).to.be.null
User.count().success(function(count) {
expect(count).to.equal(0)
t.commit().success(function() {
User.count().success(function(count) {
expect(count).to.equal(1)
done()
})
})
})
}) })
}) })
}) })
})
}) })
}) })
}) })
...@@ -483,14 +491,20 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -483,14 +491,20 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
it('supports transactions', function(done) { it('supports transactions', function(done) {
var self = this var self = this
this.sequelize.transaction(function(t) { Support.prepareTransactionTest(dialect, this.sequelize, function(sequelize) {
self.User.create({ username: 'user' }, { transaction: t}).success(function() { var User = sequelize.define('user_with_transaction', { username: Sequelize.STRING })
self.User.count().success(function(count) {
expect(count).to.equal(0) User.sync({ force: true }).success(function() {
t.commit().success(function() { sequelize.transaction(function(t) {
self.User.count().success(function(count) { User.create({ username: 'user' }, { transaction: t}).success(function() {
expect(count).to.equal(1) User.count().success(function(count) {
done() expect(count).to.equal(0)
t.commit().success(function() {
User.count().success(function(count) {
expect(count).to.equal(1)
done()
})
})
}) })
}) })
}) })
...@@ -1842,6 +1856,29 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -1842,6 +1856,29 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}) })
describe('find', function() { describe('find', function() {
it('supports transactions', function(done) {
Support.prepareTransactionTest(dialect, this.sequelize, function(sequelize) {
var User = sequelize.define('User', { username: Sequelize.STRING })
User.sync({ force: true }).success(function() {
sequelize.transaction(function(t) {
User.create({ username: 'foo' }, { transaction: t }).success(function() {
User.find({ username: 'foo' }).success(function(user1) {
User.find({ username: 'foo' }, { transaction: t }).success(function(user2) {
expect(user1).to.be.null
expect(user2).to.not.be.null
t.rollback().success(function() {
done()
})
})
})
})
})
})
})
})
describe('general / basic function', function() { describe('general / basic function', function() {
beforeEach(function(done) { beforeEach(function(done) {
var self = this var self = this
...@@ -2658,6 +2695,32 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -2658,6 +2695,32 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}) })
describe('findAll', function() { describe('findAll', function() {
it('supports transactions', function(done) {
Support.prepareTransactionTest(dialect, this.sequelize, function(sequelize) {
var User = sequelize.define('User', { username: Sequelize.STRING })
User.sync({ force: true }).success(function() {
sequelize.transaction(function(t) {
User.create({ username: 'foo' }, { transaction: t }).success(function() {
User.findAll({ username: 'foo' }).success(function(users1) {
User.findAll({ transaction: t }).success(function(users2) {
User.findAll({ username: 'foo' }, { transaction: t }).success(function(users3) {
expect(users1.length).to.equal(0)
expect(users2.length).to.equal(1)
expect(users3.length).to.equal(1)
t.rollback().success(function() {
done()
})
})
})
})
})
})
})
})
})
describe('eager loading', function() { describe('eager loading', function() {
describe('belongsTo', function() { describe('belongsTo', function() {
beforeEach(function(done) { beforeEach(function(done) {
......
...@@ -498,37 +498,39 @@ describe(Support.getTestDialectTeaser("Sequelize"), function () { ...@@ -498,37 +498,39 @@ describe(Support.getTestDialectTeaser("Sequelize"), function () {
}) })
describe('transaction', function() { describe('transaction', function() {
if (dialect === 'sqlite') { beforeEach(function(done) {
beforeEach(function() { var self = this
this.sequelize.options.storage = path.join(__dirname, 'tmp', 'db.sqlite')
this.sequelize = new Sequelize(this.sequelize.config.datase, null, null, this.sequelize.options) Support.prepareTransactionTest(dialect, this.sequelize, function(sequelize) {
self.sequelizeWithTransaction = sequelize
done()
}) })
} })
it('is a transaction method available', function() { it('is a transaction method available', function() {
expect(Support.Sequelize).to.respondTo('transaction') expect(Support.Sequelize).to.respondTo('transaction')
}) })
it('passes a transaction object to the callback', function(done) { it('passes a transaction object to the callback', function(done) {
this.sequelize.transaction(function(t) { this.sequelizeWithTransaction.transaction(function(t) {
expect(t).to.be.instanceOf(Transaction) expect(t).to.be.instanceOf(Transaction)
done() done()
}) })
}) })
it('returns a transaction object', function() { it('returns a transaction object', function() {
expect(this.sequelize.transaction(function(){})).to.be.instanceOf(Transaction) expect(this.sequelizeWithTransaction.transaction(function(){})).to.be.instanceOf(Transaction)
}) })
it('allows me to define a callback on the result', function(done) { it('allows me to define a callback on the result', function(done) {
this this
.sequelize .sequelizeWithTransaction
.transaction(function(t) { t.commit() }) .transaction(function(t) { t.commit() })
.done(done) .done(done)
}) })
it('allows me to define a callback on the transaction object', function(done) { it('allows me to define a callback on the transaction object', function(done) {
this.sequelize.transaction(function(t) { this.sequelizeWithTransaction.transaction(function(t) {
t.done(done) t.done(done)
t.commit() t.commit()
}) })
...@@ -536,21 +538,21 @@ describe(Support.getTestDialectTeaser("Sequelize"), function () { ...@@ -536,21 +538,21 @@ describe(Support.getTestDialectTeaser("Sequelize"), function () {
if (dialect === 'sqlite') { if (dialect === 'sqlite') {
it("correctly scopes transaction from other connections", function(done) { it("correctly scopes transaction from other connections", function(done) {
var TransactionTest = this.sequelize.define('TransactionTest', { name: DataTypes.STRING }, { timestamps: false }) var TransactionTest = this.sequelizeWithTransaction.define('TransactionTest', { name: DataTypes.STRING }, { timestamps: false })
, self = this , self = this
var count = function(transaction, callback) { var count = function(transaction, callback) {
var sql = self.sequelize.getQueryInterface().QueryGenerator.selectQuery('TransactionTests', { attributes: [['count(*)', 'cnt']] }) var sql = self.sequelizeWithTransaction.getQueryInterface().QueryGenerator.selectQuery('TransactionTests', { attributes: [['count(*)', 'cnt']] })
self self
.sequelize .sequelizeWithTransaction
.query(sql, null, { plain: true, raw: true, transaction: transaction }) .query(sql, null, { plain: true, raw: true, transaction: transaction })
.success(function(result) { callback(result.cnt) }) .success(function(result) { callback(result.cnt) })
} }
TransactionTest.sync({ force: true }).success(function() { TransactionTest.sync({ force: true }).success(function() {
self.sequelize.transaction(function(t1) { self.sequelizeWithTransaction.transaction(function(t1) {
self.sequelize.query('INSERT INTO ' + qq('TransactionTests') + ' (' + qq('name') + ') VALUES (\'foo\');', null, { plain: true, raw: true, transaction: t1 }).success(function() { self.sequelizeWithTransaction.query('INSERT INTO ' + qq('TransactionTests') + ' (' + qq('name') + ') VALUES (\'foo\');', null, { plain: true, raw: true, transaction: t1 }).success(function() {
count(null, function(cnt) { count(null, function(cnt) {
expect(cnt).to.equal(0) expect(cnt).to.equal(0)
...@@ -571,23 +573,23 @@ describe(Support.getTestDialectTeaser("Sequelize"), function () { ...@@ -571,23 +573,23 @@ describe(Support.getTestDialectTeaser("Sequelize"), function () {
}) })
} else { } else {
it("correctly handles multiple transactions", function(done) { it("correctly handles multiple transactions", function(done) {
var TransactionTest = this.sequelize.define('TransactionTest', { name: DataTypes.STRING }, { timestamps: false }) var TransactionTest = this.sequelizeWithTransaction.define('TransactionTest', { name: DataTypes.STRING }, { timestamps: false })
, self = this , self = this
var count = function(transaction, callback) { var count = function(transaction, callback) {
var sql = self.sequelize.getQueryInterface().QueryGenerator.selectQuery('TransactionTests', { attributes: [['count(*)', 'cnt']] }) var sql = self.sequelizeWithTransaction.getQueryInterface().QueryGenerator.selectQuery('TransactionTests', { attributes: [['count(*)', 'cnt']] })
self self
.sequelize .sequelizeWithTransaction
.query(sql, null, { plain: true, raw: true, transaction: transaction }) .query(sql, null, { plain: true, raw: true, transaction: transaction })
.success(function(result) { callback(parseInt(result.cnt, 10)) }) .success(function(result) { callback(parseInt(result.cnt, 10)) })
} }
TransactionTest.sync({ force: true }).success(function() { TransactionTest.sync({ force: true }).success(function() {
self.sequelize.transaction(function(t1) { self.sequelizeWithTransaction.transaction(function(t1) {
self.sequelize.query('INSERT INTO ' + qq('TransactionTests') + ' (' + qq('name') + ') VALUES (\'foo\');', null, { plain: true, raw: true, transaction: t1 }).success(function() { self.sequelizeWithTransaction.query('INSERT INTO ' + qq('TransactionTests') + ' (' + qq('name') + ') VALUES (\'foo\');', null, { plain: true, raw: true, transaction: t1 }).success(function() {
self.sequelize.transaction(function(t2) { self.sequelizeWithTransaction.transaction(function(t2) {
self.sequelize.query('INSERT INTO ' + qq('TransactionTests') + ' (' + qq('name') + ') VALUES (\'bar\');', null, { plain: true, raw: true, transaction: t2 }).success(function() { self.sequelizeWithTransaction.query('INSERT INTO ' + qq('TransactionTests') + ' (' + qq('name') + ') VALUES (\'bar\');', null, { plain: true, raw: true, transaction: t2 }).success(function() {
count(null, function(cnt) { count(null, function(cnt) {
expect(cnt).to.equal(0) expect(cnt).to.equal(0)
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!