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.
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.
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
$0
may not be "rake" (zeus lets you set aliases). –
Appendant 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-command –
Irby Use the following code to disable/enable specific code during migrations:
if !ARGV.include?("db:migrate") )
config.active_record.observers = :user_observer
end
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.
ActiveRecord::Base.connection.migration_context.needs_migration?
now. https://mcmap.net/q/176618/-nomethoderror-undefined-method-needs_migration-39-for-activerecord-migrator-class –
Chemotaxis 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?
© 2022 - 2024 — McMap. All rights reserved.