Skip to content

Commit

Permalink
new commit
Browse files Browse the repository at this point in the history
  • Loading branch information
DominikDrabik committed Aug 22, 2024
1 parent 526f3e0 commit a76dcdd
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ jobs:
- name : Checkout code
uses: actions/checkout@v2
- name: Test
run: docker-compose run --rm app sh -c "python manage.py test"
run: docker-compose run --rm app sh -c "python manage.py wait_for_db && python manage.py test"
- name: Lint
run: docker-compose run --rm app sh -c "flake8"
4 changes: 1 addition & 3 deletions app/core/management/commands/wait_for_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,15 @@

class Command(BaseCommand):
"""Django command to wait for the database to be available"""

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(database=['default'])
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!'))
8 changes: 2 additions & 6 deletions app/core/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,13 @@ class CommandTests(SimpleTestCase):
def test_wait_for_db_ready(self, patched_check):
"""Test commands."""
patched_check.return_value = True

call_command('wait_for_db')

patched_check.assert_called_once_with(database=['default'])
patched_check.assert_called_once_with(databases=['default'])

@patch('time.sleep')
def test_wait_for_db_delay(self, patched_sleep, patched_check):
"""Test commands."""
patched_check.side_effect = [Psycopg2Error] * 2 + [OperationalError] * 3 + [True]

call_command('wait_for_db')

self.assertEqual(patched_check.call_count, 6)
patched_check.assert_called_with(database=['default'])
patched_check.assert_called_with(databases=['default'])
4 changes: 3 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ services:
volumes:
- ./app:/app
command: >
sh -c "python manage.py runserver 0.0.0.0:8000"
sh -c "python manage.py wait_for_db &&
python manage.py migrate &&
python manage.py runserver 0.0.0.0:8000"
environment:
- DB_HOST=db
- DB_NAME=devdb
Expand Down

0 comments on commit a76dcdd

Please sign in to comment.