-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented wait_for_db command and tests for it.
- Loading branch information
1 parent
900cc54
commit 3497a99
Showing
4 changed files
with
21 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
from django.contrib import admin | ||
from django.contrib import admin # noqa | ||
|
||
# Register your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,27 @@ | ||
""" | ||
Django command to wait for the database to be available. | ||
""" | ||
import time | ||
|
||
from psycopg2 import OperationalError as Psycopg2Error | ||
|
||
from django.db.utils import OperationalError | ||
from django.core.management.base import BaseCommand | ||
|
||
|
||
class Command(BaseCommand): | ||
"""Django command to wait the database.""" | ||
|
||
def handle(self, *args, **options): | ||
pass | ||
"""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 (Psycopg2Error, OperationalError): | ||
self.stdout.write('Database unavailable, waiting 1 second...') | ||
time.sleep(1) | ||
|
||
self.stdout.write(self.style.SUCCESS('Database available!')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
from django.db import models | ||
from django.db import models # noqa | ||
|
||
# Create your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters