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

Commit 633a6cfa by Sascha Depold

transaction support for destroy

1 parent e2efb566
...@@ -327,7 +327,7 @@ module.exports = (function() { ...@@ -327,7 +327,7 @@ module.exports = (function() {
this.isDirty = isDirty this.isDirty = isDirty
} }
DAO.prototype.destroy = function() { DAO.prototype.destroy = function(options) {
var self = this var self = this
, query = null , query = null
...@@ -340,10 +340,10 @@ module.exports = (function() { ...@@ -340,10 +340,10 @@ module.exports = (function() {
if (self.__options.timestamps && self.__options.paranoid) { if (self.__options.timestamps && self.__options.paranoid) {
var attr = Utils._.underscoredIf(self.__options.deletedAt, self.__options.underscored) var attr = Utils._.underscoredIf(self.__options.deletedAt, self.__options.underscored)
self.dataValues[attr] = new Date() self.dataValues[attr] = new Date()
query = self.save() query = self.save(options)
} else { } else {
var identifier = self.__options.hasPrimaryKeys ? self.primaryKeyValues : { id: self.id }; var identifier = self.__options.hasPrimaryKeys ? self.primaryKeyValues : { id: self.id };
query = self.QueryInterface.delete(self, self.QueryInterface.QueryGenerator.addSchema(self.__factory.tableName, self.__factory.options.schema), identifier) query = self.QueryInterface.delete(self, self.QueryInterface.QueryGenerator.addSchema(self.__factory.tableName, self.__factory.options.schema), identifier, options)
} }
query.on('sql', function(sql) { query.on('sql', function(sql) {
......
...@@ -524,7 +524,7 @@ module.exports = (function() { ...@@ -524,7 +524,7 @@ module.exports = (function() {
}).run() }).run()
} }
QueryInterface.prototype.delete = function(dao, tableName, identifier) { QueryInterface.prototype.delete = function(dao, tableName, identifier, options) {
var self = this var self = this
, restrict = false , restrict = false
, cascades = [] , cascades = []
...@@ -593,7 +593,7 @@ module.exports = (function() { ...@@ -593,7 +593,7 @@ module.exports = (function() {
var chainer = new Utils.QueryChainer() var chainer = new Utils.QueryChainer()
chainer.add(self, 'queryAndEmit', [[sql, dao], 'delete']) chainer.add(self, 'queryAndEmit', [[sql, dao, options], 'delete'])
chainer.runSerially() chainer.runSerially()
.success(function(results){ .success(function(results){
......
...@@ -1448,6 +1448,28 @@ describe(Support.getTestDialectTeaser("DAO"), function () { ...@@ -1448,6 +1448,28 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
}) })
describe('destroy', function() { describe('destroy', function() {
it('supports transactions', function(done) {
Support.prepareTransactionTest(dialect, this.sequelize, function(sequelize) {
var User = sequelize.define('User', { username: Support.Sequelize.STRING })
User.sync({ force: true }).success(function() {
User.create({ username: 'foo' }).success(function(user) {
sequelize.transaction(function(t) {
user.destroy({ transaction: t }).success(function() {
User.count().success(function(count1) {
User.count({ transaction: t }).success(function(count2) {
expect(count1).to.equal(1)
expect(count2).to.equal(0)
t.rollback().success(function() { done() })
})
})
})
})
})
})
})
})
it('deletes a record from the database if dao is not paranoid', function(done) { it('deletes a record from the database if dao is not paranoid', function(done) {
var UserDestroy = this.sequelize.define('UserDestroy', { var UserDestroy = this.sequelize.define('UserDestroy', {
name: Support.Sequelize.STRING, name: Support.Sequelize.STRING,
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!