From 35e7f455a8af835d1ce62710a2a07dd939d054e0 Mon Sep 17 00:00:00 2001 From: James Nick Sears Date: Thu, 12 Jun 2008 04:48:15 -0400 Subject: [PATCH] add_index with specified key lengths with mysql --- .../connection_adapters/mysql_adapter.rb | 30 ++++++++++++++++++++ activerecord/test/cases/migration_test.rb | 11 +++++++ 2 files changed, 41 insertions(+), 0 deletions(-) diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index 93aafaa..2c2e3ed 100755 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -399,6 +399,36 @@ module ActiveRecord super(table_name, options) end + # ====== Creating an index with specified key length + # add_index(:accounts, [:memo, :description], :key_length => 8, :name => 'by_branch_party') + # generates + # CREATE INDEX by_branch_party ON accounts(`memo`(8), `description`8)) + # ====== Creating an index with specified key length + # add_index(:accounts, [:memo, :description], :key_length => {:memo => 8}, :name => 'by_branch_party') + # generates + # CREATE INDEX by_branch_party ON accounts(`memo`(8), `description`)) + def add_index(table_name, column_name, options = {}) + column_names = Array(column_name) + 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" : "" + index_name = options[:name] || index_name + if options[:key_length] + key_length = options[:key_length] + if Fixnum === key_length + quoted_column_names = column_names.map {|e| "#{quote_column_name(e)}(#{key_length})" }.join(", ") + elsif Hash === key_length + quoted_column_names = column_names.map {|e| ((key_length[e] && Fixnum === key_length[e]) ? "#{quote_column_name(e)}(#{key_length[e]})" : quote_column_name(e) ) }.join(", ") + end + end + else + index_type = options + end + quoted_column_names ||= column_names.map { |e| quote_column_name(e) }.join(", ") + execute "CREATE #{index_type} INDEX #{quote_column_name(index_name)} ON #{quote_table_name(table_name)} (#{quoted_column_names})" + end + def indexes(table_name, name = nil)#:nodoc: indexes = [] current_index = nil diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb index f36255e..9c28f18 100644 --- a/activerecord/test/cases/migration_test.rb +++ b/activerecord/test/cases/migration_test.rb @@ -49,6 +49,7 @@ if ActiveRecord::Base.connection.supports_migrations? end Person.connection.remove_column("people", "first_name") rescue nil Person.connection.remove_column("people", "middle_name") rescue nil + Person.connection.remove_column("people", "memo") rescue nil Person.connection.add_column("people", "first_name", :string, :limit => 40) Person.reset_column_information end @@ -62,6 +63,16 @@ if ActiveRecord::Base.connection.supports_migrations? assert_nothing_raised { Person.connection.add_index("people", "last_name") } assert_nothing_raised { Person.connection.remove_index("people", "last_name") } + if current_adapter?(:MysqlAdapter) + Person.connection.add_column("people", "memo", :text) + assert_nothing_raised { Person.connection.add_index("people", ["memo", "first_name"], :key_length => 8) } + assert_nothing_raised { Person.connection.remove_index("people", :column => ["memo", "first_name"]) } + assert_nothing_raised { Person.connection.add_index("people", ["memo", "first_name"], :key_length => {"memo" => 8}) } + assert_nothing_raised { Person.connection.remove_index("people", :column => ["memo", "first_name"]) } + assert_nothing_raised { Person.connection.add_index("people", [:memo, :first_name], :key_length => {:memo => 8}) } + assert_nothing_raised { Person.connection.remove_index("people", :column => ["memo", "first_name"]) } + end + # Orcl nds shrt indx nms. Sybs 2. # OpenBase does not have named indexes. You must specify a single column name unless current_adapter?(:OracleAdapter, :SybaseAdapter, :OpenBaseAdapter) -- 1.4.4.4