Rails 迁移版本兼容性

发布于 2024-11-18 10:03:45 字数 1528 浏览 3 评论 0原文

这是场景

生产/暂存代码位于版本 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绮筵 2024-11-25 10:03:45

我会注释 X+5 迁移上的 update_report 调用,运行它,然后在完成后在 Rails 控制台上运行代码片段。

或者更改迁移以执行直接 SQL 查询:

execute "update report_tables set check_number = x.check_number ...."

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:

execute "update report_tables set check_number = x.check_number ...."
删除→记忆 2024-11-25 10:03:45

如果您无法避免迁移中的模型,请将 X+5 迁移更改为:

Order.all.each do |o|
  ReportTable.where(:order_id => o.id).first.update_attributes(:check_number => o.check_number)
end

这将继续引用当时应该仍然存在的订单的支票号码属性。

If you can't avoid the models in the migration then, change the X+5 migration to:

Order.all.each do |o|
  ReportTable.where(:order_id => o.id).first.update_attributes(:check_number => o.check_number)
end

This will continue to reference the order's check number attribute which should still be there at that time.

花开雨落又逢春i 2024-11-25 10:03:45

我最终将此代码从 X+5 中的迁移

Order.all.map{|x| x.update_report }

移至 X+10 中的迁移

# migration
add_column :payment_transactions, :check_number, :integer
Order.all.map{|x| x.update_report }

I ended up moving the this code form migration in X+5

Order.all.map{|x| x.update_report }

to migration in X+10

# migration
add_column :payment_transactions, :check_number, :integer
Order.all.map{|x| x.update_report }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文