Rails – 在特定环境中跳过数据迁移

huangapple go评论87阅读模式
英文:

Rails - Skip data_migrations in specific environments

问题

我正在为数据迁移编写代码,我正在使用data-migrate gem。我有一个情况,我应该只在生产环境中跳过一个特定的数据迁移。我可以这样做

class BackfillNonStatutoryResultsWithAssessmentPeriod < ActiveRecord::Migration[7.0]
  def up 
    unless RAILS_ENV == "production"
     # 做一些事情
    end
  end
end

 def down
   raise ActiveRecord::IrreversibleMigration
  end
end

最佳和高效的实现方式是什么?

英文:

I am writing a data_migration for which I am using data-migrate gem. I have a situation where I should skip one specific data_migration in Production environment alone. I can do it like this

class BackfillNonStatutoryResultsWithAssessmentPeriod < ActiveRecord::Migration[7.0]
  def up 
    unless RAILS_ENV == "production"
     # Do something
    end
  end
end

 def down
   raise ActiveRecord::IrreversibleMigration
  end
end

What is the best and efficient way to achieve this?

答案1

得分: 1

我认为你的代码看起来不错,但我会稍微改进一下

class BackfillNonStatutoryResultsWithAssessmentPeriod < ActiveRecord::Migration[7.0]
  def up
    return if Rails.env.production?
    # 做一些操作
  end

  def down
   raise ActiveRecord::IrreversibleMigration
  end
end
英文:

I think your code looks fine, but I would improve it a little bit

class BackfillNonStatutoryResultsWithAssessmentPeriod < ActiveRecord::Migration[7.0]
  def up
    return if Rails.env.production?
    # Do something
  end

  def down
   raise ActiveRecord::IrreversibleMigration
  end
end

答案2

得分: 0

从未使用过这个宝石,但README似乎明确指出,这个宝石的整个目的是使您能够拥有一个单独的rake任务,用于迁移数据,而不仅仅是模式:

对于不需要任何中间AR活动的任务,比如开发和测试,您仍然使用db:migrate。对于生产和QA,您将它们的脚本更改为db:migrate:with_data

如果您对此有问题,那么从您的问题中并不清楚。

英文:

Never used the gem before, but the README seems to be clear that this gem's whole purpose is to enable you to have a separate rake task for migrating with data vs just schema:

> For setting tasks that don't require any intermediate AR activity, like dev and test, you stick with db:migrate. For production and QA, you change their scripts to db:migrate:with_data.

If you're having an issue with that then it's not clear from your question.

huangapple
  • 本文由 发表于 2023年8月10日 22:05:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76876491.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定