Friday, January 6, 2023

Re: Data migrations ran multiple times during deploy

tl;dr it's not safe to run multiple manage.py migrate simultaneously

https://stackoverflow.com/a/69608486

On January 5, 2023 7:47:54 PM CST, Harris Lapiroff <harrislapiroff@gmail.com> wrote:
Hi, all! Curious about how folks do data migrations as we ran into an issue recently:

Our production deployment consists of three containers running the application. When each container comes up it runs a series of startup tasks include `./manage.py migrate`. Usually it's fine to run this whenever because it's idempotent and Postgres should reject simultaneous schema changes.

So I was surprised recently when we performed a deploy that included a data migration that created some objects and it created the objects multiple times. I'm assuming what happened is that the containers each kicked off a migration command simultaneously (thus all three containers saw that the migration hadn't been run yet) and Postgres didn't block any of the transactions as conflicting.

How do you all avoid this?

Thanks!
Harris

No comments:

Post a Comment