features.py 12 KB

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