2014-10-27 50 views
3

我不得不将execute放到一个表中迁移。它看起来像这样:不在schema.rb中生成的迁移代码?

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 

这种运作良好,但db/schema.rb文件,这应该是对数据库的创建权限,却丢失了符合execute命令。

有什么我很想念,或者这是schema.rb生成时的默认行为?

我可以通过简单忽略schema.rb并在部署时生成带有rake db:migrate的表格来绕过此问题,但我看到了避免这样做的建议。

任何想法?

+0

我从来没有见过“GRANT”显示在schema.rb只有'tables'和'indexes'。另外,你在哪里找到避免使用'rake db:migrate'的建议? – 2014-10-27 14:40:28

回答

3

Active Record的模式转储程序无法处理数据库特定的项目,如外键,约束,授权语句等。将数据库格式更改为sql而不是ruby。在你的application.rb中的文件:

config.active_record.schema_format = :sql 

这将使用数据库的特定工具的架构转储到db/structure.sql。例如,如果您使用PostgreSQL,它将使用pg_dump来转储模式。使用sql格式的缺点是转储不再是数据库不可知的。如果您要从PostgreSQL迁移到MySQL,您将无法使用生成的结构文件来创建新的数据库。

您还可以生成耙命令SQL转储:

rake db:structure:dump