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

Commit ba537e9b by Ruben Bridgewater

instance.changed() should return the fields in beforeUpdate hook. Fixes #3631

1 parent 7c9d1d7b
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
- [BUG] fix showIndexQuery so appropriate indexes are returned when a schema is used - [BUG] fix showIndexQuery so appropriate indexes are returned when a schema is used
- [BUG] Fix addIndexQuery error when the model has a schema - [BUG] Fix addIndexQuery error when the model has a schema
- [BUG] Fix app crash in sqlite while running in special unique constraint errors [3730](https://github.com/sequelize/sequelize/pull/3730) - [BUG] Fix app crash in sqlite while running in special unique constraint errors [3730](https://github.com/sequelize/sequelize/pull/3730)
- [BUG] Fix regression in beforeUpdate hook where `instance.changed()` would always be false [3727](https://github.com/sequelize/sequelize/pull/3727)
# 2.1.3 # 2.1.3
- [BUG] Fix regression introduced in 2.1.2: updatedAt not set anymore [3667](https://github.com/sequelize/sequelize/pull/3667) - [BUG] Fix regression introduced in 2.1.2: updatedAt not set anymore [3667](https://github.com/sequelize/sequelize/pull/3667)
......
...@@ -1685,8 +1685,14 @@ module.exports = (function() { ...@@ -1685,8 +1685,14 @@ module.exports = (function() {
, different = false; , different = false;
return Promise.map(daos, function(dao) { return Promise.map(daos, function(dao) {
// Record updates in dao's dataValues // Record updates in instances dataValues
Utils._.extend(dao.dataValues, values); Utils._.extend(dao.dataValues, values);
// Set the changed fields on the instance
Utils._.forIn(valuesUse, function(newValue, attr) {
if (newValue !== dao._previousDataValues[attr]) {
dao.setDataValue(attr, newValue);
}
});
// Run beforeUpdate hook // Run beforeUpdate hook
return self.runHooks('beforeUpdate', dao, options).then(function() { return self.runHooks('beforeUpdate', dao, options).then(function() {
......
...@@ -3437,6 +3437,7 @@ describe(Support.getTestDialectTeaser('Hooks'), function() { ...@@ -3437,6 +3437,7 @@ describe(Support.getTestDialectTeaser('Hooks'), function() {
}); });
this.User.beforeUpdate(function(user, options, fn) { this.User.beforeUpdate(function(user, options, fn) {
expect(user.changed()).to.not.be.empty;
user.beforeHookTest = true; user.beforeHookTest = true;
fn(); fn();
}); });
...@@ -3460,33 +3461,52 @@ describe(Support.getTestDialectTeaser('Hooks'), function() { ...@@ -3460,33 +3461,52 @@ describe(Support.getTestDialectTeaser('Hooks'), function() {
}); });
}); });
it('should run the after/before functions for each item created successfully changing some data before updating', function() {
var self = this;
this.User.beforeUpdate(function(user, options) {
expect(user.changed()).to.not.be.empty;
if (user.get('id') === 1) {
user.set('aNumber', user.get('aNumber') + 3);
}
});
return this.User.bulkCreate([
{aNumber: 1}, {aNumber: 1}, {aNumber: 1}
]).then(function() {
return self.User.update({aNumber: 10}, {where: {aNumber: 1}, individualHooks: true}).spread(function(affectedRows, records) {
records.forEach(function(record, i) {
expect(record.aNumber).to.equal(10 + (record.id === 1 ? 3 : 0));
});
});
});
});
it('should run the after/before functions for each item created with an error', function() { it('should run the after/before functions for each item created with an error', function() {
var self = this var self = this
, beforeBulk = false , beforeBulk = false
, afterBulk = false; , afterBulk = false;
this.User.beforeBulkUpdate(function(options, fn) { this.User.beforeBulkUpdate(function(options) {
beforeBulk = true; beforeBulk = true;
fn();
}); });
this.User.afterBulkUpdate(function(options, fn) { this.User.afterBulkUpdate(function(options) {
afterBulk = true; afterBulk = true;
fn();
}); });
this.User.beforeUpdate(function(user, options, fn) { this.User.beforeUpdate(function(user, options) {
fn(new Error('You shall not pass!')); throw new Error('You shall not pass!');
}); });
this.User.afterUpdate(function(user, options, fn) { this.User.afterUpdate(function(user, options) {
user.username = 'User' + user.id; user.username = 'User' + user.id;
fn();
}); });
return this.User.bulkCreate([{aNumber: 1}, {aNumber: 1}, {aNumber: 1}], { fields: ['aNumber'] }).then(function() { return this.User.bulkCreate([{aNumber: 1}, {aNumber: 1}, {aNumber: 1}], { fields: ['aNumber'] }).then(function() {
return self.User.update({aNumber: 10}, {where: {aNumber: 1}, individualHooks: true}).catch(function(err) { return self.User.update({aNumber: 10}, {where: {aNumber: 1}, individualHooks: true}).catch(function(err) {
expect(err).to.be.instanceOf(Error); expect(err).to.be.instanceOf(Error);
expect(err.message).to.be.equal('You shall not pass!');
expect(beforeBulk).to.be.true; expect(beforeBulk).to.be.true;
expect(afterBulk).to.be.false; expect(afterBulk).to.be.false;
}); });
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!