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 Commented Dec 7, 2009 at 10:46
  • wow i dont get your question? yea what do u mean? Commented 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. Commented 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 
Sign up to request clarification or add additional context in comments.

5 Comments

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"
Nice. $0 gives you the file name from where application starts. For script/console it is irb ( in jruby it is jirb).
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).
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/…
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
2

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

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

Comments

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 Comment

This is ActiveRecord::Base.connection.migration_context.needs_migration? now. stackoverflow.com/a/50810130/2066546
-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 Comment

Your answer is hard to understand (poor english) and you're not answering the question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.