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

Commit 110af509 by Mick Hansen

Merge branch 'fix-update-hook' of https://github.com/BridgeAR/sequelize into Bri…

…dgeAR-fix-update-hook

Conflicts:
	changelog.md
2 parents 4483a2ff ba537e9b
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
- [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 bulkCreate: do not insert NULL for undefined values [3729](https://github.com/sequelize/sequelize/pull/3729) - [BUG] Fix bulkCreate: do not insert NULL for undefined values [3729](https://github.com/sequelize/sequelize/pull/3729)
- [BUG] Fix trying to roll back a comitted transaction if an error occured while comitting resulting in an unhandled rejection [3726](https://github.com/sequelize/sequelize/pull/3726) - [BUG] Fix trying to roll back a comitted transaction if an error occured while comitting resulting in an unhandled rejection [3726](https://github.com/sequelize/sequelize/pull/3726)
- [BUG] Fix regression in beforeUpdate hook where `instance.changed()` would always be false [3727](https://github.com/sequelize/sequelize/pull/3727)
#### Backwards compatibility changes #### Backwards compatibility changes
- The error that is thrown when a column is declared to be an enum but without any values used to "Values for ENUM haven't been defined" and is now "Values for ENUM have not been defined". - The error that is thrown when a column is declared to be an enum but without any values used to "Values for ENUM haven't been defined" and is now "Values for ENUM have not been defined".
......
...@@ -1673,8 +1673,14 @@ module.exports = (function() { ...@@ -1673,8 +1673,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!