-
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.
Update docker-compose and checks to wait for db to start up
- Loading branch information
1 parent
3a83349
commit 255e618
Showing
13 changed files
with
79 additions
and
4 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
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
Empty file.
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.contrib import admin | ||
|
||
# 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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class CoreConfig(AppConfig): | ||
default_auto_field = 'django.db.models.BigAutoField' | ||
name = 'core' |
Empty file.
Empty file.
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
"""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 for database.""" | ||
|
||
def handle(self, *args, **options): | ||
"""Entrypoint for command.""" | ||
self.stdout.write("Waiting for database...") | ||
db_up = False | ||
while not db_up: | ||
try: | ||
self.check(databases=['default']) | ||
db_up = True | ||
except (Psycopg2Error, OperationalError): | ||
self.stdout.write("Database unavailable, waiting 1s...") | ||
time.sleep(1) | ||
|
||
self.stdout.write(self.style.SUCCESS("Database available!")) |
Empty file.
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.db import models | ||
|
||
# Create your models here. |
Empty file.
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
"""Test custom Django management commands.""" | ||
from unittest.mock import patch | ||
|
||
from psycopg2 import OperationalError as Psycopg2Error | ||
from django.core.management import call_command | ||
from django.db.utils import OperationalError | ||
from django.test import SimpleTestCase | ||
|
||
|
||
@patch("core.management.commands.wait_for_db.Command.check") | ||
class CommandTests(SimpleTestCase): | ||
"""Test commands.""" | ||
|
||
def test_wait_for_db_ready(self, check): | ||
"""Test waiting for database if database ready.""" | ||
check.return_value = True | ||
|
||
call_command("wait_for_db") | ||
|
||
check.assert_called_once_with(databases=['default']) | ||
|
||
def test_wait_for_db_delay(self, check): | ||
"""Test waiting for database when getting OperationalError.""" | ||
check.side_effect = ( | ||
[Psycopg2Error] * 2 + [OperationalError] * 3 + [True] | ||
) | ||
|
||
with patch("time.sleep"): | ||
call_command("wait_for_db") | ||
|
||
self.assertEquals(check.call_count, 6) | ||
check.assert_called_with(databases=['default']) |
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