diff options
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.rb | 45 |
1 files changed, 29 insertions, 16 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 e95e97e4a8..9c0c4e3ef0 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -443,7 +443,7 @@ module ActiveRecord # # add_index(:suppliers, :name) # - # generates + # generates: # # CREATE INDEX suppliers_name_index ON suppliers(name) # @@ -451,7 +451,7 @@ module ActiveRecord # # add_index(:accounts, [:branch_id, :party_id], unique: true) # - # generates + # generates: # # CREATE UNIQUE INDEX accounts_branch_id_party_id_index ON accounts(branch_id, party_id) # @@ -459,7 +459,7 @@ module ActiveRecord # # add_index(:accounts, [:branch_id, :party_id], unique: true, name: 'by_branch_party') # - # generates + # generates: # # CREATE UNIQUE INDEX by_branch_party ON accounts(branch_id, party_id) # @@ -467,13 +467,13 @@ module ActiveRecord # # add_index(:accounts, :name, name: 'by_name', length: 10) # - # generates + # generates: # # CREATE INDEX by_name ON accounts(name(10)) # # add_index(:accounts, [:name, :surname], name: 'by_name_surname', length: {name: 10, surname: 15}) # - # generates + # generates: # # CREATE INDEX by_name_surname ON accounts(name(10), surname(15)) # @@ -483,7 +483,7 @@ module ActiveRecord # # add_index(:accounts, [:branch_id, :party_id, :surname], order: {branch_id: :desc, party_id: :asc}) # - # generates + # generates: # # CREATE INDEX by_branch_desc_party ON accounts(branch_id DESC, party_id ASC, surname) # @@ -493,18 +493,30 @@ module ActiveRecord # # add_index(:accounts, [:branch_id, :party_id], unique: true, where: "active") # - # generates + # generates: # # CREATE UNIQUE INDEX index_accounts_on_branch_id_and_party_id ON accounts(branch_id, party_id) WHERE active # # ====== Creating an index with a specific method - # add_index(:developers, :name, :using => 'btree') - # generates - # CREATE INDEX index_developers_on_name ON developers USING btree (name) -- PostgreSQL - # CREATE INDEX index_developers_on_name USING btree ON developers (name) -- MySQL + # + # add_index(:developers, :name, using: 'btree') + # + # generates: + # + # CREATE INDEX index_developers_on_name ON developers USING btree (name) -- PostgreSQL + # CREATE INDEX index_developers_on_name USING btree ON developers (name) -- MySQL # # Note: only supported by PostgreSQL and MySQL # + # ====== Creating an index with a specific type + # + # add_index(:developers, :name, type: :fulltext) + # + # generates: + # + # CREATE FULLTEXT INDEX index_developers_on_name ON developers (name) -- MySQL + # + # Note: only supported by MySQL. Supported: <tt>:fulltext</tt> and <tt>:spatial</tt> on MyISAM tables. def add_index(table_name, column_name, options = {}) index_name, index_type, index_columns, index_options = add_index_options(table_name, column_name, options) execute "CREATE #{index_type} INDEX #{quote_column_name(index_name)} ON #{quote_table_name(table_name)} (#{index_columns})#{index_options}" @@ -755,16 +767,17 @@ module ActiveRecord index_name = index_name(table_name, column: column_names) if Hash === options # legacy support, since this param was a string - options.assert_valid_keys(:unique, :order, :name, :where, :length, :internal, :using, :algorithm) + options.assert_valid_keys(:unique, :order, :name, :where, :length, :internal, :using, :algorithm, :type) index_type = options[:unique] ? "UNIQUE" : "" + index_type = options[:type].to_s if options.key?(:type) index_name = options[:name].to_s if options.key?(:name) max_index_length = options.fetch(:internal, false) ? index_name_length : allowed_index_name_length - if index_algorithms.key?(options[:algorithm]) - algorithm = index_algorithms[options[:algorithm]] - elsif options[:algorithm].present? - raise ArgumentError.new("Algorithm must be one of the following: #{index_algorithms.keys.map(&:inspect).join(', ')}") + if options.key?(:algorithm) + algorithm = index_algorithms.fetch(options[:algorithm]) { + raise ArgumentError.new("Algorithm must be one of the following: #{index_algorithms.keys.map(&:inspect).join(', ')}") + } end using = "USING #{options[:using]}" if options[:using].present? |