Migration code not generating in schema.rb?
I had to put execute
migrations in one table. 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 the db/schema.rb
file that must be authoritative to create the database is missing on this line with the command execute
.
Is there something I am missing or is this the default behavior on creation schema.rb
?
I can work around this problem by simply ignoring schema.rb
and generating the tables with rake db:migrate
when deploying, but I've seen guidelines to avoid this.
Any ideas?
source to share
The active record schema dumper cannot handle database items such as foreign keys, constraints, grant statements, etc. Change your database format to sql
instead ruby
. In the application.rb file:
config.active_record.schema_format = :sql
This will use a database specific tool to reset the schema to db/structure.sql
. For example, if you are using PostgreSQL it will use pg_dump
to dump the schema. The downside to using the format sql
is that the dump is no longer database agnostic. If you have to migrate from PostgreSQL to MySQL, you will not be able to use the generated structure file to create a new database.
You can also dump sql with rake command:
rake db:structure:dump
source to share