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

Allow insert_all/upsert_all to use indexes with columns not in the same order as in :unique_by #48018

Merged
merged 1 commit into from
Apr 27, 2023

Conversation

fatkodima
Copy link
Member

This is a small UX improvement.

Problem: If a user have a unique index on (a, b) columns and uses Model.insert_all ..., unique_by: [:a, :b] everything works fine. But then he decides that a unique index on (b, a) would be better (because he is able to search on b column via index). Now he must update all the uses of this index in all insert_all/upsert_all call in his code.

The same can be applied to the composite primary keys (see line 157), but is it common to switch columns in primary keys?

Comment on lines 153 to 155
match = Array(name_or_columns).map(&:to_s)

if index = unique_indexes.find { |i| match.include?(i.name) || i.columns == match }
if index = unique_indexes.find { |i| match.include?(i.name) || Array(i.columns).sort == match.sort }
Copy link
Member

Choose a reason for hiding this comment

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

can you move the match.sort outside the loop? Likely not a big deal in most cases, but it bother me 😅

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated.
So that's probably Shopify's scale taught to more carefully think about performance 😅

Copy link
Member

Choose a reason for hiding this comment

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

Meh, the array is small, and it's very unlikely there's more than a dozen index on the table. It's just habit to never do unnecessary work however how small :).

Copy link
Member

@byroot byroot left a comment

Choose a reason for hiding this comment

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

Minor comment, other than that 👍

@fatkodima fatkodima force-pushed the insert_all-out-of-order-indexes branch from ac4202f to 45bff16 Compare April 27, 2023 14:55
@byroot byroot merged commit ce03255 into rails:main Apr 27, 2023
8 of 9 checks passed
@byroot
Copy link
Member

byroot commented Apr 27, 2023

Thanks!

@fatkodima fatkodima deleted the insert_all-out-of-order-indexes branch April 27, 2023 14:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants