From a76dcddbc7e3186fad40d884925ef826ee2a9807 Mon Sep 17 00:00:00 2001 From: DominikDrabik Date: Thu, 22 Aug 2024 12:58:01 +0200 Subject: [PATCH] new commit --- .github/workflows/checks.yml | 2 +- app/core/management/commands/wait_for_db.py | 4 +--- app/core/tests/test_commands.py | 8 ++------ docker-compose.yml | 4 +++- 4 files changed, 7 insertions(+), 11 deletions(-) diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index 459514b..66ac24b 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -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" diff --git a/app/core/management/commands/wait_for_db.py b/app/core/management/commands/wait_for_db.py index 5ca91b9..af787ea 100644 --- a/app/core/management/commands/wait_for_db.py +++ b/app/core/management/commands/wait_for_db.py @@ -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!')) \ No newline at end of file diff --git a/app/core/tests/test_commands.py b/app/core/tests/test_commands.py index 1ef1d49..738bb27 100644 --- a/app/core/tests/test_commands.py +++ b/app/core/tests/test_commands.py @@ -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']) \ No newline at end of file + patched_check.assert_called_with(databases=['default']) \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 8eb7327..3660989 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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