Skip to content

Commit

Permalink
Complete Setup done.
Browse files Browse the repository at this point in the history
  • Loading branch information
ShivamMenda committed Sep 12, 2023
1 parent bde8596 commit c4528d5
Show file tree
Hide file tree
Showing 15 changed files with 111 additions and 7 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ name: Checks
on: [push]

jobs:
test-lint:
name: Test and Lint
db-test:
name: Test
runs-on: ubuntu-20.04
steps:
- name: Login to docker hub
Expand All @@ -15,8 +15,8 @@ jobs:
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Checkout
uses: actions/checkout@v2
- name: Wait_for_DB
run: docker-compose run --rm app sh -c "python manage.py wait_for_db"
- name: Test
run: docker-compose run --rm app sh -c "python manage.py test"
- name: Lint
run: docker-compose run --rm app sh -c "flake8"

4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ ARG DEV=false

RUN python -m venv /py && \
/py/bin/pip install --upgrade pip && \
apk add --update --no-cache postgresql-client && \
apk add --update --no-cache --virtual .tmp-build-deps \
build-base postgresql-dev musl-dev && \
/py/bin/pip install -r /tmp/requirements.txt && \
if [ $DEV = "true" ]; \
then /py/bin/pip install -r /tmp/requirements.dev.txt ; \
fi && \
rm -rf /tmp && \
apk del .tmp-build-deps && \
adduser \
--disabled-password \
--no-create-home \
Expand Down
10 changes: 8 additions & 2 deletions app/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
https://docs.djangoproject.com/en/3.2/ref/settings/
"""

import os

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
Expand Down Expand Up @@ -37,6 +39,7 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'core',
]

MIDDLEWARE = [
Expand Down Expand Up @@ -75,8 +78,11 @@

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
'ENGINE': 'django.db.backends.postgresql',
'HOST': os.environ.get('DB_HOST'),
'NAME': os.environ.get('DB_NAME'),
'USER': os.environ.get('DB_USER'),
'PASSWORD': os.environ.get('DB_PASS'),
}
}

Expand Down
Empty file added app/core/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions app/core/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin # noqa

# Register your models here.
6 changes: 6 additions & 0 deletions app/core/apps.py
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 added app/core/management/__init__.py
Empty file.
Empty file.
27 changes: 27 additions & 0 deletions app/core/management/commands/wait_for_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""
Django cmd to wait for db
"""

from django.core.management.base import BaseCommand
import time
from psycopg2 import OperationalError as Psycopg2OpError
from django.db.utils import OperationalError


class Command(BaseCommand):
""" Wait for DB """

def handle(self, *args, **options):
"""Entrypoint for command"""

self.stdout.write("Waiting for db...")
db_up = False
while db_up is False:
try:
self.check(databases=['default']) # type: ignore
db_up = True
except(Psycopg2OpError, OperationalError):
self.stdout.write('db unavailable, waiting 1 second..')
time.sleep(1)
self.stdout.write(self.style.SUCCESS('db available!'))
Empty file added app/core/migrations/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions app/core/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models # noqa

# Create your models here.
Empty file added app/core/tests/__init__.py
Empty file.
33 changes: 33 additions & 0 deletions app/core/tests/test_commands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""
Test custom djano management command
"""

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, patched_check):
"""Test wait for db if db ready """

patched_check.return_value = True
call_command('wait_for_db')
patched_check.assert_called_once_with(databases=['default'])

@patch('time.sleep')
def test_wait_for_db_delay(self, patched_sleep, patched_check):
""" Test waiting for db """

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(databases=['default'])
23 changes: 22 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,26 @@ services:
volumes:
- ./app:/app
command: >
sh -c "python manage.py migrate && 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
- DB_USER=devuser
- DB_PASS=test
depends_on:
- db
db:
image: postgres:13-alpine
volumes:
- dev-db-data:/var/lib/postgresql/data
environment:
- POSTGRES_DB=devdb
- POSTGRES_USER=devuser
- POSTGRES_PASSWORD=test


volumes:
dev-db-data:

1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Django>=3.2.4,<3.3
djangorestframework>=3.12.4,<3.13
psycopg2>=2.8.6,<2.9

0 comments on commit c4528d5

Please sign in to comment.