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

Commit 5e5cdb78 by Jan Aagaard Meier

Fix an off by one error when running hooks in bulk update and delete. Closes #2054

1 parent 09441160
Showing with 20 additions and 20 deletions
...@@ -155,14 +155,14 @@ module.exports = (function() { ...@@ -155,14 +155,14 @@ module.exports = (function() {
} }
Util.inherits(this.DAO, DAO); Util.inherits(this.DAO, DAO);
this._timestampAttributes = {} this._timestampAttributes = {}
if (this.options.timestamps) { if (this.options.timestamps) {
if (this.options.createdAt) { if (this.options.createdAt) {
this._timestampAttributes.createdAt = Utils._.underscoredIf(this.options.createdAt, this.options.underscored) this._timestampAttributes.createdAt = Utils._.underscoredIf(this.options.createdAt, this.options.underscored)
} }
if (this.options.updatedAt) { if (this.options.updatedAt) {
this._timestampAttributes.updatedAt = Utils._.underscoredIf(this.options.updatedAt, this.options.underscored) this._timestampAttributes.updatedAt = Utils._.underscoredIf(this.options.updatedAt, this.options.underscored)
} }
if (this.options.paranoid && this.options.deletedAt) { if (this.options.paranoid && this.options.deletedAt) {
this._timestampAttributes.deletedAt = Utils._.underscoredIf(this.options.deletedAt, this.options.underscored) this._timestampAttributes.deletedAt = Utils._.underscoredIf(this.options.deletedAt, this.options.underscored)
...@@ -603,7 +603,7 @@ module.exports = (function() { ...@@ -603,7 +603,7 @@ module.exports = (function() {
DAOFactory.prototype.count = function(options) { DAOFactory.prototype.count = function(options) {
options = Utils._.clone(options || {}) options = Utils._.clone(options || {})
return new Utils.CustomEventEmitter(function (emitter) { return new Utils.CustomEventEmitter(function (emitter) {
var col = this.sequelize.col('*') var col = this.sequelize.col('*')
if (options.include) { if (options.include) {
...@@ -1030,6 +1030,10 @@ module.exports = (function() { ...@@ -1030,6 +1030,10 @@ module.exports = (function() {
if (options && options.hooks === true) { if (options && options.hooks === true) {
var tick = 0 var tick = 0
var next = function(i) { var next = function(i) {
if (i >= records.length) {
return finished();
}
self.runHooks(self.options.hooks.afterDestroy, records[i], function(err, newValues) { self.runHooks(self.options.hooks.afterDestroy, records[i], function(err, newValues) {
if (!!err) { if (!!err) {
return finished(err) return finished(err)
...@@ -1038,10 +1042,6 @@ module.exports = (function() { ...@@ -1038,10 +1042,6 @@ module.exports = (function() {
records[i].dataValues = !!newValues ? newValues.dataValues : records[i].dataValues records[i].dataValues = !!newValues ? newValues.dataValues : records[i].dataValues
tick++ tick++
if (tick >= records.length) {
return finished()
}
next(tick) next(tick)
}) })
} }
...@@ -1058,6 +1058,10 @@ module.exports = (function() { ...@@ -1058,6 +1058,10 @@ module.exports = (function() {
self.all({where: where}).error(function(err) { emitter.emit('error', err) }) self.all({where: where}).error(function(err) { emitter.emit('error', err) })
.success(function(records) { .success(function(records) {
var next = function(i) { var next = function(i) {
if (i >= records.length) {
return runQuery(null, records)
}
self.runHooks(self.options.hooks.beforeDestroy, records[i], function(err, newValues) { self.runHooks(self.options.hooks.beforeDestroy, records[i], function(err, newValues) {
if (!!err) { if (!!err) {
return runQuery(err) return runQuery(err)
...@@ -1066,10 +1070,6 @@ module.exports = (function() { ...@@ -1066,10 +1070,6 @@ module.exports = (function() {
records[i].dataValues = !!newValues ? newValues.dataValues : records[i].dataValues records[i].dataValues = !!newValues ? newValues.dataValues : records[i].dataValues
tick++ tick++
if (tick >= records.length) {
return runQuery(null, records)
}
next(tick) next(tick)
}) })
} }
...@@ -1145,6 +1145,10 @@ module.exports = (function() { ...@@ -1145,6 +1145,10 @@ module.exports = (function() {
if (options && options.hooks === true && !!records && records.length > 0) { if (options && options.hooks === true && !!records && records.length > 0) {
var tick = 0 var tick = 0
var next = function(i) { var next = function(i) {
if (i >= records.length) {
return finished(null, records)
}
self.runHooks(self.options.hooks.afterUpdate, records[i], function(err, newValues) { self.runHooks(self.options.hooks.afterUpdate, records[i], function(err, newValues) {
if (!!err) { if (!!err) {
return finished(err) return finished(err)
...@@ -1153,10 +1157,6 @@ module.exports = (function() { ...@@ -1153,10 +1157,6 @@ module.exports = (function() {
records[i].dataValues = !!newValues ? newValues.dataValues : records[i].dataValues records[i].dataValues = !!newValues ? newValues.dataValues : records[i].dataValues
tick++ tick++
if (tick >= records.length) {
return finished(null, records)
}
next(tick) next(tick)
}) })
} }
...@@ -1176,6 +1176,10 @@ module.exports = (function() { ...@@ -1176,6 +1176,10 @@ module.exports = (function() {
} }
var next = function(i) { var next = function(i) {
if (i >= records.length) {
return runQuery(null, records)
}
self.runHooks(self.options.hooks.beforeUpdate, records[i], function(err, newValues) { self.runHooks(self.options.hooks.beforeUpdate, records[i], function(err, newValues) {
if (!!err) { if (!!err) {
return runQuery(err) return runQuery(err)
...@@ -1184,10 +1188,6 @@ module.exports = (function() { ...@@ -1184,10 +1188,6 @@ module.exports = (function() {
records[i].dataValues = !!newValues ? newValues.dataValues : records[i].dataValues records[i].dataValues = !!newValues ? newValues.dataValues : records[i].dataValues
tick++ tick++
if (tick >= records.length) {
return runQuery(null, records)
}
next(tick) next(tick)
}) })
} }
......
...@@ -1883,7 +1883,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () { ...@@ -1883,7 +1883,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
hookCalled++ hookCalled++
next() next()
}) })
B.hasMany(A) B.hasMany(A)
A.hasMany(B) A.hasMany(B)
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!