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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With