how to get ActiveRecord Schema Dumper to dump out larger columns
(This assumes you are using mysql, but similar changes will work for postgres, etc.)
If you are using Ruby on Rails and are trying to store large objects in a column, you may run into the following problem:
create_table :bogus do |t|
t.text :big_column, :limit => 1024*1024
endThat will create the table as expected, but mysql with automatically turn the text column into mediumtext (rails/ActiveRecord has no idea). The same think can happen with longtext, mediumblog, longblob, etc.
That’s all well and good, but when you run schema dumper (e.g., rake db:schema:dump), you’ll get this:
create_table :bogus do |t|
t.text :big_column
endand then you are screwed. Especially when you go to run tests (which can invoke the schema dumping).
There is a fairly easy fix for this. There is even a patch for it on the rails trac site (thanks Will Bryant!). But, until it gets fixed in the rails core…
Assuming you are using Rails 2.0 (edgerails as of now), which introduces support for initializers, create a file config/intializers/active_record_tweak.rb:
##
## from http://dev.rubyonrails.org/ticket/7424
##
module ActiveRecord
module ConnectionAdapters
class MysqlColumn
private
def extract_limit(sql_type)
if sql_type =~ /blob|text/i
case sql_type
when /tiny/i
255
when /medium/i
16777215
when /long/i
2147483647 # mysql only allows 2^31-1, not 2^32-1, somewhat inconsistently with the tiny/medium/normal cases
else
super # we could return 65535 here, but we leave it undecorated by default
end
else
super
end
end
end
end
end(If you have an earlier version of Rails, just put this code in config/environment.rb)
This isn’t perfect – the original limit information in your migration will get lost, replaced with whatever the maximum is for that column, but it’s a quick fix to get everything working again.
NOTE: If this solves a problem you were having, please go to the original ticket on railstrac and add a comment (you have to register and login to add a comment) saying:
+1 solves the problem without side-effects for me
(See this report for more info on this comment.)