Skip to content

Commit 4d801fe

Browse files
mtyakaxitij2000
authored andcommitted
fix: monkey-patch django db introspection to avoid performance issues
(cherry picked from commit d393d6e AND fixed linting issues) (cherry picked from commit 216a206)
1 parent 2817dd1 commit 4d801fe

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

cms/__init__.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
isort:skip_file
77
"""
88

9+
# FAL-2248: Monkey patch django's get_storage_engine to work around long migrations times.
10+
# This fixes a performance issue with database migrations in Ocim. We will need to keep
11+
# this patch in our opencraft-release/* branches until edx-platform upgrades to Django 4.*
12+
# which will include this commit:
13+
# https://github.com/django/django/commit/518ce7a51f994fc0585d31c4553e2072bf816f76
14+
import django.db.backends.mysql.introspection
915

1016
# We monkey patch Kombu's entrypoints listing because scanning through this
1117
# accounts for the majority of LMS/Studio startup time for tests, and we don't
@@ -22,3 +28,23 @@
2228
# that shared_task will use this app, and also ensures that the celery
2329
# singleton is always configured for the CMS.
2430
from .celery import APP as CELERY_APP # lint-amnesty, pylint: disable=wrong-import-position
31+
32+
33+
def get_storage_engine(self, cursor, table_name):
34+
"""
35+
This is a patched version of `get_storage_engine` that fixes a
36+
performance issue with migrations. For more info see FAL-2248 and
37+
https://github.com/django/django/pull/14766
38+
"""
39+
cursor.execute("""
40+
SELECT engine
41+
FROM information_schema.tables
42+
WHERE table_name = %s
43+
AND table_schema = DATABASE()""", [table_name])
44+
result = cursor.fetchone()
45+
if not result:
46+
return self.connection.features._mysql_storage_engine # pylint: disable=protected-access
47+
return result[0]
48+
49+
50+
django.db.backends.mysql.introspection.DatabaseIntrospection.get_storage_engine = get_storage_engine

lms/__init__.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,30 @@
1818
# that shared_task will use this app, and also ensures that the celery
1919
# singleton is always configured for the LMS.
2020
from .celery import APP as CELERY_APP # lint-amnesty, pylint: disable=wrong-import-position
21+
22+
# FAL-2248: Monkey patch django's get_storage_engine to work around long migrations times.
23+
# This fixes a performance issue with database migrations in Ocim. We will need to keep
24+
# this patch in our opencraft-release/* branches until edx-platform upgrades to Django 4.*
25+
# which will include this commit:
26+
# https://github.com/django/django/commit/518ce7a51f994fc0585d31c4553e2072bf816f76
27+
import django.db.backends.mysql.introspection
28+
29+
30+
def get_storage_engine(self, cursor, table_name):
31+
"""
32+
This is a patched version of `get_storage_engine` that fixes a
33+
performance issue with migrations. For more info see FAL-2248 and
34+
https://github.com/django/django/pull/14766
35+
"""
36+
cursor.execute("""
37+
SELECT engine
38+
FROM information_schema.tables
39+
WHERE table_name = %s
40+
AND table_schema = DATABASE()""", [table_name])
41+
result = cursor.fetchone()
42+
if not result:
43+
return self.connection.features._mysql_storage_engine # pylint: disable=protected-access
44+
return result[0]
45+
46+
47+
django.db.backends.mysql.introspection.DatabaseIntrospection.get_storage_engine = get_storage_engine

0 commit comments

Comments
 (0)