diff options
| author | Matjaz Gregoric <mtyaka@gmail.com> | 2021-08-12 09:35:46 +0200 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2021-08-20 11:05:21 +0200 |
| commit | 518ce7a51f994fc0585d31c4553e2072bf816f76 (patch) | |
| tree | d55ebb767850ce4a22c37bbaf3574eebc5888f74 | |
| parent | e9aa20e4e17aee7adcf52fcd3f445cd296fa9b7b (diff) | |
Fixed #33017 -- Fixed storage engine introspection on MySQL.
This also improves performance on MySQL instances with a large number
of databases, since querying the information_schema table can be very
slow
| -rw-r--r-- | django/db/backends/mysql/introspection.py | 11 | ||||
| -rw-r--r-- | tests/backends/mysql/test_introspection.py | 32 |
2 files changed, 38 insertions, 5 deletions
diff --git a/django/db/backends/mysql/introspection.py b/django/db/backends/mysql/introspection.py index cd3e13eebe..2383c9ca1b 100644 --- a/django/db/backends/mysql/introspection.py +++ b/django/db/backends/mysql/introspection.py @@ -180,10 +180,13 @@ class DatabaseIntrospection(BaseDatabaseIntrospection): Retrieve the storage engine for a given table. Return the default storage engine if the table doesn't exist. """ - cursor.execute( - "SELECT engine " - "FROM information_schema.tables " - "WHERE table_name = %s", [table_name]) + 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 diff --git a/tests/backends/mysql/test_introspection.py b/tests/backends/mysql/test_introspection.py index 37fbcb1513..4f13622eda 100644 --- a/tests/backends/mysql/test_introspection.py +++ b/tests/backends/mysql/test_introspection.py @@ -1,6 +1,6 @@ from unittest import skipUnless -from django.db import connection +from django.db import connection, connections from django.test import TestCase @@ -27,3 +27,33 @@ class ParsingTests(TestCase): with self.subTest(check_clause): check_columns = _parse_constraint_columns(check_clause, table_columns) self.assertEqual(list(check_columns), expected_columns) + + +@skipUnless(connection.vendor == 'mysql', 'MySQL tests') +class StorageEngineTests(TestCase): + databases = {'default', 'other'} + + def test_get_storage_engine(self): + table_name = 'test_storage_engine' + create_sql = 'CREATE TABLE %s (id INTEGER) ENGINE = %%s' % table_name + drop_sql = 'DROP TABLE %s' % table_name + default_connection = connections['default'] + other_connection = connections['other'] + try: + with default_connection.cursor() as cursor: + cursor.execute(create_sql % 'InnoDB') + self.assertEqual( + default_connection.introspection.get_storage_engine(cursor, table_name), + 'InnoDB', + ) + with other_connection.cursor() as cursor: + cursor.execute(create_sql % 'MyISAM') + self.assertEqual( + other_connection.introspection.get_storage_engine(cursor, table_name), + 'MyISAM', + ) + finally: + with default_connection.cursor() as cursor: + cursor.execute(drop_sql) + with other_connection.cursor() as cursor: + cursor.execute(drop_sql) |
