Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
View
@@ -1,5 +1,18 @@
*SVN*
+* Migrations: uniquely name multicolumn indexes so you don't have to. [Jeremy Kemper]
+ # people_active_last_name_index, people_active_deactivated_at_index
+ add_index :people, [:active, :last_name]
+ add_index :people, [:active, :deactivated_at]
+ remove_index :people, [:active, :last_name]
+ remove_index :people, [:active, :deactivated_at]
+
+ WARNING: backward-incompatibility. Multicolumn indexes created before this
+ revision were named using the first column name only. Now they're uniquely
+ named using all indexed columns.
+
+ To remove an old multicolumn index, remove_index :table_name, :first_column
+
* Fix for deep includes on the same association. [richcollins@gmail.com]
* 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
View
@@ -178,14 +178,14 @@ def rename_column(table_name, column_name, new_column_name)
# ====== Creating a unique index
# add_index(:accounts, [:branch_id, :party_id], :unique => true)
# generates
- # CREATE UNIQUE INDEX accounts_branch_id_index ON accounts(branch_id, party_id)
+ # CREATE UNIQUE INDEX accounts_branch_id_party_id_index ON accounts(branch_id, party_id)
# ====== Creating a named index
# add_index(:accounts, [:branch_id, :party_id], :unique => true, :name => 'by_branch_party')
# generates
# CREATE UNIQUE INDEX by_branch_party ON accounts(branch_id, party_id)
def add_index(table_name, column_name, options = {})
column_names = Array(column_name)
- index_name = index_name(table_name, :column => column_names.first)
+ index_name = index_name(table_name, :column => column_names)
if Hash === options # legacy support, since this param was a string
index_type = options[:unique] ? "UNIQUE" : ""
@@ -199,16 +199,14 @@ def add_index(table_name, column_name, options = {})
# Remove the given index from the table.
#
- # Remove the suppliers_name_index in the suppliers table (legacy support, use the second or third forms).
+ # Remove the suppliers_name_index in the suppliers table.
# remove_index :suppliers, :name
- # Remove the index named accounts_branch_id in the accounts table.
+ # Remove the index named accounts_branch_id_index in the accounts table.
# remove_index :accounts, :column => :branch_id
+ # Remove the index named accounts_branch_id_party_id_index in the accounts table.
+ # remove_index :accounts, :column => [:branch_id, :party_id]
# Remove the index named by_branch_party in the accounts table.
# remove_index :accounts, :name => :by_branch_party
- #
- # You can remove an index on multiple columns by specifying the first column.
- # add_index :accounts, [:username, :password]
- # remove_index :accounts, :username
def remove_index(table_name, options = {})
execute "DROP INDEX #{quote_column_name(index_name(table_name, options))} ON #{table_name}"
end
@@ -216,14 +214,14 @@ def remove_index(table_name, options = {})
def index_name(table_name, options) #:nodoc:
if Hash === options # legacy support
if options[:column]
- "#{table_name}_#{options[:column]}_index"
+ "#{table_name}_#{Array(options[:column]).join('_')}_index"
elsif options[:name]
options[:name]
else
raise ArgumentError, "You must specify the index name"
end
else
- "#{table_name}_#{options}_index"
+ index_name(table_name, :column => options)
end
end
8 activerecord/test/migration_test.rb
View
@@ -58,7 +58,13 @@ def test_add_index
assert_nothing_raised { Person.connection.remove_index("people", "last_name") }
assert_nothing_raised { Person.connection.add_index("people", ["last_name", "first_name"]) }
- assert_nothing_raised { Person.connection.remove_index("people", "last_name") }
+ assert_nothing_raised { Person.connection.remove_index("people", :column => ["last_name", "first_name"]) }
+ assert_nothing_raised { Person.connection.add_index("people", ["last_name", "first_name"]) }
+ assert_nothing_raised { Person.connection.remove_index("people", :name => "people_last_name_first_name_index") }
+ assert_nothing_raised { Person.connection.add_index("people", ["last_name", "first_name"]) }
+ assert_nothing_raised { Person.connection.remove_index("people", "last_name_first_name") }
+ assert_nothing_raised { Person.connection.add_index("people", ["last_name", "first_name"]) }
+ assert_nothing_raised { Person.connection.remove_index("people", ["last_name", "first_name"]) }
# quoting
# Note: changed index name from "key" to "key_idx" since "key" is a Firebird reserved word
Please sign in to comment.
Something went wrong with that request. Please try again.