gpt4 book ai didi

mysql - 当部分迁移失败时,为什么不是 "undo"?

转载 作者:行者123 更新时间:2023-11-30 23:13:03 24 4
gpt4 key购买 nike

假设我有这个迁移:

class MigrateStuff < ActiveRecord::Migration
def up
add_column :contacts, :receive_newsletter, :boolean, :default => false
add_index :contacts, :receive_newsletter

for t in SomeOtherThing.all
#... do stuff in here
end
end

def down
#...
end
end

所以我添加了一个列和索引。然后我将一些数据处理到新列中。如果我的 for 循环中的某些部分失败了会怎样?列/索引未删除。我尝试将其添加到交易中:

class MoveEmailRecipientsToContacts < ActiveRecord::Migration
def up
Volunteer.transaction do
add_column :contacts, :receive_newsletter, :boolean, :default => false
add_index :contacts, :receive_newsletter

for t in SomeOtherThing.all
#... do stuff in here
end
end
end

def down
#...
end
end

那么for block 出现了异常,这不是导致事务回滚吗?但是列和索引仍然存在!

处理这个问题的正确方法是什么?

最佳答案

另一种方法是通过引发 ActiveRecord::Rollback 异常手动回滚。

raise ActiveRecord::Rollback

当你想回滚时。

关于mysql - 当部分迁移失败时,为什么不是 "undo"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19106624/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com