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

Commit 0fe77d6c by David DOLCIMASCOLO Committed by Sushant

fix(destroy): attributes updated in a beforeDestroy hook are now persisted on soft delete. (#9319)

1 parent be8760db
...@@ -3980,7 +3980,7 @@ class Model { ...@@ -3980,7 +3980,7 @@ class Model {
if (this.constructor._timestampAttributes.deletedAt && options.force === false) { if (this.constructor._timestampAttributes.deletedAt && options.force === false) {
const attribute = this.constructor.rawAttributes[this.constructor._timestampAttributes.deletedAt]; const attribute = this.constructor.rawAttributes[this.constructor._timestampAttributes.deletedAt];
const field = attribute.field || this.constructor._timestampAttributes.deletedAt; const field = attribute.field || this.constructor._timestampAttributes.deletedAt;
const values = {}; const values = Utils.mapValueFieldNames(this.dataValues, this.changed() || [], this.constructor);
values[field] = new Date(); values[field] = new Date();
where[field] = attribute.hasOwnProperty('defaultValue') ? attribute.defaultValue : null; where[field] = attribute.hasOwnProperty('defaultValue') ? attribute.defaultValue : null;
......
...@@ -76,6 +76,48 @@ describe(Support.getTestDialectTeaser('Hooks'), () => { ...@@ -76,6 +76,48 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
}); });
}); });
}); });
describe('with paranoid mode enabled', () => {
beforeEach(function() {
this.ParanoidUser = this.sequelize.define('ParanoidUser', {
username: DataTypes.STRING,
updatedBy: DataTypes.INTEGER,
virtualField: {
type: DataTypes.VIRTUAL(DataTypes.INTEGER, ['updatedBy']),
get() {
return this.updatedBy - 1;
}
}
}, {
paranoid: true,
hooks: {
beforeDestroy: instance => instance.updatedBy = 1
}
});
});
it('sets other changed values when soft deleting and a beforeDestroy hooks kicks in', function() {
return this.ParanoidUser.sync({ force: true })
.then(() => this.ParanoidUser.create({ username: 'user1' }))
.then(user => user.destroy())
.then(() => this.ParanoidUser.findOne({ paranoid: false }))
.then(user => {
expect(user.updatedBy).to.equal(1);
});
});
it('should not throw error when a beforeDestroy hook changes a virtual column', function() {
this.ParanoidUser.beforeDestroy(instance => instance.virtualField = 2);
return this.ParanoidUser.sync({ force: true })
.then(() => this.ParanoidUser.create({ username: 'user1' }))
.then(user => user.destroy())
.then(() => this.ParanoidUser.findOne({ paranoid: false }))
.then(user => {
expect(user.virtualField).to.equal(0);
});
});
});
}); });
}); });
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!