features.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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, 4)
  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. 10,
  115. 4,
  116. 3,
  117. ) < self.connection.mysql_version < (10, 5, 2):
  118. skips.update(
  119. {
  120. "https://jira.mariadb.org/browse/MDEV-19598": {
  121. "schema.tests.SchemaTests."
  122. "test_alter_not_unique_field_to_primary_key",
  123. },
  124. }
  125. )
  126. if self.connection.mysql_is_mariadb and (
  127. 10,
  128. 4,
  129. 12,
  130. ) < self.connection.mysql_version < (10, 5):
  131. skips.update(
  132. {
  133. "https://jira.mariadb.org/browse/MDEV-22775": {
  134. "schema.tests.SchemaTests."
  135. "test_alter_pk_with_self_referential_field",
  136. },
  137. }
  138. )
  139. if not self.supports_explain_analyze:
  140. skips.update(
  141. {
  142. "MariaDB and MySQL >= 8.0.18 specific.": {
  143. "queries.test_explain.ExplainTests.test_mysql_analyze",
  144. },
  145. }
  146. )
  147. if "ONLY_FULL_GROUP_BY" in self.connection.sql_mode:
  148. skips.update(
  149. {
  150. "GROUP BY cannot contain nonaggregated column when "
  151. "ONLY_FULL_GROUP_BY mode is enabled on MySQL, see #34262.": {
  152. "aggregation.tests.AggregateTestCase."
  153. "test_group_by_nested_expression_with_params",
  154. },
  155. }
  156. )
  157. if self.connection.mysql_version < (8, 0, 31):
  158. skips.update(
  159. {
  160. "Nesting of UNIONs at the right-hand side is not supported on "
  161. "MySQL < 8.0.31": {
  162. "queries.test_qs_combinators.QuerySetSetOperationTests."
  163. "test_union_nested"
  164. },
  165. }
  166. )
  167. return skips
  168. @cached_property
  169. def _mysql_storage_engine(self):
  170. "Internal method used in Django tests. Don't rely on this from your code"
  171. return self.connection.mysql_server_data["default_storage_engine"]
  172. @cached_property
  173. def allows_auto_pk_0(self):
  174. """
  175. Autoincrement primary key can be set to 0 if it doesn't generate new
  176. autoincrement values.
  177. """
  178. return "NO_AUTO_VALUE_ON_ZERO" in self.connection.sql_mode
  179. @cached_property
  180. def update_can_self_select(self):
  181. return self.connection.mysql_is_mariadb
  182. @cached_property
  183. def can_introspect_foreign_keys(self):
  184. "Confirm support for introspected foreign keys"
  185. return self._mysql_storage_engine != "MyISAM"
  186. @cached_property
  187. def introspected_field_types(self):
  188. return {
  189. **super().introspected_field_types,
  190. "BinaryField": "TextField",
  191. "BooleanField": "IntegerField",
  192. "DurationField": "BigIntegerField",
  193. "GenericIPAddressField": "CharField",
  194. }
  195. @cached_property
  196. def can_return_columns_from_insert(self):
  197. return self.connection.mysql_is_mariadb and self.connection.mysql_version >= (
  198. 10,
  199. 5,
  200. 0,
  201. )
  202. can_return_rows_from_bulk_insert = property(
  203. operator.attrgetter("can_return_columns_from_insert")
  204. )
  205. @cached_property
  206. def has_zoneinfo_database(self):
  207. return self.connection.mysql_server_data["has_zoneinfo_database"]
  208. @cached_property
  209. def is_sql_auto_is_null_enabled(self):
  210. return self.connection.mysql_server_data["sql_auto_is_null"]
  211. @cached_property
  212. def supports_column_check_constraints(self):
  213. if self.connection.mysql_is_mariadb:
  214. return True
  215. return self.connection.mysql_version >= (8, 0, 16)
  216. supports_table_check_constraints = property(
  217. operator.attrgetter("supports_column_check_constraints")
  218. )
  219. @cached_property
  220. def can_introspect_check_constraints(self):
  221. if self.connection.mysql_is_mariadb:
  222. return True
  223. return self.connection.mysql_version >= (8, 0, 16)
  224. @cached_property
  225. def has_select_for_update_skip_locked(self):
  226. if self.connection.mysql_is_mariadb:
  227. return self.connection.mysql_version >= (10, 6)
  228. return True
  229. @cached_property
  230. def has_select_for_update_of(self):
  231. return not self.connection.mysql_is_mariadb
  232. @cached_property
  233. def supports_explain_analyze(self):
  234. return self.connection.mysql_is_mariadb or self.connection.mysql_version >= (
  235. 8,
  236. 0,
  237. 18,
  238. )
  239. @cached_property
  240. def supported_explain_formats(self):
  241. # Alias MySQL's TRADITIONAL to TEXT for consistency with other
  242. # backends.
  243. formats = {"JSON", "TEXT", "TRADITIONAL"}
  244. if not self.connection.mysql_is_mariadb and self.connection.mysql_version >= (
  245. 8,
  246. 0,
  247. 16,
  248. ):
  249. formats.add("TREE")
  250. return formats
  251. @cached_property
  252. def supports_transactions(self):
  253. """
  254. All storage engines except MyISAM support transactions.
  255. """
  256. return self._mysql_storage_engine != "MyISAM"
  257. @cached_property
  258. def ignores_table_name_case(self):
  259. return self.connection.mysql_server_data["lower_case_table_names"]
  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. @cached_property
  265. def can_introspect_json_field(self):
  266. if self.connection.mysql_is_mariadb:
  267. return self.can_introspect_check_constraints
  268. return True
  269. @cached_property
  270. def supports_index_column_ordering(self):
  271. if self._mysql_storage_engine != "InnoDB":
  272. return False
  273. if self.connection.mysql_is_mariadb:
  274. return self.connection.mysql_version >= (10, 8)
  275. return True
  276. @cached_property
  277. def supports_expression_indexes(self):
  278. return (
  279. not self.connection.mysql_is_mariadb
  280. and self._mysql_storage_engine != "MyISAM"
  281. and self.connection.mysql_version >= (8, 0, 13)
  282. )
  283. @cached_property
  284. def supports_select_intersection(self):
  285. is_mariadb = self.connection.mysql_is_mariadb
  286. return is_mariadb or self.connection.mysql_version >= (8, 0, 31)
  287. supports_select_difference = property(
  288. operator.attrgetter("supports_select_intersection")
  289. )
  290. @cached_property
  291. def can_rename_index(self):
  292. if self.connection.mysql_is_mariadb:
  293. return self.connection.mysql_version >= (10, 5, 2)
  294. return True
  295. @cached_property
  296. def supports_expression_defaults(self):
  297. if self.connection.mysql_is_mariadb:
  298. return True
  299. return self.connection.mysql_version >= (8, 0, 13)
  300. @cached_property
  301. def has_native_uuid_field(self):
  302. is_mariadb = self.connection.mysql_is_mariadb
  303. return is_mariadb and self.connection.mysql_version >= (10, 7)
  304. @cached_property
  305. def allows_group_by_selected_pks(self):
  306. if self.connection.mysql_is_mariadb:
  307. return "ONLY_FULL_GROUP_BY" not in self.connection.sql_mode
  308. return True