Skip to content

Commit

Permalink
Configured Docker compose and wait for db
Browse files Browse the repository at this point in the history
  • Loading branch information
rotem123456 committed Jul 3, 2024
1 parent 1766c32 commit 0d20d82
Show file tree
Hide file tree
Showing 6 changed files with 28 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 @@
- name: Checkout
uses: actions/checkout@v2
- name: Test
run: docker-compose run --rm app sh -c "python3 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"
2 changes: 1 addition & 1 deletion app/core/admin.py
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.
26 changes: 20 additions & 6 deletions app/core/management/commands/wait_for_db.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
"""
Django command for the database to be available.
Django command to wait for the database to be available.
"""
import time

from typing import Any
from django.core.management import BaseCommand
from psycopg2 import OperationalError as Psycopg2OpError

from django.db.utils import OperationalError
from django.core.management.base import BaseCommand


class Command(BaseCommand):
"""Django command to wait for database"""
"""Django command to wait for database."""

def handle(self, *args: Any, **options: Any):
pass
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(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!'))

2 changes: 1 addition & 1 deletion app/core/models.py
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.
3 changes: 2 additions & 1 deletion app/core/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ def test_wait_for_db_delay(self, patched_sleep, patched_check):
call_command('wait_for_db')

self.assertEqual(patched_check.call_count, 6)
patched_check.assert_called_with(databases=['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 @@ -11,7 +11,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 0d20d82

Please sign in to comment.