I've structured the management command to primarily check the database
connection's readiness without interacting with URLs or the web server.
The goal is to ensure that the database is available before proceeding
with other operations.
Here's a simplified version of my management command:
{{{
"""
Django command to wait for the database to be available.
"""
import time
from psycopg2 import OperationalError as Psycopg2OpError
from django.db.utils import OperationalError
from django.core.management.base import BaseCommand
class Command(BaseCommand):
"""Django command to wait for database."""
def handle(self, *args, **options):
"""Entrypoint for command."""
self.stdout.write('Waiting for database...')
db_up = False
while db_up is False:
try:
self.check(databases=['default'])
db_up = True
except (Psycopg2OpError, OperationalError):
self.stdout.write('Database unavailable, waiting 1
second...')
time.sleep(1)
self.stdout.write(self.style.SUCCESS('Database available!'))
}}}
Despite my best efforts, when I execute python manage.py wait_for_db, it
seems to trigger some URLs or components that access the database. As a
result, I receive errors indicating that the app is attempting to access a
database table during the process of checking if the database connection
is ready.
Is there a specific reason why running a management command would lead to
URL-related interactions and database access? How can I modify my
management command to solely check the database connection's readiness
without causing these unintended side effects?
Any insights or guidance on this issue would be greatly appreciated. Thank
you in advance!
Environment:
Django version: 4.1
Python version: 3.10
Database: Postgres
--
Ticket URL: <https://code.djangoproject.com/ticket/34800>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* status: new => closed
* resolution: => invalid
Comment:
Hello,
Please seek support from the appropriate
[https://www.djangoproject.com/community/ support channels] to confirm
there is an issue with Django before filing an issue. Closing as invalid;
feel free to reopen with any references to support discussions + some more
specific details about the exact issue.
Thanks
--
Ticket URL: <https://code.djangoproject.com/ticket/34800#comment:1>