From 700de14c760cb18ea4bff01af3763354ec17532d Mon Sep 17 00:00:00 2001 From: Alan Rominger Date: Fri, 7 Oct 2022 15:50:54 -0400 Subject: [PATCH] Make the migration middleware faster, second attempt --- awx/main/middleware.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/awx/main/middleware.py b/awx/main/middleware.py index 7299e2589e..160b9e77d3 100644 --- a/awx/main/middleware.py +++ b/awx/main/middleware.py @@ -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"))