Hacker News new | ask | show | jobs
by zepolen 2688 days ago
No,

  with transaction.atomic():
    for user in db.users.all():
      user.do()
is not the same as

  for user in db.users.all():
    with transaction.atomic():
      user.do()
If the first fails, the entire data rolls back, if the second fails, half your db might be in an inconsistent state.
1 comments

Yes, of course, they are not the same. I did not claim they were. I said:

> You can often recast that migration to something more like

By which I mean, in my experience you can usually write your migrations so that your code can work with the old AND the new version of the data, in which case you don't need to have a transaction around the whole operation.

This takes more work but is safer:

https://martinfowler.com/bliki/ParallelChange.html

https://www.amazon.com/Refactoring-Databases-Evolutionary-pa...