| 1 |
import operator |
| 2 |
|
| 3 |
from django.db.backends.base.features import BaseDatabaseFeatures |
| 4 |
from django.utils.functional import cached_property |
| 5 |
|
| 6 |
|
| 7 |
class DatabaseFeatures(BaseDatabaseFeatures): |
| 8 |
empty_fetchmany_value = () |
| 9 |
related_fields_match_type = True |
| 10 |
# MySQL doesn't support sliced subqueries with IN/ALL/ANY/SOME. |
| 11 |
allow_sliced_subqueries_with_in = False |
| 12 |
has_select_for_update = True |
| 13 |
has_select_for_update_nowait = True |
| 14 |
supports_forward_references = False |
| 15 |
supports_regex_backreferencing = False |
| 16 |
supports_date_lookup_using_string = False |
| 17 |
supports_timezones = False |
| 18 |
requires_explicit_null_ordering_when_grouping = True |
| 19 |
atomic_transactions = False |
| 20 |
can_clone_databases = True |
| 21 |
supports_comments = True |
| 22 |
supports_comments_inline = True |
| 23 |
supports_temporal_subtraction = True |
| 24 |
supports_slicing_ordering_in_compound = True |
| 25 |
supports_index_on_text_field = False |
| 26 |
supports_over_clause = True |
| 27 |
supports_frame_range_fixed_distance = True |
| 28 |
supports_update_conflicts = True |
| 29 |
delete_can_self_reference_subquery = False |
| 30 |
create_test_procedure_without_params_sql = """ |
| 31 |
CREATE PROCEDURE test_procedure () |
| 32 |
BEGIN |
| 33 |
DECLARE V_I INTEGER; |
| 34 |
SET V_I = 1; |
| 35 |
END; |
| 36 |
""" |
| 37 |
create_test_procedure_with_int_param_sql = """ |
| 38 |
CREATE PROCEDURE test_procedure (P_I INTEGER) |
| 39 |
BEGIN |
| 40 |
DECLARE V_I INTEGER; |
| 41 |
SET V_I = P_I; |
| 42 |
END; |
| 43 |
""" |
| 44 |
# Neither MySQL nor MariaDB support partial indexes. |
| 45 |
supports_partial_indexes = False |
| 46 |
# COLLATE must be wrapped in parentheses because MySQL treats COLLATE as an |
| 47 |
# indexed expression. |
| 48 |
collate_as_index_expression = True |
| 49 |
insert_test_table_with_defaults = "INSERT INTO {} () VALUES ()" |
| 50 |
|
| 51 |
supports_order_by_nulls_modifier = False |
| 52 |
order_by_nulls_first = True |
| 53 |
supports_logical_xor = True |
| 54 |
|
| 55 |
supports_stored_generated_columns = True |
| 56 |
supports_virtual_generated_columns = True |
| 57 |
|
| 58 |
@cached_property |
| 59 |
def minimum_database_version(self): |
| 60 |
if self.connection.mysql_is_mariadb: |
| 61 |
return (10, 5) |
| 62 |
else: |
| 63 |
return (8, 0, 11) |
| 64 |
|
| 65 |
@cached_property |
| 66 |
def test_collations(self): |
| 67 |
return { |
| 68 |
"ci": "utf8mb4_general_ci", |
| 69 |
"non_default": "utf8mb4_esperanto_ci", |
| 70 |
"swedish_ci": "utf8mb4_swedish_ci", |
| 71 |
"virtual": "utf8mb4_esperanto_ci", |
| 72 |
} |
| 73 |
|
| 74 |
test_now_utc_template = "UTC_TIMESTAMP(6)" |
| 75 |
|
| 76 |
@cached_property |
| 77 |
def django_test_skips(self): |
| 78 |
skips = { |
| 79 |
"This doesn't work on MySQL.": { |
| 80 |
"db_functions.comparison.test_greatest.GreatestTests." |
| 81 |
"test_coalesce_workaround", |
| 82 |
"db_functions.comparison.test_least.LeastTests." |
| 83 |
"test_coalesce_workaround", |
| 84 |
}, |
| 85 |
"MySQL doesn't support functional indexes on a function that " |
| 86 |
"returns JSON": { |
| 87 |
"schema.tests.SchemaTests.test_func_index_json_key_transform", |
| 88 |
}, |
| 89 |
"MySQL supports multiplying and dividing DurationFields by a " |
| 90 |
"scalar value but it's not implemented (#25287).": { |
| 91 |
"expressions.tests.FTimeDeltaTests.test_durationfield_multiply_divide", |
| 92 |
}, |
| 93 |
"UPDATE ... ORDER BY syntax on MySQL/MariaDB does not support ordering by" |
| 94 |
"related fields.": { |
| 95 |
"update.tests.AdvancedTests." |
| 96 |
"test_update_ordered_by_inline_m2m_annotation", |
| 97 |
"update.tests.AdvancedTests.test_update_ordered_by_m2m_annotation", |
| 98 |
"update.tests.AdvancedTests.test_update_ordered_by_m2m_annotation_desc", |
| 99 |
}, |
| 100 |
} |
| 101 |
if self.connection.mysql_is_mariadb and ( |
| 102 |
self.connection.mysql_version < (10, 5, 2) |
| 103 |
): |
| 104 |
skips.update( |
| 105 |
{ |
| 106 |
"https://jira.mariadb.org/browse/MDEV-19598": { |
| 107 |
"schema.tests.SchemaTests." |
| 108 |
"test_alter_not_unique_field_to_primary_key", |
| 109 |
}, |
| 110 |
} |
| 111 |
) |
| 112 |
if not self.supports_explain_analyze: |
| 113 |
skips.update( |
| 114 |
{ |
| 115 |
"MariaDB and MySQL >= 8.0.18 specific.": { |
| 116 |
"queries.test_explain.ExplainTests.test_mysql_analyze", |
| 117 |
}, |
| 118 |
} |
| 119 |
) |
| 120 |
if "ONLY_FULL_GROUP_BY" in self.connection.sql_mode: |
| 121 |
skips.update( |
| 122 |
{ |
| 123 |
"GROUP BY cannot contain nonaggregated column when " |
| 124 |
"ONLY_FULL_GROUP_BY mode is enabled on MySQL, see #34262.": { |
| 125 |
"aggregation.tests.AggregateTestCase." |
| 126 |
"test_group_by_nested_expression_with_params", |
| 127 |
}, |
| 128 |
} |
| 129 |
) |
| 130 |
if self.connection.mysql_version < (8, 0, 31): |
| 131 |
skips.update( |
| 132 |
{ |
| 133 |
"Nesting of UNIONs at the right-hand side is not supported on " |
| 134 |
"MySQL < 8.0.31": { |
| 135 |
"queries.test_qs_combinators.QuerySetSetOperationTests." |
| 136 |
"test_union_nested" |
| 137 |
}, |
| 138 |
} |
| 139 |
) |
| 140 |
if not self.connection.mysql_is_mariadb: |
| 141 |
skips.update( |
| 142 |
{ |
| 143 |
"MySQL doesn't allow renaming columns referenced by generated " |
| 144 |
"columns": { |
| 145 |
"migrations.test_operations.OperationTests." |
| 146 |
"test_invalid_generated_field_changes_on_rename_stored", |
| 147 |
"migrations.test_operations.OperationTests." |
| 148 |
"test_invalid_generated_field_changes_on_rename_virtual", |
| 149 |
}, |
| 150 |
} |
| 151 |
) |
| 152 |
return skips |
| 153 |
|
| 154 |
@cached_property |
| 155 |
def _mysql_storage_engine(self): |
| 156 |
"Internal method used in Django tests. Don't rely on this from your code" |
| 157 |
return self.connection.mysql_server_data["default_storage_engine"] |
| 158 |
|
| 159 |
@cached_property |
| 160 |
def allows_auto_pk_0(self): |
| 161 |
""" |
| 162 |
Autoincrement primary key can be set to 0 if it doesn't generate new |
| 163 |
autoincrement values. |
| 164 |
""" |
| 165 |
return "NO_AUTO_VALUE_ON_ZERO" in self.connection.sql_mode |
| 166 |
|
| 167 |
@cached_property |
| 168 |
def update_can_self_select(self): |
| 169 |
return self.connection.mysql_is_mariadb |
| 170 |
|
| 171 |
@cached_property |
| 172 |
def can_introspect_foreign_keys(self): |
| 173 |
"Confirm support for introspected foreign keys" |
| 174 |
return self._mysql_storage_engine != "MyISAM" |
| 175 |
|
| 176 |
@cached_property |
| 177 |
def introspected_field_types(self): |
| 178 |
return { |
| 179 |
**super().introspected_field_types, |
| 180 |
"BinaryField": "TextField", |
| 181 |
"BooleanField": "IntegerField", |
| 182 |
"DurationField": "BigIntegerField", |
| 183 |
"GenericIPAddressField": "CharField", |
| 184 |
} |
| 185 |
|
| 186 |
@cached_property |
| 187 |
def can_return_columns_from_insert(self): |
| 188 |
return self.connection.mysql_is_mariadb |
| 189 |
|
| 190 |
can_return_rows_from_bulk_insert = property( |
| 191 |
operator.attrgetter("can_return_columns_from_insert") |
| 192 |
) |
| 193 |
|
| 194 |
@cached_property |
| 195 |
def has_zoneinfo_database(self): |
| 196 |
return self.connection.mysql_server_data["has_zoneinfo_database"] |
| 197 |
|
| 198 |
@cached_property |
| 199 |
def is_sql_auto_is_null_enabled(self): |
| 200 |
return self.connection.mysql_server_data["sql_auto_is_null"] |
| 201 |
|
| 202 |
@cached_property |
| 203 |
def supports_column_check_constraints(self): |
| 204 |
if self.connection.mysql_is_mariadb: |
| 205 |
return True |
| 206 |
return self.connection.mysql_version >= (8, 0, 16) |
| 207 |
|
| 208 |
supports_table_check_constraints = property( |
| 209 |
operator.attrgetter("supports_column_check_constraints") |
| 210 |
) |
| 211 |
|
| 212 |
@cached_property |
| 213 |
def can_introspect_check_constraints(self): |
| 214 |
if self.connection.mysql_is_mariadb: |
| 215 |
return True |
| 216 |
return self.connection.mysql_version >= (8, 0, 16) |
| 217 |
|
| 218 |
@cached_property |
| 219 |
def has_select_for_update_skip_locked(self): |
| 220 |
if self.connection.mysql_is_mariadb: |
| 221 |
return self.connection.mysql_version >= (10, 6) |
| 222 |
return True |
| 223 |
|
| 224 |
@cached_property |
| 225 |
def has_select_for_update_of(self): |
| 226 |
return not self.connection.mysql_is_mariadb |
| 227 |
|
| 228 |
@cached_property |
| 229 |
def supports_explain_analyze(self): |
| 230 |
return self.connection.mysql_is_mariadb or self.connection.mysql_version >= ( |
| 231 |
8, |
| 232 |
0, |
| 233 |
18, |
| 234 |
) |
| 235 |
|
| 236 |
@cached_property |
| 237 |
def supported_explain_formats(self): |
| 238 |
# Alias MySQL's TRADITIONAL to TEXT for consistency with other |
| 239 |
# backends. |
| 240 |
formats = {"JSON", "TEXT", "TRADITIONAL"} |
| 241 |
if not self.connection.mysql_is_mariadb and self.connection.mysql_version >= ( |
| 242 |
8, |
| 243 |
0, |
| 244 |
16, |
| 245 |
): |
| 246 |
formats.add("TREE") |
| 247 |
return formats |
| 248 |
|
| 249 |
@cached_property |
| 250 |
def supports_transactions(self): |
| 251 |
""" |
| 252 |
All storage engines except MyISAM support transactions. |
| 253 |
""" |
| 254 |
return self._mysql_storage_engine != "MyISAM" |
| 255 |
|
| 256 |
@cached_property |
| 257 |
def ignores_table_name_case(self): |
| 258 |
return self.connection.mysql_server_data["lower_case_table_names"] |
| 259 |
|
| 260 |
@cached_property |
| 261 |
def supports_default_in_lead_lag(self): |
| 262 |
# To be added in https://jira.mariadb.org/browse/MDEV-12981. |
| 263 |
return not self.connection.mysql_is_mariadb |
| 264 |
|
| 265 |
@cached_property |
| 266 |
def can_introspect_json_field(self): |
| 267 |
if self.connection.mysql_is_mariadb: |
| 268 |
return self.can_introspect_check_constraints |
| 269 |
return True |
| 270 |
|
| 271 |
@cached_property |
| 272 |
def supports_index_column_ordering(self): |
| 273 |
if self._mysql_storage_engine != "InnoDB": |
| 274 |
return False |
| 275 |
if self.connection.mysql_is_mariadb: |
| 276 |
return self.connection.mysql_version >= (10, 8) |
| 277 |
return True |
| 278 |
|
| 279 |
@cached_property |
| 280 |
def supports_expression_indexes(self): |
| 281 |
return ( |
| 282 |
not self.connection.mysql_is_mariadb |
| 283 |
and self._mysql_storage_engine != "MyISAM" |
| 284 |
and self.connection.mysql_version >= (8, 0, 13) |
| 285 |
) |
| 286 |
|
| 287 |
@cached_property |
| 288 |
def supports_select_intersection(self): |
| 289 |
is_mariadb = self.connection.mysql_is_mariadb |
| 290 |
return is_mariadb or self.connection.mysql_version >= (8, 0, 31) |
| 291 |
|
| 292 |
supports_select_difference = property( |
| 293 |
operator.attrgetter("supports_select_intersection") |
| 294 |
) |
| 295 |
|
| 296 |
@cached_property |
| 297 |
def can_rename_index(self): |
| 298 |
if self.connection.mysql_is_mariadb: |
| 299 |
return self.connection.mysql_version >= (10, 5, 2) |
| 300 |
return True |
| 301 |
|
| 302 |
@cached_property |
| 303 |
def supports_expression_defaults(self): |
| 304 |
if self.connection.mysql_is_mariadb: |
| 305 |
return True |
| 306 |
return self.connection.mysql_version >= (8, 0, 13) |
| 307 |
|
| 308 |
@cached_property |
| 309 |
def has_native_uuid_field(self): |
| 310 |
is_mariadb = self.connection.mysql_is_mariadb |
| 311 |
return is_mariadb and self.connection.mysql_version >= (10, 7) |
| 312 |
|
| 313 |
@cached_property |
| 314 |
def allows_group_by_selected_pks(self): |
| 315 |
if self.connection.mysql_is_mariadb: |
| 316 |
return "ONLY_FULL_GROUP_BY" not in self.connection.sql_mode |
| 317 |
return True |