17

Is there some easy way to detect it?

I want to skip some code in the envirmonment.rb file when the rake/rails migrations are running.

3
  • What do you mean with "is running in migration"? The question doesn't really make sense. :S Dec 7, 2009 at 10:46
  • wow i dont get your question? yea what do u mean? Dec 7, 2009 at 13:21
  • 2
    I don't known in envirmonment.rb whether my app is running from rake db:migrate or ruby script/server. If it's db:migrate, and i write some db query in envirmonment.rb, rails cannot do migrate because talbe does not exist yet.
    – user199403
    Dec 7, 2009 at 16:16

4 Answers 4

31

I had this problem in a legacy application I was maintaining. There were some observers that were interfering with migrations past a certain point, so I disabled them during migration by checking the application name and arguments

  # Activate observers that should always be running
  # config.active_record.observers = :cacher, :garbage_collector, :forum_observer# observers break a migrate from VERSION xxx - disable them for rake db:migrate
unless ( File.basename($0) == "rake" && ARGV.include?("db:migrate") )
  config.active_record.observers = :user_observer
end

Incorporating the comment below by @strw667, in Rails 6.1:

  # Activate observers that should always be running
  # config.active_record.observers = :cacher, :garbage_collector, :forum_observer# observers break a migrate from VERSION xxx - disable them for rake db:migrate
unless (File.basename($0) == "rake" &&  Rake.application.top_level_tasks == ["db:migrate")
  config.active_record.observers = :user_observer
end
5
  • 1
    Great! Works just as well for optional gems that you only need in a rake task, but not in prod (and that may in fact interfere with your local script/server). Example: config.gem 'fsevents' if RAILS_ENV == 'development' && File.basename($0) == "rake"
    – Aeon
    Oct 23, 2010 at 23:32
  • Nice. $0 gives you the file name from where application starts. For script/console it is irb ( in jruby it is jirb).
    – so_mv
    Dec 4, 2012 at 3:12
  • 1
    Now that there are app preloaders like zeus, this trick may not work any more because $0 may not be "rake" (zeus lets you set aliases).
    – Kelvin
    Mar 25, 2015 at 15:38
  • It appears that this doesn't work any more since Rails 6.1 due to ARGV not being populated. Anyone know an alternative? c.f: stackoverflow.com/questions/67124237/…
    – stwr667
    May 30, 2022 at 5:46
  • 1
    In Rails 6.1 you can use File.basename($0) == "rake" && Rake.application.top_level_tasks == ["db:migrate"]. C.f: stackoverflow.com/a/67287175/1852005
    – stwr667
    May 30, 2022 at 6:21
2

Use the following code to disable/enable specific code during migrations:

if !ARGV.include?("db:migrate") )
  config.active_record.observers = :user_observer
end
1

If you are running code that need the DB up to date I would suggest:

ActiveRecord::Base.connected? # This returns false if the db couldn't be connected to.
&& !ActiveRecord::Migrator.needs_migration? # This checks if a migration needs to run.

This will handle if you are running other db: tasks like db:setup.

1
-4

i think if u want to skip, just comment (#) on code.

or many choose on migration rake.

for example : rake db:migrate:up VERSION=2000123232 its mean , only 2000123232_create_article do migration.

or rake db:migrate VERSION=2000123232 mean start from after 2000123232

or rake db:migrate:down VERSION=2000123232

just rake help u can see what u need to rake.

Do you mean that?

1
  • Your answer is hard to understand (poor english) and you're not answering the question.
    – ZedTuX
    Jan 31, 2020 at 9:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.