3

我有一些从初始化程序运行的代码,它工作正常。(它将默认设置从 yaml 文件保存到rails-settings-cache gem 的数据库。)

但是当我在 Travis CI 上运行它时,由于它是从头开始迁移,初始化程序失败,因为表还不存在。

有没有办法在迁移之后但在应用程序启动之前运行代码?

4

1 回答 1

13

So while I don't love doing this, an easy way to prevent the initializer from running during db:migrate, but running on application start or test run is to wrap it in a clause testing if the table exists. So if you take your existing initializer code and wrap it in

if ActiveRecord::Base.connection.table_exists? 'table_name'
   ....
end

where 'table_name' is the name of the missing table, then both rake db:migrate and the spec run should be able to complete successfully.

于 2013-11-07T16:33:49.600 回答