4

I had to put execute into one table migration. It looks like this:

class CreateFoos < ActiveRecord::Migration
  def up
    create_table :items do |t|
      t.integer :bar
    end

    execute("GRANT SELECT ON items TO otheruser;")
  end

  def down
    drop_table :items
  end
end

This works well, but db/schema.rb file, which should be authority for database creation, is missing that line with execute command.

Is there something I'm missing or this is default behavior when schema.rb is generated?

I can bypass this issue by simply ignoring schema.rb and generating tables with rake db:migrate when deploying, but I saw recommendations to avoid doing so.

Any ideas?

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
10robinho
  • 520
  • 6
  • 17
  • i've never seen 'GRANT' show up in schema.rb only `tables` and `indexes`. Also, where did you find recommendation to avoid using `rake db:migrate`? – Ben Oct 27 '14 at 14:40

1 Answers1

3

Active Record's schema dumper can't handle database specific items like foreign keys, constraints, grant statements, etc. Change your database format to sql instead of of ruby. In your application.rb file:

config.active_record.schema_format = :sql

This will use a database specific tool to dump the schema to db/structure.sql. For example, if you are using PostgreSQL it will use pg_dump to dump the schema. The drawback to using the sql format is that the dump is no longer database agnostic. If you were to migrate from PostgreSQL to MySQL you would not be able to use the generated structure file to create a new database.

You can also generate a sql dump with the rake command:

rake db:structure:dump
Lukas Eklund
  • 6,068
  • 1
  • 32
  • 33