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

Make option value to variant association unique #4146

Conversation

jarednorman
Copy link
Member

@jarednorman jarednorman commented Aug 15, 2021

Description

I can't think of a reason why someone would want to associate the same option value with a given variant multiple times. This makes the index on the variant/option_value_id columns of the spree_option_values_variants join table a unique index so we can't have a variant that has the same option value multiple times.

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)

@jarednorman jarednorman marked this pull request as draft August 15, 2021 20:38
@jarednorman
Copy link
Member Author

I don't think any of the errors have anything to do with my change. 🤔

@jarednorman jarednorman force-pushed the why-would-you-ever-want-the-same-option-value-on-a-variant-twice branch from bb6f068 to 4af9993 Compare August 16, 2021 23:58
test.rb Outdated Show resolved Hide resolved
test.rb Outdated Show resolved Hide resolved
@jarednorman
Copy link
Member Author

Had to scroll back more. I used 6.2 as the migration version, but currently we have to write migrations for the lowest supported ActiveRecord version.

@jarednorman jarednorman force-pushed the why-would-you-ever-want-the-same-option-value-on-a-variant-twice branch 2 times, most recently from 6c77ecb to f38fa05 Compare August 17, 2021 00:13
@jarednorman jarednorman marked this pull request as ready for review August 17, 2021 00:13
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.

🎉 looks good to me, thanks @jarednorman !

def up
remove_index :spree_option_values_variants, [:variant_id, :option_value_id]
add_index :spree_option_values_variants, [:variant_id, :option_value_id],
name: "index_option_values_variants_on_variant_id_and_option_value_id",
Copy link
Member

Choose a reason for hiding this comment

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

out of curiosity, why the explicit index name?

Copy link
Member Author

Choose a reason for hiding this comment

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

Rails will generate an index name that exceeds the 64 character limit. The name I've used just matches the existing index's name, so presumably whoever created the original index also ran into this.

@kennyadsl kennyadsl added this to the 4.0 milestone Sep 1, 2021
@kennyadsl kennyadsl added the release:major Breaking change on hold until next major release label Sep 6, 2021
@waiting-for-dev waiting-for-dev added type:enhancement Proposed or newly added feature changelog:solidus_core Changes to the solidus_core gem and removed Needs Core Team Review labels Aug 24, 2022
@mamhoff
Copy link
Contributor

mamhoff commented Jan 31, 2023

This is not to hold this back: We associate the same option value with multiple variants. We have a third field, an integer, that differentiates the variants from one another. We are a special case though as we sell a lot of stuff in differently sized bags.

@jarednorman
Copy link
Member Author

@mamhoff I don't think this will impact you then? You can still have multiple variants per option value, just not the the same option value on one variant multiple times.

@mamhoff
Copy link
Contributor

mamhoff commented Jan 31, 2023

Ah true! I mistakenly thought we need to have different option value combos for each variant. 👍

@kennyadsl
Copy link
Member

Hey @jarednorman, can you please rebase this one? Thanks! 🙏

@waiting-for-dev waiting-for-dev force-pushed the why-would-you-ever-want-the-same-option-value-on-a-variant-twice branch from f38fa05 to a9aea0b Compare April 25, 2023 13:31
@waiting-for-dev waiting-for-dev requested a review from a team as a code owner April 25, 2023 13:31
@waiting-for-dev
Copy link
Contributor

Hey @jarednorman, can you please rebase this one? Thanks! pray

I did it myself and fixed the conflict

@BenMorganIO
Copy link
Contributor

This would be a good chance to mark the variant_id and the option_value_id as NOT NULL and add the two foreign key constraints. I would assume ON DELETE CASCADE ON UPDATE CASCADE would be appropriate here.

I can't think of a reason why someone would want to associate the same
option value with a given variant multiple times. This makes the index
on the variant/option_value_id columns of the
spree_option_values_variants join table a unique index so we can't have
a variant that has the same option value multiple times.

There's a vaguely related issue over on solidus_importer where the
option value processor attempts to do this if you try to reimport
products. This will cause it to error out instead.
@jarednorman jarednorman force-pushed the why-would-you-ever-want-the-same-option-value-on-a-variant-twice branch from a9aea0b to 672116e Compare April 25, 2023 21:43
@kennyadsl
Copy link
Member

This would be a good chance to mark the variant_id and the option_value_id as NOT NULL and add the two foreign key constraints. I would assume ON DELETE CASCADE ON UPDATE CASCADE would be appropriate here.

We decided to skip this kind of changes for this major release. There are already a bunch of database changes and we don't want to be a nightmare to update.

@BenMorganIO thanks a lot for the suggestion, I think we should design a process to make these changes safer. An idea is a script that runs at boot and checks the database (especially important in production) and gives some hints if you need to fix inconsistencies. BTW, we can discuss in Slack or in a separate issue/PR if you want.

@kennyadsl kennyadsl merged commit 05dd2e2 into solidusio:master Apr 26, 2023
@jarednorman jarednorman deleted the why-would-you-ever-want-the-same-option-value-on-a-variant-twice branch April 26, 2023 15:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog:solidus_core Changes to the solidus_core gem release:major Breaking change on hold until next major release type:enhancement Proposed or newly added feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants