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

Ticket #4853: mysql_key_length.df

File mysql_key_length.df, 6.0 kB (added by martin.kihlgren@adocca.com, 2 years ago)

the new and improved patch!

Line 
1 Index: test/migration_test.rb
2 ===================================================================
3 --- test/migration_test.rb      (revision 4347)
4 +++ test/migration_test.rb      (working copy)
5 @@ -50,6 +50,7 @@
6        Person.connection.add_column "people", "last_name", :string       
7        Person.connection.add_column "people", "administrator", :boolean
8        Person.connection.add_column "people", "key", :string
9 +      Person.connection.add_column "people", "longtext", :text
10        
11        assert_nothing_raised { Person.connection.add_index("people", "last_name") }
12        assert_nothing_raised { Person.connection.remove_index("people", "last_name") }
13 @@ -61,6 +62,10 @@
14        assert_nothing_raised { Person.connection.add_index("people", ["key"], :name => "key", :unique => true) }
15        assert_nothing_raised { Person.connection.remove_index("people", :name => "key", :unique => true) }
16  
17 +      # key length
18 +      assert_nothing_raised { Person.connection.add_index("people", ["longtext"], :name => "longtext", :limit => {"longtext" => 10}) }
19 +      assert_nothing_raised { Person.connection.remove_index("people", :name => "longtext") }
20 +
21        # Sybase adapter does not support indexes on :boolean columns
22        unless current_adapter?(:SybaseAdapter)
23          assert_nothing_raised { Person.connection.add_index("people", %w(last_name first_name administrator), :name => "named_admin") }
24 Index: lib/active_record/connection_adapters/abstract/schema_statements.rb
25 ===================================================================
26 --- lib/active_record/connection_adapters/abstract/schema_statements.rb (revision 4347)
27 +++ lib/active_record/connection_adapters/abstract/schema_statements.rb (working copy)
28 @@ -190,11 +190,17 @@
29          if Hash === options # legacy support, since this param was a string
30            index_type = options[:unique] ? "UNIQUE" : ""
31            index_name = options[:name] || index_name
32 +          limit = {}
33 +          if options.include?(:limit) && self.supports_subkey_length?
34 +            options[:limit].each do |col, lim|
35 +              limit[col] = lim
36 +            end
37 +          end
38          else
39            index_type = options
40          end
41          quoted_column_names = column_names.map { |e| quote_column_name(e) }.join(", ")
42 -        execute "CREATE #{index_type} INDEX #{quote_column_name(index_name)} ON #{table_name} (#{quoted_column_names})"
43 +        execute "CREATE #{index_type} INDEX #{quote_column_name(index_name)} ON #{table_name} (#{column_names.collect do |col| "#{quote_column_name(col)}#{limit.include?(col) ? "(#{limit[col]})" : ""}" end.join(", ")})"
44        end
45  
46        # Remove the given index from the table.
47 Index: lib/active_record/connection_adapters/abstract/schema_definitions.rb
48 ===================================================================
49 --- lib/active_record/connection_adapters/abstract/schema_definitions.rb        (revision 4347)
50 +++ lib/active_record/connection_adapters/abstract/schema_definitions.rb        (working copy)
51 @@ -162,7 +162,15 @@
52          end
53      end
54  
55 -    class IndexDefinition < Struct.new(:table, :name, :unique, :columns) #:nodoc:
56 +    class IndexDefinition
57 +      attr_accessor :table, :name, :unique, :columns, :limit
58 +      def initialize(table, name, unique, columns, limit = {})
59 +        @table = table
60 +        @name = name
61 +        @unique = unique
62 +        @columns = columns
63 +        @limit = limit
64 +      end
65      end
66  
67      class ColumnDefinition < Struct.new(:base, :name, :type, :limit, :default, :null) #:nodoc:
68 Index: lib/active_record/connection_adapters/abstract_adapter.rb
69 ===================================================================
70 --- lib/active_record/connection_adapters/abstract_adapter.rb   (revision 4347)
71 +++ lib/active_record/connection_adapters/abstract_adapter.rb   (working copy)
72 @@ -39,7 +39,15 @@
73        def supports_migrations?
74          false
75        end
76 -     
77 +
78 +      #
79 +      # Does this adapter support subkey length in CREATE INDEX statements?
80 +      # Ex: CREATE INDEX users_name_index ON users (name(20))
81 +      #     
82 +      def supports_subkey_length?
83 +        false
84 +      end
85 +
86        # Does this adapter support using DISTINCT within COUNT?  This is +true+
87        # for all adapters except sqlite.
88        def supports_count_distinct?
89 Index: lib/active_record/connection_adapters/mysql_adapter.rb
90 ===================================================================
91 --- lib/active_record/connection_adapters/mysql_adapter.rb      (revision 4347)
92 +++ lib/active_record/connection_adapters/mysql_adapter.rb      (working copy)
93 @@ -80,6 +80,10 @@
94          "MySQL server has gone away"
95        ]
96  
97 +      def supports_subkey_length?
98 +        true
99 +      end
100 +
101        def initialize(connection, logger, connection_options, config)
102          super(connection, logger)
103          @connection_options, @config = connection_options, config
104 @@ -274,13 +278,16 @@
105        def indexes(table_name, name = nil)#:nodoc:
106          indexes = []
107          current_index = nil
108 +        limit = {}
109          execute("SHOW KEYS FROM #{table_name}", name).each do |row|
110            if current_index != row[2]
111              next if row[2] == "PRIMARY" # skip the primary key
112              current_index = row[2]
113              indexes << IndexDefinition.new(row[0], row[2], row[1] == "0", [])
114            end
115 -
116 +          if !row[7].nil?
117 +            indexes.last.limit[row[4]] = row[7].to_i
118 +          end
119            indexes.last.columns << row[4]
120          end
121          indexes
122 Index: lib/active_record/schema_dumper.rb
123 ===================================================================
124 --- lib/active_record/schema_dumper.rb  (revision 4347)
125 +++ lib/active_record/schema_dumper.rb  (working copy)
126 @@ -127,6 +127,7 @@
127          indexes.each do |index|
128            stream.print "  add_index #{index.table.inspect}, #{index.columns.inspect}, :name => #{index.name.inspect}"
129            stream.print ", :unique => true" if index.unique
130 +          stream.print ", :limit => {#{index.limit.keys.collect do |key| "\"#{key}\" => #{index.limit[key]}" end.join(", ")}}" if !index.limit.empty?
131            stream.puts
132          end
133          stream.puts unless indexes.empty?