Skip to content
This repository
Browse code

Migrations: uniquely name multicolumn indexes so you don't have to.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4767 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 79542f8d0b1008428da3cd9c5136769fc70334af 1 parent 6457b36
Jeremy Kemper jeremy authored
13 activerecord/CHANGELOG
... ... @@ -1,5 +1,18 @@
1 1 *SVN*
2 2
  3 +* Migrations: uniquely name multicolumn indexes so you don't have to. [Jeremy Kemper]
  4 + # people_active_last_name_index, people_active_deactivated_at_index
  5 + add_index :people, [:active, :last_name]
  6 + add_index :people, [:active, :deactivated_at]
  7 + remove_index :people, [:active, :last_name]
  8 + remove_index :people, [:active, :deactivated_at]
  9 +
  10 + WARNING: backward-incompatibility. Multicolumn indexes created before this
  11 + revision were named using the first column name only. Now they're uniquely
  12 + named using all indexed columns.
  13 +
  14 + To remove an old multicolumn index, remove_index :table_name, :first_column
  15 +
3 16 * Fix for deep includes on the same association. [richcollins@gmail.com]
4 17
5 18 * Tweak fixtures so they don't try to use a non-ActiveRecord class. [Kevin Clark]
18 activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
@@ -178,14 +178,14 @@ def rename_column(table_name, column_name, new_column_name)
178 178 # ====== Creating a unique index
179 179 # add_index(:accounts, [:branch_id, :party_id], :unique => true)
180 180 # generates
181   - # CREATE UNIQUE INDEX accounts_branch_id_index ON accounts(branch_id, party_id)
  181 + # CREATE UNIQUE INDEX accounts_branch_id_party_id_index ON accounts(branch_id, party_id)
182 182 # ====== Creating a named index
183 183 # add_index(:accounts, [:branch_id, :party_id], :unique => true, :name => 'by_branch_party')
184 184 # generates
185 185 # CREATE UNIQUE INDEX by_branch_party ON accounts(branch_id, party_id)
186 186 def add_index(table_name, column_name, options = {})
187 187 column_names = Array(column_name)
188   - index_name = index_name(table_name, :column => column_names.first)
  188 + index_name = index_name(table_name, :column => column_names)
189 189
190 190 if Hash === options # legacy support, since this param was a string
191 191 index_type = options[:unique] ? "UNIQUE" : ""
@@ -199,16 +199,14 @@ def add_index(table_name, column_name, options = {})
199 199
200 200 # Remove the given index from the table.
201 201 #
202   - # Remove the suppliers_name_index in the suppliers table (legacy support, use the second or third forms).
  202 + # Remove the suppliers_name_index in the suppliers table.
203 203 # remove_index :suppliers, :name
204   - # Remove the index named accounts_branch_id in the accounts table.
  204 + # Remove the index named accounts_branch_id_index in the accounts table.
205 205 # remove_index :accounts, :column => :branch_id
  206 + # Remove the index named accounts_branch_id_party_id_index in the accounts table.
  207 + # remove_index :accounts, :column => [:branch_id, :party_id]
206 208 # Remove the index named by_branch_party in the accounts table.
207 209 # remove_index :accounts, :name => :by_branch_party
208   - #
209   - # You can remove an index on multiple columns by specifying the first column.
210   - # add_index :accounts, [:username, :password]
211   - # remove_index :accounts, :username
212 210 def remove_index(table_name, options = {})
213 211 execute "DROP INDEX #{quote_column_name(index_name(table_name, options))} ON #{table_name}"
214 212 end
@@ -216,14 +214,14 @@ def remove_index(table_name, options = {})
216 214 def index_name(table_name, options) #:nodoc:
217 215 if Hash === options # legacy support
218 216 if options[:column]
219   - "#{table_name}_#{options[:column]}_index"
  217 + "#{table_name}_#{Array(options[:column]).join('_')}_index"
220 218 elsif options[:name]
221 219 options[:name]
222 220 else
223 221 raise ArgumentError, "You must specify the index name"
224 222 end
225 223 else
226   - "#{table_name}_#{options}_index"
  224 + index_name(table_name, :column => options)
227 225 end
228 226 end
229 227
8 activerecord/test/migration_test.rb
@@ -58,7 +58,13 @@ def test_add_index
58 58 assert_nothing_raised { Person.connection.remove_index("people", "last_name") }
59 59
60 60 assert_nothing_raised { Person.connection.add_index("people", ["last_name", "first_name"]) }
61   - assert_nothing_raised { Person.connection.remove_index("people", "last_name") }
  61 + assert_nothing_raised { Person.connection.remove_index("people", :column => ["last_name", "first_name"]) }
  62 + assert_nothing_raised { Person.connection.add_index("people", ["last_name", "first_name"]) }
  63 + assert_nothing_raised { Person.connection.remove_index("people", :name => "people_last_name_first_name_index") }
  64 + assert_nothing_raised { Person.connection.add_index("people", ["last_name", "first_name"]) }
  65 + assert_nothing_raised { Person.connection.remove_index("people", "last_name_first_name") }
  66 + assert_nothing_raised { Person.connection.add_index("people", ["last_name", "first_name"]) }
  67 + assert_nothing_raised { Person.connection.remove_index("people", ["last_name", "first_name"]) }
62 68
63 69 # quoting
64 70 # Note: changed index name from "key" to "key_idx" since "key" is a Firebird reserved word

0 comments on commit 79542f8

Please sign in to comment.
Something went wrong with that request. Please try again.