How to check if Rails code is running within a migration
Asked Answered
A

4

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.

Ammoniate answered 7/12, 2009 at 6:32 Comment(3)
What do you mean with "is running in migration"? The question doesn't really make sense. :SIronwork
wow i dont get your question? yea what do u mean?Tyrocidine
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.Ammoniate
K
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
Kalil answered 7/12, 2009 at 17:26 Comment(5)
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"Lillia
Nice. $0 gives you the file name from where application starts. For script/console it is irb ( in jruby it is jirb).Humbuggery
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).Appendant
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: #67124737Irby
In Rails 6.1 you can use File.basename($0) == "rake" && Rake.application.top_level_tasks == ["db:migrate"]. C.f: https://mcmap.net/q/176617/-how-do-i-detect-in-rails-if-i-am-running-a-rake-commandIrby
N
2

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

if !ARGV.include?("db:migrate") )
  config.active_record.observers = :user_observer
end
Neighbors answered 16/12, 2020 at 2:16 Comment(0)
C
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.

Chitin answered 8/6, 2021 at 15:11 Comment(1)
This is ActiveRecord::Base.connection.migration_context.needs_migration? now. https://mcmap.net/q/176618/-nomethoderror-undefined-method-needs_migration-39-for-activerecord-migrator-classChemotaxis
T
-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?

Tyrocidine answered 7/12, 2009 at 13:27 Comment(1)
Your answer is hard to understand (poor english) and you're not answering the question.Jibe

© 2022 - 2024 — McMap. All rights reserved.