features.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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, 31)
  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 ... DROP COLUMN" supported?
  26. can_alter_table_drop_column = Database.sqlite_version_info >= (3, 35, 5)
  27. supports_parentheses_in_compound = False
  28. can_defer_constraint_checks = True
  29. supports_over_clause = True
  30. supports_frame_range_fixed_distance = True
  31. supports_frame_exclusion = True
  32. supports_aggregate_filter_clause = True
  33. order_by_nulls_first = True
  34. supports_json_field_contains = False
  35. supports_update_conflicts = True
  36. supports_update_conflicts_with_target = True
  37. supports_stored_generated_columns = True
  38. supports_virtual_generated_columns = True
  39. test_collations = {
  40. "ci": "nocase",
  41. "cs": "binary",
  42. "non_default": "nocase",
  43. "virtual": "nocase",
  44. }
  45. django_test_expected_failures = {
  46. # The django_format_dtdelta() function doesn't properly handle mixed
  47. # Date/DateTime fields and timedeltas.
  48. "expressions.tests.FTimeDeltaTests.test_mixed_comparisons1",
  49. }
  50. create_test_table_with_composite_primary_key = """
  51. CREATE TABLE test_table_composite_pk (
  52. column_1 INTEGER NOT NULL,
  53. column_2 INTEGER NOT NULL,
  54. PRIMARY KEY(column_1, column_2)
  55. )
  56. """
  57. insert_test_table_with_defaults = 'INSERT INTO {} ("null") VALUES (1)'
  58. supports_default_keyword_in_insert = False
  59. @cached_property
  60. def django_test_skips(self):
  61. skips = {
  62. "SQLite stores values rounded to 15 significant digits.": {
  63. "model_fields.test_decimalfield.DecimalFieldTests."
  64. "test_fetch_from_db_without_float_rounding",
  65. },
  66. "SQLite naively remakes the table on field alteration.": {
  67. "schema.tests.SchemaTests.test_unique_no_unnecessary_fk_drops",
  68. "schema.tests.SchemaTests.test_unique_and_reverse_m2m",
  69. "schema.tests.SchemaTests."
  70. "test_alter_field_default_doesnt_perform_queries",
  71. "schema.tests.SchemaTests."
  72. "test_rename_column_renames_deferred_sql_references",
  73. },
  74. "SQLite doesn't support negative precision for ROUND().": {
  75. "db_functions.math.test_round.RoundTests."
  76. "test_null_with_negative_precision",
  77. "db_functions.math.test_round.RoundTests."
  78. "test_decimal_with_negative_precision",
  79. "db_functions.math.test_round.RoundTests."
  80. "test_float_with_negative_precision",
  81. "db_functions.math.test_round.RoundTests."
  82. "test_integer_with_negative_precision",
  83. },
  84. "The actual query cannot be determined on SQLite": {
  85. "backends.base.test_base.ExecuteWrapperTests.test_wrapper_debug",
  86. },
  87. }
  88. if self.connection.is_in_memory_db():
  89. skips.update(
  90. {
  91. "the sqlite backend's close() method is a no-op when using an "
  92. "in-memory database": {
  93. "servers.test_liveserverthread.LiveServerThreadTest."
  94. "test_closes_connections",
  95. "servers.tests.LiveServerTestCloseConnectionTest."
  96. "test_closes_connections",
  97. },
  98. "For SQLite in-memory tests, closing the connection destroys"
  99. "the database.": {
  100. "test_utils.tests.AssertNumQueriesUponConnectionTests."
  101. "test_ignores_connection_configuration_queries",
  102. },
  103. }
  104. )
  105. else:
  106. skips.update(
  107. {
  108. "Only connections to in-memory SQLite databases are passed to the "
  109. "server thread.": {
  110. "servers.tests.LiveServerInMemoryDatabaseLockTest."
  111. "test_in_memory_database_lock",
  112. },
  113. "multiprocessing's start method is checked only for in-memory "
  114. "SQLite databases": {
  115. "backends.sqlite.test_creation.TestDbSignatureTests."
  116. "test_get_test_db_clone_settings_not_supported",
  117. },
  118. }
  119. )
  120. return skips
  121. @cached_property
  122. def introspected_field_types(self):
  123. return {
  124. **super().introspected_field_types,
  125. "BigAutoField": "AutoField",
  126. "DurationField": "BigIntegerField",
  127. "GenericIPAddressField": "CharField",
  128. "SmallAutoField": "AutoField",
  129. }
  130. @cached_property
  131. def supports_json_field(self):
  132. with self.connection.cursor() as cursor:
  133. try:
  134. with transaction.atomic(self.connection.alias):
  135. cursor.execute('SELECT JSON(\'{"a": "b"}\')')
  136. except OperationalError:
  137. return False
  138. return True
  139. can_introspect_json_field = property(operator.attrgetter("supports_json_field"))
  140. has_json_object_function = property(operator.attrgetter("supports_json_field"))
  141. @cached_property
  142. def can_return_columns_from_insert(self):
  143. return Database.sqlite_version_info >= (3, 35)
  144. can_return_rows_from_bulk_insert = property(
  145. operator.attrgetter("can_return_columns_from_insert")
  146. )