Here is another python solution, using a custom Django command :
from io import StringIO
from time import sleep
from django.core.management import BaseCommand, call_command
class Command(BaseCommand):
def handle(self, *, **__):
pending_migrations = True
while pending_migrations:
output = StringIO()
call_command("migrate", plan=True, stdout=output)
command_result = output.getvalue()
pending_migrations = not "No planned migration operations" in command_result
if pending_migrations:
sleep(5)
It will retry every 5 seconds (I need it in my case but it's no mandatory to do so).
To use it, simply run python manage.py wait_for_migrations
, assuming the code above is in a file wait_for_migrations.py
, in a Django app, under <app>/management/commands
(whole path: <app>/management/commands/wait_for_migrations.py
.
To learn more about custom commands : LINK
I use this custom command in the startup sequence of my celery docker containers to be sure the database has been migrated by the Django backend container, before starting celery itself.