diff options
author | Alan Rominger <arominge@redhat.com> | 2022-10-07 21:50:54 +0200 |
---|---|---|
committer | Dave <dmz.oneill@gmail.com> | 2024-02-21 14:43:54 +0100 |
commit | 700de14c760cb18ea4bff01af3763354ec17532d (patch) | |
tree | 7f89c063fdec54961373e2a2348a18d0f43e736d | |
parent | Deleted duplicate graphics that were converted to drawio. (diff) | |
download | awx-700de14c760cb18ea4bff01af3763354ec17532d.tar.xz awx-700de14c760cb18ea4bff01af3763354ec17532d.zip |
Make the migration middleware faster, second attempt
-rw-r--r-- | awx/main/middleware.py | 22 |
1 files 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")) |