Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix GH #3931. A text type column with limit is returned incorrect data type on mysql. #4382

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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

Expand Down
31 changes: 31 additions & 0 deletions activerecord/test/cases/migration_test.rb
Expand Up @@ -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