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

Commit ed111c0c by Gabe Gorelick Committed by Sushant

feat(model/update): support paranoid option (#8735)

1 parent 42638ff2
Showing with 64 additions and 2 deletions
......@@ -2589,6 +2589,7 @@ class Model {
* @param {Object} values
* @param {Object} options
* @param {Object} options.where Options to describe the scope of the search.
* @param {Boolean} [options.paranoid=true] If true, only non-deleted records will be updated. If false, both deleted and non-deleted records will be updated. Only applies if `options.paranoid` is true for the model.
* @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.hooks=true] Run before / after bulk update hooks?
......@@ -2607,14 +2608,14 @@ class Model {
this._optionsMustContainWhere(options);
options = Utils.cloneDeep(options);
options = _.defaults(options, {
options = this._paranoidClause(this, _.defaults(options, {
validate: true,
hooks: true,
individualHooks: false,
returning: false,
force: false,
sideEffects: true
});
}));
options.type = QueryTypes.BULKUPDATE;
......
......@@ -1135,6 +1135,67 @@ describe(Support.getTestDialectTeaser('Model'), () => {
});
});
it('does not update soft deleted records when model is paranoid', function() {
const ParanoidUser = this.sequelize.define('ParanoidUser', { username: DataTypes.STRING }, { paranoid: true });
return this.sequelize.sync({ force: true }).then(() => {
return ParanoidUser.bulkCreate([
{ username: 'user1' },
{ username: 'user2' }
]);
}).then(() => {
return ParanoidUser.destroy({
where: {
username: 'user1'
}
});
}).then(() => {
return ParanoidUser.update({ username: 'foo' }, {
where: {}
});
}).then(() => {
return ParanoidUser.findAll({
paranoid: false,
where: {
username: 'foo'
}
});
}).then(users => {
expect(users).to.have.lengthOf(1, 'should not update soft-deleted record');
});
});
it('updates soft deleted records when paranoid is overridden', function() {
const ParanoidUser = this.sequelize.define('ParanoidUser', { username: DataTypes.STRING }, { paranoid: true });
return this.sequelize.sync({ force: true }).then(() => {
return ParanoidUser.bulkCreate([
{ username: 'user1' },
{ username: 'user2' }
]);
}).then(() => {
return ParanoidUser.destroy({
where: {
username: 'user1'
}
});
}).then(() => {
return ParanoidUser.update({ username: 'foo' }, {
where: {},
paranoid: false
});
}).then(() => {
return ParanoidUser.findAll({
paranoid: false,
where: {
username: 'foo'
}
});
}).then(users => {
expect(users).to.have.lengthOf(2);
});
});
if (dialect === 'postgres') {
it('returns the affected rows if `options.returning` is true', function() {
const self = this,
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!