Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: running an initializer after migrate

I have some code which I run from an initializer and it works fine. (It saves default settings from a yaml file to the database for the rails-settings-cache gem.)

But when I run this on Travis CI, since it is doing a migration from scratch, the initializer fails because the table does not exist yet.

Is there a way of running code after the migration but before the application starts?

like image 568
Joe Gatt Avatar asked Oct 15 '25 04:10

Joe Gatt


1 Answers

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.

like image 80
Peter Goldstein Avatar answered Oct 17 '25 09:10

Peter Goldstein