Rails 迁移版本兼容性
这是场景
生产/暂存代码位于版本 X
代码版本 X 代码
# order model
class Order < ActiveRecord::Base
has_many :payment_transactions
# has column for check_number
def update_report
ReportTable.where(:order_id => id).first.update_attributes(:check_number => check_number)
end
end
# payment_transaction model
class PaymentTransaction < ActiveRecord::Base
end
版本 X + 5 代码
# migration
Order.all.map{|x| x.update_report }
版本 X + 10 代码(当前)
# migration
add_column :payment_transactions, :check_number, :integer
# order model
class Order < ActiveRecord::Base
has_many :payment_transactions
# moved the column check_number to payment_transactions
def check_number
self.payment_transactions.where(:method => 'check').blank? ? nil : self.payment_transactions.where(:method => 'check').first.check_number
end
def update_report
ReportTable.where(:order_id => id).first.update_attributes(:check_number => check_number)
end
end
# payment_transaction model
class PaymentTransaction < ActiveRecord::Base
# has column for check_number
end
现在,当我将暂存环境上的代码更新到最新版本(X+10)并且运行迁移,X+5 上的迁移失败,因为它尝试运行此迁移
def check_number
self.payment_transactions.where(:method => 'check').blank? ? nil : self.payment_transactions.where(:method => 'check').first.check_number
end
,并且 payment_transaction 在 X+10 迁移之前不会获取 check_number 字段。
处理这个问题的最佳方法是什么?
Here is the scenario
production/staging code is on version X
Version X of code
# order model
class Order < ActiveRecord::Base
has_many :payment_transactions
# has column for check_number
def update_report
ReportTable.where(:order_id => id).first.update_attributes(:check_number => check_number)
end
end
# payment_transaction model
class PaymentTransaction < ActiveRecord::Base
end
Version X + 5 of code
# migration
Order.all.map{|x| x.update_report }
Version X + 10 of code (current)
# migration
add_column :payment_transactions, :check_number, :integer
# order model
class Order < ActiveRecord::Base
has_many :payment_transactions
# moved the column check_number to payment_transactions
def check_number
self.payment_transactions.where(:method => 'check').blank? ? nil : self.payment_transactions.where(:method => 'check').first.check_number
end
def update_report
ReportTable.where(:order_id => id).first.update_attributes(:check_number => check_number)
end
end
# payment_transaction model
class PaymentTransaction < ActiveRecord::Base
# has column for check_number
end
Now when i update the code on staging environment to the latest version (X+10) and run migration, the migration on X+5 fails because it tries to run this
def check_number
self.payment_transactions.where(:method => 'check').blank? ? nil : self.payment_transactions.where(:method => 'check').first.check_number
end
and payment_transaction will not get check_number field until X+10 migration.
Whats the best way to handle this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会注释 X+5 迁移上的
update_report
调用,运行它,然后在完成后在 Rails 控制台上运行代码片段。或者更改迁移以执行直接 SQL 查询:
I would comment the
update_report
call on the X+5 migration, run it, then run the snippet on rails console after I'm done.Or change the migration to perform a direct SQL query:
如果您无法避免迁移中的模型,请将 X+5 迁移更改为:
这将继续引用当时应该仍然存在的订单的支票号码属性。
If you can't avoid the models in the migration then, change the X+5 migration to:
This will continue to reference the order's check number attribute which should still be there at that time.
我最终将此代码从 X+5 中的迁移
移至 X+10 中的迁移
I ended up moving the this code form migration in X+5
to migration in X+10