【发布时间】:2022-03-30 23:09:16
【问题描述】:
我在 Rails 应用程序中创建了迁移:
class UpdateDefaultValues < ActiveRecord::Migration[5.2]
def up
OrderType.where(:category =>"reclamation").update_all(:prevent_delivery => false)
InvoiceType.where(:category =>"reclamation").update_all(:send_automatically => true)
end
def down
OrderType.where(:category =>"reclamation").update_all(:prevent_delivery => true)
InvoiceType.where(:category =>"reclamation").update_all(:send_automatically => false)
end
end
值得注意的是,此迁移不会更改数据库架构;它只执行数据更改。
我想知道:rails 是否自动在 SQL 事务中执行 up 和 down 方法,还是我需要将这两个更新包装在自己的事务中,以确保迁移是全部更新还是什么都不更新?
【问题讨论】:
-
来自谷歌的第一个链接:stackoverflow.com/questions/37820740/…
-
@user2541867,我的示例中的迁移不会更改架构,因此例如 MySql 此类迁移可以用事务包装。
标签: ruby-on-rails