Fixed #32342 -- Added index order introspection on MySQL 8.0.1+.

This commit is contained in:
Mariusz Felisiak 2021-01-11 11:45:20 +01:00 committed by GitHub
parent cd3019bc10
commit 407d3cf39c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 2 deletions

View File

@ -14,7 +14,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
supports_forward_references = False supports_forward_references = False
supports_regex_backreferencing = False supports_regex_backreferencing = False
supports_date_lookup_using_string = False supports_date_lookup_using_string = False
supports_index_column_ordering = False
supports_timezones = False supports_timezones = False
requires_explicit_null_ordering_when_grouping = True requires_explicit_null_ordering_when_grouping = True
can_release_savepoints = True can_release_savepoints = True
@ -219,3 +218,10 @@ class DatabaseFeatures(BaseDatabaseFeatures):
if self.connection.mysql_is_mariadb: if self.connection.mysql_is_mariadb:
return self.supports_json_field and self.can_introspect_check_constraints return self.supports_json_field and self.can_introspect_check_constraints
return self.supports_json_field 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)
)

View File

@ -229,6 +229,8 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
'check': False, 'check': False,
'foreign_key': (ref_table, ref_column) if ref_column else None, '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) constraints[constraint]['columns'].add(column)
# Now get the constraint types # Now get the constraint types
type_query = """ type_query = """
@ -289,7 +291,9 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
} }
# Now add in the indexes # Now add in the indexes
cursor.execute("SHOW INDEX FROM %s" % self.connection.ops.quote_name(table_name)) 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: if index not in constraints:
constraints[index] = { constraints[index] = {
'columns': OrderedSet(), 'columns': OrderedSet(),
@ -298,9 +302,13 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
'check': False, 'check': False,
'foreign_key': None, 'foreign_key': None,
} }
if self.connection.features.supports_index_column_ordering:
constraints[index]['orders'] = []
constraints[index]['index'] = True constraints[index]['index'] = True
constraints[index]['type'] = Index.suffix if type_ == 'BTREE' else type_.lower() constraints[index]['type'] = Index.suffix if type_ == 'BTREE' else type_.lower()
constraints[index]['columns'].add(column) 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 # Convert the sorted sets to lists
for constraint in constraints.values(): for constraint in constraints.values():
constraint['columns'] = list(constraint['columns']) constraint['columns'] = list(constraint['columns'])