New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(destroy): Properties updated in a beforeDestroy hook are now pers… #9319
Conversation
I think this should be applied on Just tell me, I'm happy to open another PR if needs be. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few changes
test/integration/model.test.js
Outdated
@@ -1433,6 +1433,26 @@ describe(Support.getTestDialectTeaser('Model'), () => { | |||
}); | |||
}); | |||
|
|||
it('sets other changed values when soft deleting and a beforeDestroy hooks kicks in', function() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move to https://github.com/sequelize/sequelize/blob/master/test/integration/hooks/destroy.test.js
Please create a new group for paranoid, under that place this test
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
lib/model.js
Outdated
@@ -3896,7 +3896,7 @@ class Model { | |||
if (this.constructor._timestampAttributes.deletedAt && options.force === false) { | |||
const attribute = this.constructor.rawAttributes[this.constructor._timestampAttributes.deletedAt]; | |||
const field = attribute.field || this.constructor._timestampAttributes.deletedAt; | |||
const values = {}; | |||
const values = Utils.mapValueFieldNames(this, this.changed() || [], this.constructor); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it should be mapValueFieldNames(this.dataValues,....
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
test/integration/model.test.js
Outdated
@@ -1433,6 +1433,26 @@ describe(Support.getTestDialectTeaser('Model'), () => { | |||
}); | |||
}); | |||
|
|||
it('sets other changed values when soft deleting and a beforeDestroy hooks kicks in', function() { | |||
const ParanoidUser = this.sequelize.define('ParanoidUser', { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also add a test where you try to update a virtual field in hook and see it doesn't get updated (i.e. it should not throw error for unknown column)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
591e2ca
to
d1be757
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, some cosmetic changes
@@ -76,6 +76,51 @@ describe(Support.getTestDialectTeaser('Hooks'), () => { | |||
}); | |||
}); | |||
}); | |||
|
|||
describe('with paranoid mode enabled', () => { | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove extra line
expect(user.virtualField).to.equal(0); | ||
}); | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove extra line
}); | ||
|
||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove extra line
…isted on soft delete. Closes sequelize#9318
d1be757
to
bb42b17
Compare
@sushantdhiman Done, sorry for the extra blank lines |
Thanks for the appoval, what about the backport to v4 ? |
I'll cherry pick commit from master and port to v4 :) |
I was checking other use of this hook, we need to apply this change to Lines 2546 to 2568 in 4caa17c
So Also This change may be unexpected for some users and definitely change hook behaviour (#9318 (comment)) so its better to keep this in v5 |
Fine to keep this v5 only. Cheers |
@sushantdhiman Sorry, I'm puzzled 😀
I'm thinking that the code should individually call That would sum up to something like (pseudo-code):
Calling WDYT? |
@ddolcimascolo Good question and your pseudo-code is exactly what we should do (like the update case), I reckon we need this change for |
Hey guys (and @sushantdhiman), Can you share the status of this contribution? Do you need more code from me or can we consider it good to merge, eventually opening another issue to track the refactoring of the bulk 'destroy' operation? For what it's worth, we're using this patch in production for several weeks now without any issue. It fits well with bonaval/sequelize-temporal#7 which I contributed too. Thanks, David |
…isted on soft delete. Closes #9318
Pull Request check-list
Please make sure to review and check all of these items:
npm run test
ornpm run test-DIALECT
pass with this change (including linting)?Description of change
When getting the list of things to update in the database to perform a soft delete, the code is now considering changed values (through
Model.changed()
) instead of only updatingdeletedAt
.Closes issue #9318