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

Ticket #4853: index_definition_patch.diff

File index_definition_patch.diff, 4.8 kB (added by rubyonrails.org@troja.ath.cx, 2 years ago)

a patch that makes rails understand subkey lengths in indexes

  • activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb

    old new  
    190190        if Hash === options # legacy support, since this param was a string 
    191191          index_type = options[:unique] ? "UNIQUE" : "" 
    192192          index_name = options[:name] || index_name 
     193          limit = {} 
     194          if options.include?(:limit) && self.supports_subkey_length? 
     195            options[:limit].each do |col, lim| 
     196              limit[col] = lim 
     197            end 
     198          end  
    193199        else 
    194200          index_type = options 
    195201        end 
    196202        quoted_column_names = column_names.map { |e| quote_column_name(e) }.join(", ") 
    197         execute "CREATE #{index_type} INDEX #{quote_column_name(index_name)} ON #{table_name} (#{quoted_column_names})" 
     203        execute "CREATE #{index_type} INDEX #{index_name} ON #{table_name} (#{Array(column_name).collect do |col| "#{col}#{limit.include?(col) ? "(#{limit[col]})" : ""}" end.join(", ")})" 
    198204      end 
    199205 
    200206      # Remove the given index from the table. 
  • activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb

    old new  
    161161        end 
    162162    end 
    163163 
    164     class IndexDefinition < Struct.new(:table, :name, :unique, :columns) #:nodoc: 
     164    class IndexDefinition 
     165      attr_accessor :table, :name, :unique, :columns, :limit 
     166      def initialize(table, name, unique, columns, limit = {}) 
     167        @table = table 
     168        @name = name 
     169        @unique = unique 
     170        @columns = columns 
     171        @limit = limit 
     172      end 
    165173    end 
    166174 
    167175    class ColumnDefinition < Struct.new(:base, :name, :type, :limit, :default, :null) #:nodoc: 
  • activerecord/lib/active_record/connection_adapters/abstract_adapter.rb

    old new  
    3939      def supports_migrations? 
    4040        false 
    4141      end 
    42        
     42 
     43      # 
     44      # Does this adapter support subkey length in CREATE INDEX statements? 
     45      # Ex: CREATE INDEX users_name_index ON users (name(20)) 
     46      #       
     47      def supports_subkey_length? 
     48        false 
     49      end 
     50 
    4351      # Does this adapter support using DISTINCT within COUNT?  This is +true+ 
    4452      # for all adapters except sqlite. 
    4553      def supports_count_distinct? 
  • activerecord/lib/active_record/connection_adapters/mysql_adapter.rb

    old new  
    8080        "MySQL server has gone away" 
    8181      ] 
    8282 
     83      def supports_subkey_length? 
     84        true 
     85      end 
     86 
    8387      def initialize(connection, logger, connection_options, config) 
    8488        super(connection, logger) 
    8589        @connection_options, @config = connection_options, config 
     
    274278      def indexes(table_name, name = nil)#:nodoc: 
    275279        indexes = [] 
    276280        current_index = nil 
     281        limit = {} 
    277282        execute("SHOW KEYS FROM #{table_name}", name).each do |row| 
    278283          if current_index != row[2] 
    279284            next if row[2] == "PRIMARY" # skip the primary key 
    280285            current_index = row[2] 
    281286            indexes << IndexDefinition.new(row[0], row[2], row[1] == "0", []) 
    282287          end 
    283  
     288          if !row[7].nil? 
     289            indexes.last.limit[row[4]] = row[7].to_i 
     290          end 
    284291          indexes.last.columns << row[4] 
    285292        end 
    286293        indexes 
  • activerecord/lib/active_record/schema_dumper.rb

    old new  
    113113        indexes.each do |index| 
    114114          stream.print "  add_index #{index.table.inspect}, #{index.columns.inspect}, :name => #{index.name.inspect}" 
    115115          stream.print ", :unique => true" if index.unique 
     116          stream.print ", :limit => {#{index.limit.keys.collect do |key| "\"#{key}\" => #{index.limit[key]}" end.join(", ")}}" if !index.limit.empty? 
    116117          stream.puts 
    117118        end 
    118119        stream.puts unless indexes.empty?