Search code examples
ruby-on-railsdatabase-migrationinitializer

Rails initializers are running while migrating database


It is very surprising that Rails's initializers run while running any rake task, including db:migrate and db:seed.

An initializer in my app starts a background thread (a kind of worker process), and it should be executed only when the application is running in debug and production mode.

How to prevent a specific initializer from running when doing rake db:migrate or how to detect in initializer that a rake task is running?


Solution

  • Here is a solution how to prevent an initializer from running in Rake task:

    unless ( File.basename($0) == 'rake')
       # Initializer code
    end