仅在测试环境上运行迁移脚本

dsp*_*cer 2 testing unit-testing test-environments rails-migrations ruby-on-rails-3.2

有没有一种方法只能在测试环境中运行迁移?

由于暂存和生产数据库已经存在,因此我只想在测试环境中创建表并将数据植入种子。

dsp*_*cer 5

我已经根据这里的帖子找到了解决方案。这使我只能在测试环境中应用迁移。

class CreateLicenseDatabase < ActiveRecord::Migration
  def change
    if Rails.env.test?

      create_table.....

    end
  end
end
Run Code Online (Sandbox Code Playgroud)

  • 我只想在测试环境中运行一些迁移的原因是,我只想为测试环境复制旧数据库。 (2认同)