Skip to content
This repository
Browse code

Backport of fix from #5173 - fixes #7252

Rather than use the MySQL specific TINYTEXT, MEDIUMTEXT and LONGTEXT
datatypes, Active Record migrations use TEXT(n) where n is the limit
specified by the developer. Unfortunately how MySQL interprets n
depends on the column's encoding so any limit above 5592405 will be
interpreted as a LONGTEXT when the encoding is UTF-8.

This commit fixes this by interpreting the limit within the adapter
and using the specific MySQL datatype as appropriate.
  • Loading branch information...
commit f07c7085795c64f75d6b9a4ea55689322b7b9a1f 1 parent 3166606
Andrew White authored August 04, 2012
2  Gemfile
@@ -41,7 +41,7 @@ platforms :ruby do
41 41
   group :db do
42 42
     gem "pg", ">= 0.9.0"
43 43
     gem "mysql", ">= 2.8.1"
44  
-    gem "mysql2", "~> 0.2.17"
  44
+    gem "mysql2", :git => "git://github.com/brianmario/mysql2.git", :branch => "0.2.x"
45 45
   end
46 46
 end
47 47
 
4  activerecord/CHANGELOG
... ...
@@ -1,3 +1,7 @@
  1
+## Rails 3.0.17 (unreleased)
  2
+
  3
+* Fix type_to_sql with text and limit on mysql/mysql2 (GH #7252)
  4
+
1 5
 ## Rails 3.0.16 (Jul 26, 2012)
2 6
 
3 7
 *   No changes.
29  activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
@@ -522,15 +522,26 @@ def rename_column(table_name, column_name, new_column_name) #:nodoc:
522 522
 
523 523
       # Maps logical Rails types to MySQL-specific data types.
524 524
       def type_to_sql(type, limit = nil, precision = nil, scale = nil)
525  
-        return super unless type.to_s == 'integer'
526  
-
527  
-        case limit
528  
-        when 1; 'tinyint'
529  
-        when 2; 'smallint'
530  
-        when 3; 'mediumint'
531  
-        when nil, 4, 11; 'int(11)'  # compatibility with MySQL default
532  
-        when 5..8; 'bigint'
533  
-        else raise(ActiveRecordError, "No integer type has byte size #{limit}")
  525
+        case type.to_s
  526
+        when 'integer'
  527
+          case limit
  528
+          when 1; 'tinyint'
  529
+          when 2; 'smallint'
  530
+          when 3; 'mediumint'
  531
+          when nil, 4, 11; 'int(11)'  # compatibility with MySQL default
  532
+          when 5..8; 'bigint'
  533
+          else raise(ActiveRecordError, "No integer type has byte size #{limit}")
  534
+          end
  535
+        when 'text'
  536
+          case limit
  537
+          when 0..0xff;               'tinytext'
  538
+          when nil, 0x100..0xffff;    'text'
  539
+          when 0x10000..0xffffff;     'mediumtext'
  540
+          when 0x1000000..0xffffffff; 'longtext'
  541
+          else raise(ActiveRecordError, "No text type has character length #{limit}")
  542
+          end
  543
+        else
  544
+          super
534 545
         end
535 546
       end
536 547
 
2  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
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 f07c708

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