-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Discard Paranoia #3488
Discard Paranoia #3488
Conversation
a4774da
to
d2573bb
Compare
625c12d
to
99e93e9
Compare
e612a9f
to
0780b35
Compare
0780b35
to
921bf8b
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.
@cedum thank you for working on such important and extensive change 🙏 🙇
I like the way the PR is coming 👍 , I left some discussion notes for some aspects that may be important to consider.
@@ -108,9 +103,9 @@ def active? | |||
where(type: to_s, active: true).count > 0 | |||
end | |||
|
|||
# @deprecated Use .with_deleted.find instead | |||
# @deprecated Use .with_discarded.find instead | |||
def find_with_destroyed(*args) |
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.
Not sure if this PR would be the best place for doing it, but I think that this deprecated method may just be removed.
Since this PR already has many breaking changes, it may make sense to take advantage of that in order to simply remove this old code as well.
include Discard::Model | ||
self.discard_column = :deleted_at | ||
|
||
default_scope { kept } |
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.
default_scope
has a bit of bad rap, see for example https://rails-bestpractices.com/posts/2013/06/15/default_scope-is-evil/
I'm not implying we should not use it at all, but we should make store maintainers explicitly aware of its existence, with a message such as please verify your discard models overrides don't include a default_scope
and that the new default_scope
does not interact weirdly with your custom code.
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.
Yeah, I understand the desire to use it here as it makes the migration significantly easier. I'd prefer we not use it by default for any future models we make soft-deletable though.
@@ -14,7 +14,6 @@ | |||
require 'state_machines-activerecord' | |||
|
|||
require 'spree/deprecation' | |||
require 'spree/paranoia_deprecations' |
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.
🎉
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.
Awesome work here, thanks for doing this. All looks good to me.
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.
I like it. With such a large change we should have something in the release notes to indicate this large of a change.
7310037
to
2a1c21d
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.
Great work here! Excited for this.
589249b
to
f73f89a
Compare
f73f89a
to
949082b
Compare
We're replacing Paranoia with Discard as the only option for soft-deleting records.
Paranoia is being removed in favor of Discard. #destroy becomes the equivalent method of #really_destroy! for hard deleting records.
Paranoia has been completely removed. We don't need anymore to handle this case here.
Cleanup paranoia related deprecated methods and code comments.
Paranoia is replaced with Discard for soft-deleting records.
949082b
to
df54786
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.
The best PR's title ever! Thanks @cedum
Description
This removes paranoia and leaves discard as the only option for soft-deleting records.
Ref issues: #3393, #2354
TODO:
with_deleted
). PR Replace Paranoia methods with Discard equivalents #3554paranoia
gem from dependencies oncesolidus_auth_devise
dependency will be fixed: it's using paranoia but not declaring it explicitly as a dependency. Created PR onsolidus_auth_devise
Add paranoia dependency explicitly solidus_auth_devise#183Spree::Api::ResourceController#destroy
should support models withdiscard
the same way asSpree::Admin::ResourceController#destroy
PR Add soft-delete support for Api::ResourceController #3854Checklist:
Upgrade Notes
Paranoia has been replaced by Discard gem for models having the soft-delete feature enabled.
For more details about this move check this issue where the rationale has been discussed.
The soft-delete feature in Solidus, beyond being used as an archival tool, is mostly required to prevent hard-deletion of records other critical models depends on (i.e. Order).
NOTE: the soft-deletable models still have a default scope that filters out soft-deleted records to ease the migration.
Starting with Solidus v3 the following methods introduced by Paranoia are replaced by their Discard equivalents:
Class Methods
with_deleted
becomeswith_discarded
Returns both active and soft-deleted records.
only_deleted
becomesdiscarded
Returns only soft-deleted records.
restore(id_or_ids)
becomeswhere(id: [id(s)]).undiscard_all
Restores a list of soft-deleted records.
Instance Methods
destroy
(alsoparanoia_destroy
) becomesdiscard
Soft-deletes a record by firing also any callbacks defined for the model.
Before Solidus v3 when calling
#destroy
, hence soft-deleting a record, any[before|around|after]_destroy
callbacks defined for that model were also fired.With Solidus v3 any callbacks when soft-deleting a record must be defined using
[before|around|after]_discard
API.[before|around|after]_destroy
will be fired instead when hard-deleting a record.delete
(alsoparanoia_delete
) becomesdiscard
* (*see notes below)Soft-deletes a record without firing the callbacks.
Discard doesn't provide a direct equivalent for soft-deleting without firing the
[before|around|after]_discard
callbacks.The same behavior may be obtained by updating directly the soft-delete column:
update_attribute(self.class.discard_column, Time.current)
restore
(alsorestore!
) becomesundiscard
(andundiscard!
)restores a soft deleted record. The methods with a bang
!
raise an exception when failed.really_destroy!
becomesdestroy
hard-deletes a record.
paranoia_destroyed?
(alsodeleted?
) becomesdiscarded?
checks if a record has been soft-deleted.
Model Callbacks
[before|around|after|]_destroy
becomes[before|around|after|]_discard
fired when soft-deleting a record.
[before|around|after|]_real_destroy
becomes[before|around|after|]_destroy
fired when hard-deleting a record.
[before|around|after|]_restore
becomes[before|around|after|]_undiscard
fired when restoring a soft-deleted a record.