Skip to content
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

Merged
merged 5 commits into from
Jan 29, 2021
Merged

Discard Paranoia #3488

merged 5 commits into from
Jan 29, 2021

Conversation

cedum
Copy link
Contributor

@cedum cedum commented Jan 24, 2020

Description
This removes paranoia and leaves discard as the only option for soft-deleting records.

Ref issues: #3393, #2354

TODO:

Checklist:

  • I have followed Pull Request guidelines
  • I have added a detailed description into each commit message
  • I have updated Guides and README accordingly to this change (if needed)
  • I have added tests to cover this change (if needed)
  • I have attached screenshots to this PR for visual changes (if needed)

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

Instance Methods

  • destroy (also paranoia_destroy) becomes discard
    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 (also paranoia_delete) becomes discard* (*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 (also restore!) becomes undiscard (and undiscard!)
    restores a soft deleted record. The methods with a bang ! raise an exception when failed.

  • really_destroy! becomes destroy
    hard-deletes a record.

  • paranoia_destroyed? (also deleted?) becomes discarded?
    checks if a record has been soft-deleted.

Model Callbacks

@cedum cedum force-pushed the remove-paranoia branch 2 times, most recently from a4774da to d2573bb Compare January 24, 2020 17:11
@cedum cedum force-pushed the remove-paranoia branch 7 times, most recently from 625c12d to 99e93e9 Compare January 31, 2020 16:39
@cedum cedum force-pushed the remove-paranoia branch 4 times, most recently from e612a9f to 0780b35 Compare February 7, 2020 16:10
@cedum cedum marked this pull request as ready for review February 7, 2020 16:11
Copy link
Member

@spaghetticode spaghetticode left a 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)
Copy link
Member

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 }
Copy link
Member

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.

Copy link
Member

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'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

Copy link
Member

@jarednorman jarednorman left a 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.

Copy link
Contributor

@ericsaupe ericsaupe left a 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.

@kennyadsl kennyadsl added this to the 3.0.0 milestone Mar 24, 2020
@cedum cedum force-pushed the remove-paranoia branch 2 times, most recently from 7310037 to 2a1c21d Compare March 27, 2020 13:31
@cedum cedum changed the title Remove paranoia Discard Paranoia Mar 27, 2020
Copy link
Member

@jarednorman jarednorman left a 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.

@kennyadsl kennyadsl added the release:major Breaking change on hold until next major release label Apr 20, 2020
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.
Copy link
Member

@kennyadsl kennyadsl left a 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

@kennyadsl kennyadsl merged commit b0092d0 into solidusio:master Jan 29, 2021
@kennyadsl kennyadsl deleted the remove-paranoia branch January 29, 2021 11:43
SamuelMartini added a commit to solidusio-contrib/solidus_sale_prices that referenced this pull request Feb 26, 2021
SamuelMartini added a commit to solidusio-contrib/solidus_sale_prices that referenced this pull request Feb 26, 2021
@kennyadsl kennyadsl removed the release:major Breaking change on hold until next major release label Apr 30, 2021
cpfergus1 pushed a commit to cpfergus1/solidus_sale_prices that referenced this pull request Jul 1, 2021
cpfergus1 pushed a commit to cpfergus1/solidus_sale_prices that referenced this pull request Jul 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants