Skip to content

Commit

Permalink
Add migrations checks, separate migration run script
Browse files Browse the repository at this point in the history
  • Loading branch information
mkleszcz committed Sep 18, 2019
1 parent 87e26e6 commit 84e9987
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 7 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ python:
services:
- docker
script:
- docker build -t backend .
- BACKEND_IMAGE=backend docker-compose -f docker-compose-ci.yml run backend /app/scripts/dev/run_tests.sh
2 changes: 1 addition & 1 deletion docker-compose-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ services:
db:
image: postgres:9.6.8
ports:
- "5432:5432"
- "5432"
env_file: test.env

localstack:
Expand Down
4 changes: 2 additions & 2 deletions restauth/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/

SECRET_KEY = os.environ.get('SECRET_KEY')
SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY')
DEBUG = os.environ.get('DEBUG')
ALLOWED_HOSTS = list(filter(lambda s: len(s) > 0, map(str.strip, os.environ.get('ALLOWED_HOSTS', '').split(','))))
ALLOWED_HOSTS = list(filter(lambda s: len(s) > 0, map(str.strip, os.environ.get('ALLOWED_HOSTS', '*').split(','))))


# Application definition
Expand Down
5 changes: 4 additions & 1 deletion restauth/urls.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,8 @@
url(r'^doc/', get_swagger_view(title='Documentation')),

path('auth/', include(user_patterns)),
path('password-reset/', include(password_reset_patterns))
path('password-reset/', include(password_reset_patterns)),

path('', views.HomeView.as_view(), name='home'),
path('status/elb', views.HealthCheckView.as_view(), name='elb_status'),
]
29 changes: 28 additions & 1 deletion restauth/views.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from rest_framework import generics
from rest_framework import permissions
from rest_framework import views
from rest_framework import response
from rest_framework import status
from rest_framework.throttling import AnonRateThrottle
from django.db import DEFAULT_DB_ALIAS, connections
from django.db.migrations.executor import MigrationExecutor

from . import serializers

Expand Down Expand Up @@ -51,4 +56,26 @@ class PasswordResetConfirmationView(generics.CreateAPIView):
Set new password, it requires to provide the new password to set.
"""
permission_classes = (permissions.AllowAny,)
serializer_class = serializers.PasswordResetConfirmationSerializer
serializer_class = serializers.PasswordResetConfirmationSerializer


class HomeView(views.APIView):
permission_classes = []

def get(self, request):
return response.Response(status=status.HTTP_200_OK)


class HealthCheckView(views.APIView):
authentication_classes = ()
permission_classes = (permissions.AllowAny,)

@staticmethod
def get(request):
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
plan = executor.migration_plan(executor.loader.graph.leaf_nodes())

if plan:
return response.Response(status=status.HTTP_503_SERVICE_UNAVAILABLE)

return response.Response(status=status.HTTP_200_OK)
2 changes: 1 addition & 1 deletion scripts/dev/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ echo "Waiting for DB"
python ./scripts/dev/wait_for_postgres.py

echo "Run migrations"
/app/manage.py makemigrations --dry-run --check || { echo "ERROR: there were changes in the models, but migration listed above have not been created and are not saved in version control"; exit 1; }
python /app/manage.py makemigrations --dry-run --check || { echo "ERROR: there were changes in the models, but migration listed above have not been created and are not saved in version control"; exit 1; }

echo "Run pytest"
pytest --maxfail=1 --junitxml=/test-results/report.xml
1 change: 0 additions & 1 deletion scripts/run-backend.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@

set -e

python manage.py migrate --no-input
uwsgi --ini uwsgi.ini
5 changes: 5 additions & 0 deletions scripts/run-migrations.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

set -e

python manage.py migrate --no-input
4 changes: 4 additions & 0 deletions test.env
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
DEBUG=on

DJANGO_SECRET_KEY=secret-key

ALLOWED_HOSTS=*

POSTGRES_PASSWORD=test-app-secret
POSTGRES_DB=test-app
POSTGRES_USER=test-user
Expand Down

0 comments on commit 84e9987

Please sign in to comment.