Make the migration middleware faster, second attempt

This commit is contained in:
Alan Rominger 2022-10-07 15:50:54 -04:00 committed by Dave
parent 8605e339df
commit 700de14c76

View File

@ -9,7 +9,7 @@ import urllib.parse
from django.conf import settings
from django.contrib.auth import logout
from django.contrib.auth.models import User
from django.db.migrations.executor import MigrationExecutor
from django.db.migrations.recorder import MigrationRecorder
from django.db import connection
from django.shortcuts import redirect
from django.apps import apps
@ -17,9 +17,11 @@ from django.utils.deprecation import MiddlewareMixin
from django.utils.translation import gettext_lazy as _
from django.urls import reverse, resolve
from awx.main import migrations
from awx.main.utils.named_url_graph import generate_graph, GraphNode
from awx.conf import fields, register
from awx.main.utils.profiling import AWXProfiler
from awx.main.utils.common import memoize
logger = logging.getLogger('awx.main.middleware')
@ -198,9 +200,21 @@ class URLModificationMiddleware(MiddlewareMixin):
request.path_info = new_path
@memoize(ttl=20)
def is_migrating():
last_applied = MigrationRecorder(connection).migration_qs.order_by('-applied').only('name').first().name
last_number = int(last_applied.split('_', 1)[0])
for migration_name in dir(migrations):
try:
migration_number = int(migration_name.split('_', 1)[0])
except ValueError:
continue
if migration_number > last_number:
return True
return False
class MigrationRanCheckMiddleware(MiddlewareMixin):
def process_request(self, request):
executor = MigrationExecutor(connection)
plan = executor.migration_plan(executor.loader.graph.leaf_nodes())
if bool(plan) and getattr(resolve(request.path), 'url_name', '') != 'migrations_notran':
if is_migrating() and getattr(resolve(request.path), 'url_name', '') != 'migrations_notran':
return redirect(reverse("ui:migrations_notran"))