Rails migrations for large text columns

May 24th, 2008 Rails

Sometimes you need big text columns in your database.

For example, I'm using bj to asynchronously process large xml files.

By default, the bj database migrations create :text columns types which map to mysql TEXT columns. In mysql, TEXT columns are limited to 64K of data. Luckily there are the MEDIUMTEXT and LONGTEXT column types for holding large data.

So how do you get these types when using Rails migrations?

Just specify the limit:


class AdjustBjColumnTypes < ActiveRecord::Migration
  def self.up
    # this bumps col from TEXT to MEDIUMTEXT in mysql
    change_column :bj_job, :stdin, :text, :limit => 64.kilobytes + 1
    change_column :bj_job_archive, :stdin, :text, :limit => 64.kilobytes + 1
  end

  def self.down
    # this bumps col from MEDIUMTEXT to TEXT in mysql
    change_column :bj_job, :stdin, :text
    change_column :bj_job_archive, :stdin, :text
  end
end

References

Mysql Data Type Storage Requirements

--- --- ---

1 Comment

  1. Comment by ook on 06/08/08
    Exactly what I need, when I need it! Thank you for posting, you prevent me a long search and test cycle ;)

Commenting is closed for this article.