summaryrefslogtreecommitdiff
path: root/pedidos-ya/app/core/management/commands/wait_for_db.py
diff options
context:
space:
mode:
Diffstat (limited to 'pedidos-ya/app/core/management/commands/wait_for_db.py')
-rw-r--r--pedidos-ya/app/core/management/commands/wait_for_db.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/pedidos-ya/app/core/management/commands/wait_for_db.py b/pedidos-ya/app/core/management/commands/wait_for_db.py
new file mode 100644
index 0000000..b6bdcde
--- /dev/null
+++ b/pedidos-ya/app/core/management/commands/wait_for_db.py
@@ -0,0 +1,21 @@
+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"""
+
+ 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!'))