英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论