Skip to content

Commit

Permalink
fix: monkey-patch django db introspection to avoid performance issues
Browse files Browse the repository at this point in the history
(cherry picked from commit d393d6e AND
fixed linting issues)
(cherry picked from commit 216a206)
  • Loading branch information
mtyaka authored and xitij2000 committed Jun 23, 2023
1 parent 2817dd1 commit 4d801fe
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
26 changes: 26 additions & 0 deletions cms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
isort:skip_file
"""

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

# We monkey patch Kombu's entrypoints listing because scanning through this
# accounts for the majority of LMS/Studio startup time for tests, and we don't
Expand All @@ -22,3 +28,23 @@
# that shared_task will use this app, and also ensures that the celery
# singleton is always configured for the CMS.
from .celery import APP as CELERY_APP # lint-amnesty, pylint: disable=wrong-import-position


def get_storage_engine(self, cursor, table_name):
"""
This is a patched version of `get_storage_engine` that fixes a
performance issue with migrations. For more info see FAL-2248 and
https://github.com/django/django/pull/14766
"""
cursor.execute("""
SELECT engine
FROM information_schema.tables
WHERE table_name = %s
AND table_schema = DATABASE()""", [table_name])
result = cursor.fetchone()
if not result:
return self.connection.features._mysql_storage_engine # pylint: disable=protected-access
return result[0]


django.db.backends.mysql.introspection.DatabaseIntrospection.get_storage_engine = get_storage_engine
27 changes: 27 additions & 0 deletions lms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,30 @@
# that shared_task will use this app, and also ensures that the celery
# singleton is always configured for the LMS.
from .celery import APP as CELERY_APP # lint-amnesty, pylint: disable=wrong-import-position

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


def get_storage_engine(self, cursor, table_name):
"""
This is a patched version of `get_storage_engine` that fixes a
performance issue with migrations. For more info see FAL-2248 and
https://github.com/django/django/pull/14766
"""
cursor.execute("""
SELECT engine
FROM information_schema.tables
WHERE table_name = %s
AND table_schema = DATABASE()""", [table_name])
result = cursor.fetchone()
if not result:
return self.connection.features._mysql_storage_engine # pylint: disable=protected-access
return result[0]


django.db.backends.mysql.introspection.DatabaseIntrospection.get_storage_engine = get_storage_engine

0 comments on commit 4d801fe

Please sign in to comment.