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.

Sebastián Palma
  • 32,692
  • 6
  • 40
  • 59
user199403
  • 483
  • 1
  • 6
  • 11
  • What do you mean with "is running in migration"? The question doesn't really make sense. :S – Simone Carletti Dec 07 '09 at 10:46
  • wow i dont get your question? yea what do u mean? – Ahmad Ramdani Dec 07 '09 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 07 '09 at 16:16

4 Answers4

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
Jeff Paquette
  • 7,089
  • 2
  • 31
  • 40
  • 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 '10 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 04 '12 at 03: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 '15 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: https://stackoverflow.com/questions/67124237/how-to-get-argv-options-on-rails-6-1 – stwr667 May 30 '22 at 05:46
  • 1
    In Rails 6.1 you can use `File.basename($0) == "rake" && Rake.application.top_level_tasks == ["db:migrate"]`. C.f: https://stackoverflow.com/a/67287175/1852005 – stwr667 May 30 '22 at 06: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
Weston Ganger
  • 6,324
  • 4
  • 41
  • 39
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.

Kelly Stannard
  • 331
  • 2
  • 6
  • This is `ActiveRecord::Base.connection.migration_context.needs_migration?` now. https://stackoverflow.com/a/50810130/2066546 – fiedl Feb 07 '23 at 22:24
-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?

Ahmad Ramdani
  • 208
  • 4
  • 14