Ticket #4853: index_definition_patch.diff
| File index_definition_patch.diff, 4.8 kB (added by rubyonrails.org@troja.ath.cx, 2 years ago) |
|---|
-
activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
old new 190 190 if Hash === options # legacy support, since this param was a string 191 191 index_type = options[:unique] ? "UNIQUE" : "" 192 192 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 193 199 else 194 200 index_type = options 195 201 end 196 202 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(", ")})" 198 204 end 199 205 200 206 # Remove the given index from the table. -
activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
old new 161 161 end 162 162 end 163 163 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 165 173 end 166 174 167 175 class ColumnDefinition < Struct.new(:base, :name, :type, :limit, :default, :null) #:nodoc: -
activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
old new 39 39 def supports_migrations? 40 40 false 41 41 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 43 51 # Does this adapter support using DISTINCT within COUNT? This is +true+ 44 52 # for all adapters except sqlite. 45 53 def supports_count_distinct? -
activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
old new 80 80 "MySQL server has gone away" 81 81 ] 82 82 83 def supports_subkey_length? 84 true 85 end 86 83 87 def initialize(connection, logger, connection_options, config) 84 88 super(connection, logger) 85 89 @connection_options, @config = connection_options, config … … 274 278 def indexes(table_name, name = nil)#:nodoc: 275 279 indexes = [] 276 280 current_index = nil 281 limit = {} 277 282 execute("SHOW KEYS FROM #{table_name}", name).each do |row| 278 283 if current_index != row[2] 279 284 next if row[2] == "PRIMARY" # skip the primary key 280 285 current_index = row[2] 281 286 indexes << IndexDefinition.new(row[0], row[2], row[1] == "0", []) 282 287 end 283 288 if !row[7].nil? 289 indexes.last.limit[row[4]] = row[7].to_i 290 end 284 291 indexes.last.columns << row[4] 285 292 end 286 293 indexes -
activerecord/lib/active_record/schema_dumper.rb
old new 113 113 indexes.each do |index| 114 114 stream.print " add_index #{index.table.inspect}, #{index.columns.inspect}, :name => #{index.name.inspect}" 115 115 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? 116 117 stream.puts 117 118 end 118 119 stream.puts unless indexes.empty?