summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMariusz Felisiak <felisiak.mariusz@gmail.com>2021-01-11 11:45:20 +0100
committerGitHub <noreply@github.com>2021-01-11 11:45:20 +0100
commit407d3cf39cd6a62f7277e401d646a4c7e8446879 (patch)
treea668de300f0527649dfcc35d8ffd2d9b04525713
parentcd3019bc106eed27b2f97776e4dd9ec7cbac29b2 (diff)
Fixed #32342 -- Added index order introspection on MySQL 8.0.1+.
-rw-r--r--django/db/backends/mysql/features.py8
-rw-r--r--django/db/backends/mysql/introspection.py10
2 files changed, 16 insertions, 2 deletions
diff --git a/django/db/backends/mysql/features.py b/django/db/backends/mysql/features.py
index 72fff72648..0840a4cf6b 100644
--- a/django/db/backends/mysql/features.py
+++ b/django/db/backends/mysql/features.py
@@ -14,7 +14,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
supports_forward_references = False
supports_regex_backreferencing = False
supports_date_lookup_using_string = False
- supports_index_column_ordering = False
supports_timezones = False
requires_explicit_null_ordering_when_grouping = True
can_release_savepoints = True
@@ -219,3 +218,10 @@ class DatabaseFeatures(BaseDatabaseFeatures):
if self.connection.mysql_is_mariadb:
return self.supports_json_field and self.can_introspect_check_constraints
return self.supports_json_field
+
+ @cached_property
+ def supports_index_column_ordering(self):
+ return (
+ not self.connection.mysql_is_mariadb and
+ self.connection.mysql_version >= (8, 0, 1)
+ )
diff --git a/django/db/backends/mysql/introspection.py b/django/db/backends/mysql/introspection.py
index 4c0178144a..649df5c6d9 100644
--- a/django/db/backends/mysql/introspection.py
+++ b/django/db/backends/mysql/introspection.py
@@ -229,6 +229,8 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
'check': False,
'foreign_key': (ref_table, ref_column) if ref_column else None,
}
+ if self.connection.features.supports_index_column_ordering:
+ constraints[constraint]['orders'] = []
constraints[constraint]['columns'].add(column)
# Now get the constraint types
type_query = """
@@ -289,7 +291,9 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
}
# Now add in the indexes
cursor.execute("SHOW INDEX FROM %s" % self.connection.ops.quote_name(table_name))
- for table, non_unique, index, colseq, column, type_ in [x[:5] + (x[10],) for x in cursor.fetchall()]:
+ for table, non_unique, index, colseq, column, order, type_ in [
+ x[:6] + (x[10],) for x in cursor.fetchall()
+ ]:
if index not in constraints:
constraints[index] = {
'columns': OrderedSet(),
@@ -298,9 +302,13 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
'check': False,
'foreign_key': None,
}
+ if self.connection.features.supports_index_column_ordering:
+ constraints[index]['orders'] = []
constraints[index]['index'] = True
constraints[index]['type'] = Index.suffix if type_ == 'BTREE' else type_.lower()
constraints[index]['columns'].add(column)
+ if self.connection.features.supports_index_column_ordering:
+ constraints[index]['orders'].append('DESC' if order == 'D' else 'ASC')
# Convert the sorted sets to lists
for constraint in constraints.values():
constraint['columns'] = list(constraint['columns'])