import time

# used to test if the database connection is available
from django.db import connections
# operational error django will throw if database isn't available
from django.db.utils import OperationalError
# the class we need to build on in order to create the custom command
from django.core.management.base import BaseCommand


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

    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!"))

import time

# used to test if the database connection is available
from django.db import connections
# operational error django will throw if database isn't available
from django.db.utils import OperationalError
# the class we need to build on in order to create the custom command
from django.core.management.base import BaseCommand


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

    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!"))

# used to test if the database connection is available # operational error django will throw if database isn't available # the class we need to build on in order to create the custom command Django command to pause execution until database is available