Most of our stuff is deployed under Docker Swarm - though it would work 
with compose or other systems -  and typically we have a separate service, 
using our standard container image for the app but with the command 
overridden to run 'manage.py migrate'. The swarm is told to run just one 
replica of this, and not to restart it.

Whenever we deploy a new version of the image, therefore, this gets run 
just once.

It does mean that the app can start up and run before the migration, but if 
it fails as a result it will be restarted after a short delay. In practice 
this has never been an issue for us.

Quentin
On Saturday, 7 January 2023 at 00:40:29 UTC Ryan Nowakowski wrote:

> 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 <harrisl...@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
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/b1da7431-3ddb-4dd5-8d2e-714b24e20a38n%40googlegroups.com.

Reply via email to