aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2006-08-15 23:05:01 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2006-08-15 23:05:01 +0000
commit79542f8d0b1008428da3cd9c5136769fc70334af (patch)
tree9ec553a1ebb8b5d81f162ebd1896f9a5e3c5ffc6 /activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
parent6457b365a1a63018665dfbb4cb365d85be9dd771 (diff)
downloadrails-79542f8d0b1008428da3cd9c5136769fc70334af.tar.gz
rails-79542f8d0b1008428da3cd9c5136769fc70334af.tar.bz2
rails-79542f8d0b1008428da3cd9c5136769fc70334af.zip
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
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb')
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb18
1 files changed, 8 insertions, 10 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
index cd13e264a1..b7f835e747 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
@@ -178,14 +178,14 @@ module ActiveRecord
# ====== 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 @@ module ActiveRecord
# 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 @@ module ActiveRecord
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