diff --git a/core/management/commands/wait_for_db.py b/core/management/commands/wait_for_db.py new file mode 100644 index 0000000..0029bdb --- /dev/null +++ b/core/management/commands/wait_for_db.py @@ -0,0 +1,31 @@ +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 database is available""" + + # Critical: Disable system checks to prevent the command from crashing + # if the database is not yet available. + requires_system_checks = [] + + def handle(self, *args, **options): + self.stdout.write('Waiting for database...') + db_conn = None + for i in range(30): # Retry for 30 seconds + try: + db_conn = connections['default'] + # Try to actually connect + db_conn.cursor() + self.stdout.write(self.style.SUCCESS('Database available!')) + return + except OperationalError: + self.stdout.write('Database unavailable, waiting 1 second...') + time.sleep(1) + except Exception as e: + self.stdout.write(self.style.WARNING(f'Database error: {e}, waiting 1 second...')) + time.sleep(1) + + self.stdout.write(self.style.ERROR('Database unavailable after 30 seconds.')) \ No newline at end of file diff --git a/entrypoint.sh b/entrypoint.sh old mode 100644 new mode 100755 index b1b9940..bc83c86 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -3,6 +3,19 @@ # Exit immediately if a command exits with a non-zero status set -e +echo "Starting deployment script..." +echo "Environment Verification:" +echo "DB_ENGINE: ${DB_ENGINE:-'Not Set (will default to settings.py)'}" +echo "DB_HOST: ${DB_HOST:-'Not Set'}" +echo "DB_PORT: ${DB_PORT:-'Not Set'}" +echo "DB_NAME: ${DB_NAME:-'Not Set'}" +echo "DB_USER: ${DB_USER:-'Not Set'}" +# Do NOT print DB_PASS + +# Wait for database to be ready +echo "Checking database connection..." +python manage.py wait_for_db + # Apply database migrations echo "Applying database migrations..." python manage.py migrate