Permalink
Browse files

Fix incorrect text type limit on mysql. Fix GH #3931.

  • Loading branch information...
1 parent a3b3643 commit 9765cf5162488d8fead055b0c5059081d28d33b1 @kennyj committed Jan 25, 2012
@@ -474,15 +474,27 @@ def rename_column(table_name, column_name, new_column_name) #:nodoc:
# Maps logical Rails types to MySQL-specific data types.
def type_to_sql(type, limit = nil, precision = nil, scale = nil)
- return super unless type.to_s == 'integer'
-
- case limit
- when 1; 'tinyint'
- when 2; 'smallint'
- when 3; 'mediumint'
- when nil, 4, 11; 'int(11)' # compatibility with MySQL default
- when 5..8; 'bigint'
- else raise(ActiveRecordError, "No integer type has byte size #{limit}")
+ case type.to_s
+ when 'integer'
+ case limit
+ when 1; 'tinyint'
+ when 2; 'smallint'
+ when 3; 'mediumint'
+ when nil, 4, 11; 'int(11)' # compatibility with MySQL default
+ when 5..8; 'bigint'
+ else raise(ActiveRecordError, "No integer type has byte size #{limit}")
+ end
+ when 'text'
+ case limit
+ when nil; 'text'
+ when 0..0xff; 'tinytext'
+ when 0x100..0xffff; 'text'
+ when 0x10000..0xffffff; 'mediumtext'
+ when 0x1000000..0xffffffff; 'longtext'
+ else raise(ActiveRecordError, "No text type has character length #{limit}")
+ end
+ else
+ super
end
end
@@ -944,4 +944,35 @@ def test_copying_migrations_to_empty_directory
ensure
clear
end
+
+ if current_adapter?(:MysqlAdapter) or current_adapter?(:Mysql2Adapter)
+ def test_text_column_with_limits
+ assert_nothing_raised do
+ Person.connection.create_table :testings do |t|
+ t.column :tinytext, :text, :limit => 0xff
+ t.column :text , :text, :limit => 0xffff
+ t.column :text_default, :text
+ t.column :mediumtext, :text, :limit => 0xffffff
+ t.column :longtext, :text, :limit => 0xffffffff
+ end
+ end
+
+ columns = Person.connection.columns(:testings)
+
+ tiny = columns.detect { |c| c.name == "tinytext" }
+ text = columns.detect { |c| c.name == "text" }
+ text_default = columns.detect { |c| c.name == "text_default" }
+ medium = columns.detect { |c| c.name == "mediumtext" }
+ long = columns.detect { |c| c.name == "longtext" }
+
+ assert_match 'tinytext', tiny.sql_type
+ assert_match 'text', text.sql_type
+ assert_match 'text', text_default.sql_type
+ assert_match 'mediumtext', medium.sql_type
+ assert_match 'longtext', long.sql_type
+ ensure
+ Person.connection.drop_table :testings rescue nil
+ end
+ end
+
end

0 comments on commit 9765cf5

Please sign in to comment.