Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Migrations code not generated in schema.rb?

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?

like image 603
10robinho Avatar asked Sep 16 '25 02:09

10robinho


1 Answers

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
like image 189
Lukas Eklund Avatar answered Sep 19 '25 13:09

Lukas Eklund