Rails 2.3.x migrations with mediumtext and longtext data types for MySQL
Yesterday I needed to change a column from type TEXT
to MEDIUMTEXT
via a migration in a Rails 2.3.x app.
MySQL have the following TEXT
data types:
- TINYTEXT - 256 bytes
- TEXT - 65,535 bytes
- MEDIUMTEXT - 16,777,215 bytes
- LONGTEXT - 4,294,967,295 bytes
The way how Rails choose the proper data type is based on the :limit
option.
However, the behavior in Rails 2.3.x is a little weird. Rails will round up the :limit
value to one of those four TEXT
type limits.
Examples:
t.text :foo, :limit => 100
will generate a TINYTEXT column with a limit of 256 bytes
t.text :foo :limit => 64.kilobytes + 1
will generate a MEDIUMTEXT column with a limit of 16,777,215 bytes.
That was a Rails bug, and was solved for Rails 4 and backported to Rails 3.2 and 3.1.