Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if Rails code is running within a migration

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.

like image 573
user199403 Avatar asked Dec 07 '09 06:12

user199403


People also ask

How can I check my rails migration status?

rake db:migrate:status (Rails 3 to 5) or rails db:migrate:status (Rails 5) will accomplish this. See this commit. up means the migration has been run. down means the migration has not been run.

How rails know which migration is pending?

Rails provides a configuration option to indicate if we want to be alerted if any migration is pending. This will raise an error of the pending migrations and notify the user as shown in the image below. This can be disabled with the false value for the option as shown below.

How do I run a specific rail in migration?

To run a specific migration up or down, use db:migrate:up or db:migrate:down . The version number in the above commands is the numeric prefix in the migration's filename. For example, to migrate to the migration 20160515085959_add_name_to_users. rb , you would use 20160515085959 as the version number.

How does rails migration work internally?

A Rails migration is a tool for changing an application's database schema. Instead of managing SQL scripts, you define database changes in a domain-specific language (DSL). The code is database-independent, so you can easily move your app to a new platform.


1 Answers

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
like image 129
Jeff Paquette Avatar answered Oct 02 '22 19:10

Jeff Paquette