From 407d3cf39cd6a62f7277e401d646a4c7e8446879 Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Mon, 11 Jan 2021 11:45:20 +0100 Subject: [PATCH] Fixed #32342 -- Added index order introspection on MySQL 8.0.1+. --- django/db/backends/mysql/features.py | 8 +++++++- django/db/backends/mysql/introspection.py | 10 +++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/django/db/backends/mysql/features.py b/django/db/backends/mysql/features.py index 72fff72648c..0840a4cf6bc 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 4c0178144a4..649df5c6d99 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'])