Skip to content
This repository
Browse code

Fix type_to_sql with text and limit on mysql/mysql2. Fix GH #3931.

  • Loading branch information...
commit fe7cacb5106cd240e4d18ae518fa4e56c7e4ec5d 1 parent 29054ba
Toshinori Kajihara authored February 26, 2012
29  activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
@@ -484,15 +484,26 @@ def rename_column(table_name, column_name, new_column_name) #:nodoc:
484 484
 
485 485
       # Maps logical Rails types to MySQL-specific data types.
486 486
       def type_to_sql(type, limit = nil, precision = nil, scale = nil)
487  
-        return super unless type.to_s == 'integer'
488  
-
489  
-        case limit
490  
-        when 1; 'tinyint'
491  
-        when 2; 'smallint'
492  
-        when 3; 'mediumint'
493  
-        when nil, 4, 11; 'int(11)'  # compatibility with MySQL default
494  
-        when 5..8; 'bigint'
495  
-        else raise(ActiveRecordError, "No integer type has byte size #{limit}")
  487
+        case type.to_s
  488
+        when 'integer'
  489
+          case limit
  490
+          when 1; 'tinyint'
  491
+          when 2; 'smallint'
  492
+          when 3; 'mediumint'
  493
+          when nil, 4, 11; 'int(11)'  # compatibility with MySQL default
  494
+          when 5..8; 'bigint'
  495
+          else raise(ActiveRecordError, "No integer type has byte size #{limit}")
  496
+          end
  497
+        when 'text'
  498
+          case limit
  499
+          when 0..0xff;               'tinytext'
  500
+          when nil, 0x100..0xffff;    'text'
  501
+          when 0x10000..0xffffff;     'mediumtext'
  502
+          when 0x1000000..0xffffffff; 'longtext'
  503
+          else raise(ActiveRecordError, "No text type has character length #{limit}")
  504
+          end
  505
+        else
  506
+          super
496 507
         end
497 508
       end
498 509
 
4  activerecord/test/schema/mysql2_specific_schema.rb
... ...
@@ -1,5 +1,5 @@
1 1
 ActiveRecord::Schema.define do
2  
-  create_table :binary_fields, :force => true, :options => 'CHARACTER SET latin1' do |t|
  2
+  create_table :binary_fields, :force => true do |t|
3 3
     t.binary :tiny_blob,   :limit => 255
4 4
     t.binary :normal_blob, :limit => 65535
5 5
     t.binary :medium_blob, :limit => 16777215
@@ -32,4 +32,4 @@
32 32
 ) CHARACTER SET utf8 COLLATE utf8_general_ci
33 33
 SQL
34 34
 
35  
-end
  35
+end
2  activerecord/test/schema/mysql_specific_schema.rb
... ...
@@ -1,5 +1,5 @@
1 1
 ActiveRecord::Schema.define do
2  
-  create_table :binary_fields, :force => true, :options => 'CHARACTER SET latin1' do |t|
  2
+  create_table :binary_fields, :force => true do |t|
3 3
     t.binary :tiny_blob,   :limit => 255
4 4
     t.binary :normal_blob, :limit => 65535
5 5
     t.binary :medium_blob, :limit => 16777215

0 notes on commit fe7cacb

Please sign in to comment.
Something went wrong with that request. Please try again.