-
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.
configured docker-compose and github action wait for db
- Loading branch information
Showing
18 changed files
with
189 additions
and
9 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
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 |
---|---|---|
@@ -1,2 +1,22 @@ | ||
# food-recipy-api | ||
This is a food recipy Django api project Integrated with Docker , CI/CD | ||
|
||
|
||
## To Start the app with Docker-compose | ||
command` docker-compose up` | ||
<!-- code` docker-compose run --rm app sh -c "python manage.py collectstatic"` --> | ||
|
||
## To check the code using linting | ||
command` docker-compose run --rm app sh -c "flake8"` | ||
|
||
## To run the test | ||
command` docker-compose run --rm app sh -c "python manage.py test"` | ||
|
||
## To run our custom command | ||
command` docker-compose run --rm app sh -c "python manage.py wait_for_db"` | ||
|
||
## To start an project | ||
command` docker-compose run --rm app sh -c "django-admin startproject app ."` | ||
|
||
## To create a core app | ||
command` docker-compose run --rm app sh -c "python manage.py startapp core` |
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,14 @@ | ||
""" | ||
calculator functions | ||
""" | ||
|
||
|
||
def add(x, y): | ||
""" add x and y and return result """ | ||
return x + y | ||
|
||
|
||
def substract(x, y): | ||
""" subtracting x from y and return result""" | ||
return y - x |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
""" | ||
Sample tests | ||
""" | ||
|
||
|
||
from django.test import SimpleTestCase | ||
from app import calc | ||
|
||
|
||
class CalcTests(SimpleTestCase): | ||
"""Test the calc module""" | ||
def test_add_numbers(self): | ||
"""Test adding numbers togeter""" | ||
res = calc.add(5, 6) | ||
self.assertEqual(res, 11) | ||
|
||
def test_substract_number(self): | ||
""""Test substracting numbers""" | ||
res = calc.substract(10, 15) | ||
self.assertEqual(res, 5) |
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,4 @@ | ||
# the noqa tells flake8 to ignore that line for linting | ||
from django.contrib import admin # noqa | ||
|
||
# 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,30 @@ | ||
""" | ||
Django command to wait for the database to be available. | ||
""" | ||
|
||
# from django.core.management.base import BaseCommand | ||
|
||
import time | ||
|
||
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 .""" | ||
|
||
def handle(self, *args, **options): | ||
""" Django command to wait for database """ | ||
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 avaialble!')) |
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 # noqa | ||
|
||
# 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,34 @@ | ||
""" | ||
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, patched_check): | ||
""" Test waiting for database if database 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 database when getting OperationalError" | ||
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']) |
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 |
---|---|---|
@@ -1,14 +1,47 @@ | ||
# Docker compose version 3.9 : is saying use the version 3.9 syntax | ||
version: "3.9" | ||
|
||
|
||
services: | ||
# NAME OF THE SERVICES app and db | ||
app: | ||
build: | ||
context: . | ||
args: | ||
- DEV=true | ||
# THIS MAP PORT 8000 FROM OUR LOCAL MACHINE TO 8000 PORT IN DOCKER | ||
ports: | ||
- "8000:8000" | ||
# VOLUMES ARE WAY OF MAPPING OR LINKING DIRECTORIES FROM OUR LOCAL MACHINE TO DOCKER | ||
# WHY WE ADDING VOLUMES IS OTHER TO SYNC THE CODE ON OUR LOCAL MACHINE TO DOCKER IN REALTIME | ||
# SO WE DON'T HAVE TO BUILD EVERYTIME | ||
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: | ||
# we are using db because it is the host name of the database db | ||
- DB_HOST=db | ||
- DB_NAME=devdb | ||
- DB_USER=devuser | ||
- DB_PASS=changeme | ||
# depends_on will wait for the db service to start before moving on | ||
# it would automatically create a network between the db service and app | ||
depends_on: | ||
- db | ||
|
||
db: | ||
image: postgres:13-alpine | ||
# stores the database which is located in docker in our local machine | ||
volumes: | ||
- dev-db-data:/var/lib/postgresql/data | ||
|
||
environment: | ||
- POSTGRES_DB=devdb | ||
- POSTGRES_USER=devuser | ||
- POSTGRES_PASSWORD=changeme | ||
|
||
volumes: | ||
dev-db-data: |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
Django>=3.2.4,<3.3 | ||
djangorestframework>=3.12.4,<3.13 | ||
djangorestframework>=3.12.4,<3.13 | ||
psycopg2>=2.8.6,<2.9 |