在schema.rb中没有生成迁移代码?

4

我必须将execute放入一个表迁移中。代码如下:

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 生成表格来绕过此问题,但我看到建议避免这样做。
有什么想法吗?

我从未在 schema.rb 中看到过 'GRANT',只有 tablesindexes。另外,请问您在哪里找到了不使用 rake db:migrate 的建议? - Ben
1个回答

3

Active Record的schema dumper无法处理数据库特定项,例如外键、约束、授权语句等。请将数据库格式更改为sql而非ruby。在您的application.rb文件中:

config.active_record.schema_format = :sql

这将使用数据库专用工具将模式转储到 db/structure.sql。例如,如果您正在使用PostgreSQL,则会使用pg_dump来转储模式。使用sql格式的缺点是该转储不再与数据库无关。如果您要从PostgreSQL迁移到MySQL,则无法使用生成的结构文件创建新的数据库。
您也可以使用rake命令生成sql转储:
rake db:structure:dump

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接