Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Middleware to warn for unapplied migrations. #1767

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,38 @@ jobs:
coverage report -m
bash <(curl -s https://codecov.io/bash)

test_check_unapplied_migrations_middleware:
name: middleware for unapplied migrations
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.7]

steps:
- uses: actions/checkout@v2

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v1
with:
python-version: ${{ matrix.python-version }}

- name: Install Python dependencies
run: |
sudo apt-get install libkrb5-dev gettext
pip install -r requirements/devel.txt
pushd tcms/ && npm install && popd

- name: Run test
run: |
export LANG=en-us
export TEST_CHECK_UNAPPLIED_MIGRATIONS_MIDDLEWARE=1
coverage run --source='.' ./manage.py test -v2 --noinput --settings=tcms.settings.test tcms.core.tests.test_middleware.TestCheckUnappliedMigrationsMiddleware

- name: Send coverage to codecov.io
run: |
coverage report -m
bash <(curl -s https://codecov.io/bash)

without_internal_bugtracker:
name: without internal bugtracker
runs-on: ubuntu-latest
Expand Down
22 changes: 22 additions & 0 deletions tcms/core/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from django.utils.deprecation import MiddlewareMixin
from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _
from django.db import DEFAULT_DB_ALIAS, connections
from django.db.migrations.executor import MigrationExecutor


class CsrfDisableMiddleware(MiddlewareMixin):
Expand All @@ -32,3 +34,23 @@ def process_request(self, request):
}
)
)


class CheckUnappliedMigrationsMiddleware(MiddlewareMixin):
def process_request(self, request):
doc_url = """https://kiwitcms.readthedocs.io/en/latest/\
installing_docker.html#initial-configuration-of-running-container"""
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
plan = executor.migration_plan(executor.loader.graph.leaf_nodes())
if plan:
messages.add_message(
request,
messages.ERROR,
mark_safe(
_('You have %(unapplied_migration_count)s unapplied migration(s). '
'See <a href="%(doc_url)s">documentation</a>') % {
"unapplied_migration_count": len(plan),
"doc_url": doc_url,
}
)
)
26 changes: 26 additions & 0 deletions tcms/core/tests/test_middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import os
import unittest
from django import test
from django.conf import settings
from django.apps import apps
from django.contrib.messages import get_messages


@unittest.skipUnless(
os.getenv('TEST_CHECK_UNAPPLIED_MIGRATIONS_MIDDLEWARE'),
'CheckUnappliedMigrationsMiddleware testing not enabled')
class TestCheckUnappliedMigrationsMiddleware(test.TestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
settings.INSTALLED_APPS.append("tcms.core.tests.test_migrations_app")
apps.set_installed_apps(settings.INSTALLED_APPS)

@test.override_settings(MIGRATION_MODULES={
"migrations": "tcms.core.tests.test_migrations_app.migrations"})
def test_unapplied_migrations(self):
unapplied_migration_message = """You have 1 unapplied migration(s).\
See <a href="https://kiwitcms.readthedocs.io/en/latest/installing_docker.html\
#initial-configuration-of-running-container">documentation</a>"""
response = self.client.get('/', follow=True)
self.assertContains(response, unapplied_migration_message)
Empty file.
15 changes: 15 additions & 0 deletions tcms/core/tests/test_migrations_app/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from django.db import migrations, models


class Migration(migrations.Migration):

operations = [

migrations.CreateModel(
"TestPerson",
[
("id", models.AutoField(primary_key=True)),
("name", models.CharField(max_length=10)),
],
),
]
Empty file.
5 changes: 5 additions & 0 deletions tcms/core/tests/test_migrations_app/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.db import models


class TestPerson(models.Model):
name = models.CharField(max_length=10)
1 change: 1 addition & 0 deletions tcms/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
'global_login_required.GlobalLoginRequiredMiddleware',
'simple_history.middleware.HistoryRequestMiddleware',
'tcms.core.middleware.CheckSettingsMiddleware',
'tcms.core.middleware.CheckUnappliedMigrationsMiddleware',
]


Expand Down