<filename>app/core/management/commands/wait_for_db.py
"""
time - for sleeping
connections - to test if db connection is available
OpError - django will throw if connection is not available
BaseCommand - will extend this one to create custom command
"""

import time

from django.db import connections
from django.db.utils import OperationalError
from django.core.management.base import BaseCommand


class Command(BaseCommand):
    """Django command to pause execution until DB is available"""

    # handle func is run when we run this management command
    def handle(self, *args, **options):
        self.stdout.write('Waiting for database...')
        db_conn = None
        while not db_conn:
            try:
                db_conn = connections['default']
            except OperationalError:
                self.stdout.write('Database unavailable, waiting 1 second...')
                time.sleep(1)
        self.stdout.write(self.style.SUCCESS('Database available!'))

<filename>app/core/management/commands/wait_for_db.py
"""
time - for sleeping
connections - to test if db connection is available
OpError - django will throw if connection is not available
BaseCommand - will extend this one to create custom command
"""

import time

from django.db import connections
from django.db.utils import OperationalError
from django.core.management.base import BaseCommand


class Command(BaseCommand):
    """Django command to pause execution until DB is available"""

    # handle func is run when we run this management command
    def handle(self, *args, **options):
        self.stdout.write('Waiting for database...')
        db_conn = None
        while not db_conn:
            try:
                db_conn = connections['default']
            except OperationalError:
                self.stdout.write('Database unavailable, waiting 1 second...')
                time.sleep(1)
        self.stdout.write(self.style.SUCCESS('Database available!'))

time - for sleeping connections - to test if db connection is available OpError - django will throw if connection is not available BaseCommand - will extend this one to create custom command Django command to pause execution until DB is available # handle func is run when we run this management command