features.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import operator
  2. from django.db import transaction
  3. from django.db.backends.base.features import BaseDatabaseFeatures
  4. from django.db.utils import OperationalError
  5. from django.utils.functional import cached_property
  6. from .base import Database
  7. class DatabaseFeatures(BaseDatabaseFeatures):
  8. minimum_database_version = (3, 21)
  9. test_db_allows_multiple_connections = False
  10. supports_unspecified_pk = True
  11. supports_timezones = False
  12. max_query_params = 999
  13. supports_transactions = True
  14. atomic_transactions = False
  15. can_rollback_ddl = True
  16. can_create_inline_fk = False
  17. requires_literal_defaults = True
  18. can_clone_databases = True
  19. supports_temporal_subtraction = True
  20. ignores_table_name_case = True
  21. supports_cast_with_precision = False
  22. time_cast_precision = 3
  23. can_release_savepoints = True
  24. has_case_insensitive_like = True
  25. # Is "ALTER TABLE ... RENAME COLUMN" supported?
  26. can_alter_table_rename_column = Database.sqlite_version_info >= (3, 25, 0)
  27. # Is "ALTER TABLE ... DROP COLUMN" supported?
  28. can_alter_table_drop_column = Database.sqlite_version_info >= (3, 35, 5)
  29. supports_parentheses_in_compound = False
  30. can_defer_constraint_checks = True
  31. supports_over_clause = Database.sqlite_version_info >= (3, 25, 0)
  32. supports_frame_range_fixed_distance = Database.sqlite_version_info >= (3, 28, 0)
  33. supports_aggregate_filter_clause = Database.sqlite_version_info >= (3, 30, 1)
  34. supports_order_by_nulls_modifier = Database.sqlite_version_info >= (3, 30, 0)
  35. # NULLS LAST/FIRST emulation on < 3.30 requires subquery wrapping.
  36. requires_compound_order_by_subquery = Database.sqlite_version_info < (3, 30)
  37. order_by_nulls_first = True
  38. supports_json_field_contains = False
  39. supports_update_conflicts = Database.sqlite_version_info >= (3, 24, 0)
  40. supports_update_conflicts_with_target = supports_update_conflicts
  41. test_collations = {
  42. "ci": "nocase",
  43. "cs": "binary",
  44. "non_default": "nocase",
  45. }
  46. django_test_expected_failures = {
  47. # The django_format_dtdelta() function doesn't properly handle mixed
  48. # Date/DateTime fields and timedeltas.
  49. "expressions.tests.FTimeDeltaTests.test_mixed_comparisons1",
  50. }
  51. create_test_table_with_composite_primary_key = """
  52. CREATE TABLE test_table_composite_pk (
  53. column_1 INTEGER NOT NULL,
  54. column_2 INTEGER NOT NULL,
  55. PRIMARY KEY(column_1, column_2)
  56. )
  57. """
  58. @cached_property
  59. def django_test_skips(self):
  60. skips = {
  61. "SQLite stores values rounded to 15 significant digits.": {
  62. "model_fields.test_decimalfield.DecimalFieldTests."
  63. "test_fetch_from_db_without_float_rounding",
  64. },
  65. "SQLite naively remakes the table on field alteration.": {
  66. "schema.tests.SchemaTests.test_unique_no_unnecessary_fk_drops",
  67. "schema.tests.SchemaTests.test_unique_and_reverse_m2m",
  68. "schema.tests.SchemaTests."
  69. "test_alter_field_default_doesnt_perform_queries",
  70. "schema.tests.SchemaTests."
  71. "test_rename_column_renames_deferred_sql_references",
  72. },
  73. "SQLite doesn't support negative precision for ROUND().": {
  74. "db_functions.math.test_round.RoundTests."
  75. "test_null_with_negative_precision",
  76. "db_functions.math.test_round.RoundTests."
  77. "test_decimal_with_negative_precision",
  78. "db_functions.math.test_round.RoundTests."
  79. "test_float_with_negative_precision",
  80. "db_functions.math.test_round.RoundTests."
  81. "test_integer_with_negative_precision",
  82. },
  83. }
  84. if Database.sqlite_version_info < (3, 27):
  85. skips.update(
  86. {
  87. "Nondeterministic failure on SQLite < 3.27.": {
  88. "expressions_window.tests.WindowFunctionTests."
  89. "test_subquery_row_range_rank",
  90. },
  91. }
  92. )
  93. if self.connection.is_in_memory_db():
  94. skips.update(
  95. {
  96. "the sqlite backend's close() method is a no-op when using an "
  97. "in-memory database": {
  98. "servers.test_liveserverthread.LiveServerThreadTest."
  99. "test_closes_connections",
  100. "servers.tests.LiveServerTestCloseConnectionTest."
  101. "test_closes_connections",
  102. },
  103. "For SQLite in-memory tests, closing the connection destroys"
  104. "the database.": {
  105. "test_utils.tests.AssertNumQueriesUponConnectionTests."
  106. "test_ignores_connection_configuration_queries",
  107. },
  108. }
  109. )
  110. else:
  111. skips.update(
  112. {
  113. "Only connections to in-memory SQLite databases are passed to the "
  114. "server thread.": {
  115. "servers.tests.LiveServerInMemoryDatabaseLockTest."
  116. "test_in_memory_database_lock",
  117. },
  118. "multiprocessing's start method is checked only for in-memory "
  119. "SQLite databases": {
  120. "backends.sqlite.test_creation.TestDbSignatureTests."
  121. "test_get_test_db_clone_settings_not_supported",
  122. },
  123. }
  124. )
  125. return skips
  126. @cached_property
  127. def supports_atomic_references_rename(self):
  128. return Database.sqlite_version_info >= (3, 26, 0)
  129. @cached_property
  130. def introspected_field_types(self):
  131. return {
  132. **super().introspected_field_types,
  133. "BigAutoField": "AutoField",
  134. "DurationField": "BigIntegerField",
  135. "GenericIPAddressField": "CharField",
  136. "SmallAutoField": "AutoField",
  137. }
  138. @cached_property
  139. def supports_json_field(self):
  140. with self.connection.cursor() as cursor:
  141. try:
  142. with transaction.atomic(self.connection.alias):
  143. cursor.execute('SELECT JSON(\'{"a": "b"}\')')
  144. except OperationalError:
  145. return False
  146. return True
  147. can_introspect_json_field = property(operator.attrgetter("supports_json_field"))
  148. has_json_object_function = property(operator.attrgetter("supports_json_field"))
  149. @cached_property
  150. def can_return_columns_from_insert(self):
  151. return Database.sqlite_version_info >= (3, 35)
  152. can_return_rows_from_bulk_insert = property(
  153. operator.attrgetter("can_return_columns_from_insert")
  154. )