如何在environment.rb中检测我的rails是否在迁移中运行

时间:2009-12-07 06:32:56

标签: ruby-on-rails rake migrate

任何简单的方法来检测它?我想在进行迁移rake时跳过environment.rb中的一些代码。

4 个答案:

答案 0 :(得分:26)

我在维护的遗留应用程序中遇到此问题。有些观察员在某一点上干扰了迁移,所以我在迁移过程中通过检查应用程序名称和参数来禁用它们

  # 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

答案 1 :(得分:0)

使用以下代码在迁移过程中禁用/启用特定代码:

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

答案 2 :(得分:0)

如果您正在运行需要最新数据库的代码,我建议:

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.

如果您正在运行其他 db: 任务,例如 db:setup,这将处理。

答案 3 :(得分:-2)

我想如果你想跳过,只需对代码发表评论(#)。

或许多人选择迁移佣金。

例如:rake db:migrate:up VERSION = 2000123232它的意思是,只有2000123232_create_article才能进行迁移。

或rake db:migrate VERSION = 2000123232表示从2000123232之后开始

或rake db:migrate:down VERSION = 2000123232

只需耙帮助你就可以看到你需要耙的东西了。

你的意思是?