-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
Support multiple indexes on the same column when loading the schema #26019
Support multiple indexes on the same column when loading the schema #26019
Conversation
r? @matthewd (@rails-bot has picked a reviewer for you, use r? to override) |
@@ -328,7 +328,7 @@ def remove_column(name) | |||
# | |||
# index(:account_id, name: 'index_projects_on_account_id') | |||
def index(column_name, options = {}) | |||
indexes[column_name] = options | |||
indexes << options.merge(column_name: column_name) |
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.
How about it?
def index(column_name, options = {})
- indexes[column_name] = options
+ indexes << [column_name, options]
end
https://github.com/rails/rails/pull/26019/files#diff-21d4fbe002689dc4b0ab29f021585457L338
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.
Oh, duh, totally forgot that iterating over an array of arrays will yield each item in the subarray to each
. Much simpler change, thanks!
b26143c
to
9b3f329
Compare
@matthewd @maclover7 would love to get this reviewed and merged into Rails core. Any chance one of you has some time to take a look? |
…ices Support multiple indexes on the same column when loading the schema
Backported in 809757f |
Thanks @rafaelfranca! |
We use Postgres for quite a few of our Rails applications, and one of the things we use constantly are partial unique indices, allowing us to have multiple indices on 1 column with different restrictions based on other column data. While everything in our production systems works fine, and adding new partial unique indices via migrations also works fine, loading multiple indices on the same column (String or Array) via our
db/schema.rb
file turns out to not work quite as expected.If you have a schema like this:
When you run
rails db:schema:load
without this patch, your local database will only include theindex_sponsorships_on_user_id
index, skipping theindex_sponsorships_on_user_id_unique
index.With this patch, all indices are loaded into the database, regardless of whether the columns in question already have an index.
As far as I can tell, this bug affects at least Rails 4.0+.