Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #5630: mysql_prefix_indexes_v2.diff

File mysql_prefix_indexes_v2.diff, 4.4 kB (added by anonymous, 2 years ago)

Another method for specifying prefix length on indexes.

  • activerecord/test/active_schema_test_mysql.rb

    old new  
    2323  def test_add_column_with_limit 
    2424    assert_equal "ALTER TABLE people ADD `key` varchar(32)", add_column(:people, :key, :string, :limit => 32) 
    2525  end 
     26 
     27  def test_add_index 
     28    assert_equal "CREATE  INDEX `accounts_firm_id_index` ON accounts (`firm_id`)", add_index(:accounts, :firm_id) 
     29    assert_equal "CREATE  INDEX `tasks_range_idx` ON tasks (`starting`, `ending`)", 
     30      add_index(:tasks, [:starting, :ending], :name => 'tasks_range_idx') 
     31  end 
    2632   
     33  def test_add_index_with_prefix 
     34    assert_equal "CREATE  INDEX `comments_idx` ON comments (`body`(200))",  
     35      add_index(:comments, {:name => :body, :length => 200}, :name => 'comments_idx') 
     36    assert_equal "CREATE  INDEX `comments_idx` ON comments (`body`(200), `type`(50))",  
     37      add_index(:comments, [{:name => :body, :length => 200}, {:name => :type, :length => 50}], :name => 'comments_idx') 
     38  end 
     39   
    2740  private 
    2841    def method_missing(method_symbol, *arguments) 
    2942      ActiveRecord::Base.connection.send(method_symbol, *arguments) 
  • activerecord/lib/active_record/connection_adapters/mysql_adapter.rb

    old new  
    286286        indexes 
    287287      end 
    288288 
     289      # Adds a new index to the table.  +column_name+ can be a single Symbol, 
     290      # a single Hash, or an array of Symbols / Hashes. 
     291      # 
     292      # The index will be named after the table and the first column names, 
     293      # unless you pass +:name+ as an option. 
     294      # 
     295      # When creating an index on multiple columns, the first column is used as a name 
     296      # for the index. For example, when you specify an index on two columns 
     297      # [+:first+, +:last+], the DBMS creates an index for both columns as well as an 
     298      # index for the first colum +:first+. Using just the first name for this index 
     299      # makes sense, because you will never have to create a singular index with this 
     300      # name. 
     301      # 
     302      # ===== Examples 
     303      # ====== Creating a simple index 
     304      #  add_index(:suppliers, :name) 
     305      # generates 
     306      #  CREATE INDEX suppliers_name_index ON suppliers(`name`) 
     307      # ====== Creating a unique index 
     308      #  add_index(:accounts, [:branch_id, :party_id], :unique => true) 
     309      # generates 
     310      #  CREATE UNIQUE INDEX accounts_branch_id_index ON accounts(`branch_id`, `party_id`) 
     311      # ====== Creating a named index 
     312      #  add_index(:accounts, [:branch_id, :party_id], :unique => true, :name => 'by_branch_party') 
     313      # generates 
     314      #  CREATE UNIQUE INDEX by_branch_party ON accounts(`branch_id`, `party_id`) 
     315      # ====== Creating an index with a prefix 
     316      #  add_index(:comments, {:name => :body, :length => 200}) 
     317      # generates 
     318      #  CREATE  INDEX comments_body_index ON comments(`body`(200)) 
     319      # ====== Creating a multi-column index with mixed prefixed and non-prefixed fields 
     320      #  add_index(:comments, [{:name => :body, :length => 200}, :type], :name => 'body_type_idx' 
     321      # generates 
     322      #  CREATE  INDEX body_type_idx ON comments(`body`(200), `length`) 
     323      def add_index(table_name, column_name, options = {}) 
     324        column_names = column_name.kind_of?(Hash) ? [column_name] : Array(column_name) 
     325        index_name   = index_name(table_name, :column => column_names.first.to_s) 
     326         
     327        if Hash === options # legacy support, since this param was a string 
     328          index_type = options[:unique] ? "UNIQUE" : "" 
     329          index_name = options[:name] || index_name 
     330        else 
     331          index_type = options 
     332        end 
     333        quoted_column_names = column_names.map { |e| 
     334          if Hash === e 
     335            quote_column_name(e[:name]) + (e[:length] ? "(#{e[:length]})" : "") 
     336          else 
     337            quote_column_name(e) 
     338          end 
     339        }.join(", ") 
     340        execute "CREATE #{index_type} INDEX #{quote_column_name(index_name)} ON #{table_name} (#{quoted_column_names})" 
     341      end 
     342 
    289343      def columns(table_name, name = nil)#:nodoc: 
    290344        sql = "SHOW FIELDS FROM #{table_name}" 
    291345        columns = []