Skip to content

Commit

Permalink
Refs kiwitcms#1696. Add middleware to warn for unapplied migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
brymut committed Jun 22, 2020
1 parent 7928c84 commit 36cc5a0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
37 changes: 37 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.loader import MigrationLoader


class CsrfDisableMiddleware(MiddlewareMixin):
Expand All @@ -32,3 +34,38 @@ 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')
loader = MigrationLoader(connections[DEFAULT_DB_ALIAS])
graph = loader.graph
app_names = sorted(loader.migrated_apps)
unapplied_migration_count = 0
for app_name in app_names:
visited = set()
for node in graph.leaf_nodes(app_name):
for plan_node in graph.forwards_plan(node):
if plan_node not in visited and plan_node[0] == app_name:
title = plan_node[1]
if graph.nodes[plan_node].replaces:
title += " (%s squashed migrations)" % len(
graph.nodes[plan_node].replaces)
applied_migration = loader.applied_migrations.get(plan_node)
if not applied_migration:
unapplied_migration_count += 1
visited.add(plan_node)
if unapplied_migration_count > 0:
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': unapplied_migration_count,
'doc_url': doc_url,
}
)
)
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

0 comments on commit 36cc5a0

Please sign in to comment.