2

I have a migration for a model with a binary field meant to store a file that can be bigger than 10Mb:

class CreateNewModel < ActiveRecord::Migration
  def change
    create_table :new_model do |t|
      ...
      t.binary :data, limit: 16777216
      ...
    end
  end
end

With the limit information the migration can create a longblob object in a MySQL or MariaDB database, as seen in How do you get Rails to use the LONGBLOB column in mysql?.

The migration seems to work fine on a MariaDB database: data has longblob type. However loading directly from the schema gives data a blob type instead of longblob, this means the rake db:setup command is no longer usable as the schema doesn't reflect the database I want.

This seems pretty evident when one looks at the db/schema.rb file:

create_table "new_model", force: :cascade do |t|
  ...
  t.binary   "data"
  ...
  t.datetime "created_at",        null: false
  t.datetime "updated_at",        null: false
end

There is no limit info and as such loading the schema can only lead to blobs and not longblobs.

Why doesn't the limit information get written in the schema?

I don't want to change the schema manually as it would mean have to redo the change at each migration (as they regenerate the schema file). What other solutions do I have, is there a way to force the limit field from the migration to the schema?

I've tried using the shorthands described in https://github.com/rails/rails/pull/21688 but it doesn't seem to exist in Rails 4.2.6.

Ixio
  • 517
  • 6
  • 21
  • You may find that big files are better left as files. What will you be doing with the file? Is it a video or some other media? – Rick James Apr 13 '16 at 22:30
  • @RickJames : It's mostly images or PDFs that need to be stored somewhere, there is only a limited number of them per user and they are size limited (shouldn't be bigger then 15Mb). The way my code & stack is I only need to backup the database which I find very helpfull. – Ixio Apr 14 '16 at 08:06

0 Answers0