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

Commit 565f9c48 by Mick Hansen

Merge branch 'Ghirro-update-fix'

2 parents 30181be1 cc9638ab
Showing with 79 additions and 1 deletions
...@@ -1577,6 +1577,7 @@ module.exports = (function() { ...@@ -1577,6 +1577,7 @@ module.exports = (function() {
* @param {Array} [options.fields] Fields to update (defaults to all fields) * @param {Array} [options.fields] Fields to update (defaults to all fields)
* @param {Boolean} [options.validate=true] Should each row be subject to validation before it is inserted. The whole insert will fail if one row fails validation * @param {Boolean} [options.validate=true] Should each row be subject to validation before it is inserted. The whole insert will fail if one row fails validation
* @param {Boolean} [options.hooks=true] Run before / after bulk update hooks? * @param {Boolean} [options.hooks=true] Run before / after bulk update hooks?
* @param {Boolean} [options.sideEffects=true] Whether or not to update the side effects of any virtual setters.
* @param {Boolean} [options.individualHooks=false] Run before / after update hooks?. If true, this will execute a SELECT followed by individual UPDATEs. A select is needed, because the row data needs to be passed to the hooks * @param {Boolean} [options.individualHooks=false] Run before / after update hooks?. If true, this will execute a SELECT followed by individual UPDATEs. A select is needed, because the row data needs to be passed to the hooks
* @param {Boolean} [options.returning=false] Return the affected rows (only for postgres) * @param {Boolean} [options.returning=false] Return the affected rows (only for postgres)
* @param {Number} [options.limit] How many rows to update (only for mysql and mariadb) * @param {Number} [options.limit] How many rows to update (only for mysql and mariadb)
...@@ -1596,7 +1597,8 @@ module.exports = (function() { ...@@ -1596,7 +1597,8 @@ module.exports = (function() {
hooks: true, hooks: true,
individualHooks: false, individualHooks: false,
returning: false, returning: false,
force: false force: false,
sideEffects: true
}, options || {}); }, options || {});
options.type = QueryTypes.BULKUPDATE; options.type = QueryTypes.BULKUPDATE;
...@@ -1631,6 +1633,11 @@ module.exports = (function() { ...@@ -1631,6 +1633,11 @@ module.exports = (function() {
var build = self.build(values); var build = self.build(values);
build.set(self._timestampAttributes.updatedAt, values[self._timestampAttributes.updatedAt], { raw: true }); build.set(self._timestampAttributes.updatedAt, values[self._timestampAttributes.updatedAt], { raw: true });
if (options.sideEffects) {
values = Utils._.assign(values, Utils._.pick(build.get(), build.changed()));
options.fields = Utils._.union(options.fields, Object.keys(values));
}
// We want to skip validations for all other fields // We want to skip validations for all other fields
options.skip = Utils._.difference(Object.keys(self.attributes), Object.keys(values)); options.skip = Utils._.difference(Object.keys(self.attributes), Object.keys(values));
return build.hookValidate(options).then(function(attributes) { return build.hookValidate(options).then(function(attributes) {
......
...@@ -975,6 +975,77 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -975,6 +975,77 @@ describe(Support.getTestDialectTeaser('Model'), function() {
}); });
}); });
it('doesn\'t update attributes that are altered by virtual setters when option is enabled', function () {
var User = this.sequelize.define('UserWithVirtualSetters', {
username: Sequelize.STRING,
illness_name: Sequelize.STRING,
illness_pain: Sequelize.INTEGER,
illness: {
type: Sequelize.VIRTUAL,
set: function (value) {
this.set('illness_name', value.name);
this.set('illness_pain', value.pain);
}
}
});
return User.sync({ force: true }).then(function () {
return User.create({
username: 'Jan',
illness_name: 'Headache',
illness_pain: 5
});
}).then(function () {
return User.update({
illness: { pain: 10, name: 'Backache' }
}, {
where: {
username: 'Jan'
},
sideEffects: false
});
}).then(function (user) {
return User.findAll();
}).spread(function (user) {
expect(user.illness_pain).to.be.equal(5);
});
});
it('updates attributes that are altered by virtual setters', function () {
var User = this.sequelize.define('UserWithVirtualSetters', {
username: Sequelize.STRING,
illness_name: Sequelize.STRING,
illness_pain: Sequelize.INTEGER,
illness: {
type: Sequelize.VIRTUAL,
set: function (value) {
this.set('illness_name', value.name);
this.set('illness_pain', value.pain);
}
}
});
return User.sync({ force: true }).then(function () {
return User.create({
username: 'Jan',
illness_name: 'Headache',
illness_pain: 5
});
}).then(function () {
return User.update({
illness: { pain: 10, name: 'Backache' }
}, {
where: {
username: 'Jan'
}
});
}).then(function (user) {
return User.findAll();
}).spread(function (user) {
expect(user.illness_pain).to.be.equal(10);
});
});
it('sets updatedAt to the current timestamp', function() { it('sets updatedAt to the current timestamp', function() {
var data = [{ username: 'Peter', secretValue: '42' }, var data = [{ username: 'Peter', secretValue: '42' },
{ username: 'Paul', secretValue: '42' }, { username: 'Paul', secretValue: '42' },
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!