| 1 |
import operator |
| 2 |
|
| 3 |
from django.db import DataError, InterfaceError |
| 4 |
from django.db.backends.base.features import BaseDatabaseFeatures |
| 5 |
from django.db.backends.postgresql.psycopg_any import is_psycopg3 |
| 6 |
from django.utils.functional import cached_property |
| 7 |
|
| 8 |
|
| 9 |
class DatabaseFeatures(BaseDatabaseFeatures): |
| 10 |
minimum_database_version = (14,) |
| 11 |
allows_group_by_selected_pks = True |
| 12 |
can_return_columns_from_insert = True |
| 13 |
can_return_rows_from_bulk_insert = True |
| 14 |
has_real_datatype = True |
| 15 |
has_native_uuid_field = True |
| 16 |
has_native_duration_field = True |
| 17 |
has_native_json_field = True |
| 18 |
can_defer_constraint_checks = True |
| 19 |
has_select_for_update = True |
| 20 |
has_select_for_update_nowait = True |
| 21 |
has_select_for_update_of = True |
| 22 |
has_select_for_update_skip_locked = True |
| 23 |
has_select_for_no_key_update = True |
| 24 |
can_release_savepoints = True |
| 25 |
supports_comments = True |
| 26 |
supports_tablespaces = True |
| 27 |
supports_transactions = True |
| 28 |
can_introspect_materialized_views = True |
| 29 |
can_distinct_on_fields = True |
| 30 |
can_rollback_ddl = True |
| 31 |
schema_editor_uses_clientside_param_binding = True |
| 32 |
supports_combined_alters = True |
| 33 |
nulls_order_largest = True |
| 34 |
closed_cursor_error_class = InterfaceError |
| 35 |
greatest_least_ignores_nulls = True |
| 36 |
can_clone_databases = True |
| 37 |
supports_temporal_subtraction = True |
| 38 |
supports_slicing_ordering_in_compound = True |
| 39 |
create_test_procedure_without_params_sql = """ |
| 40 |
CREATE FUNCTION test_procedure () RETURNS void AS $$ |
| 41 |
DECLARE |
| 42 |
V_I INTEGER; |
| 43 |
BEGIN |
| 44 |
V_I := 1; |
| 45 |
END; |
| 46 |
$$ LANGUAGE plpgsql;""" |
| 47 |
create_test_procedure_with_int_param_sql = """ |
| 48 |
CREATE FUNCTION test_procedure (P_I INTEGER) RETURNS void AS $$ |
| 49 |
DECLARE |
| 50 |
V_I INTEGER; |
| 51 |
BEGIN |
| 52 |
V_I := P_I; |
| 53 |
END; |
| 54 |
$$ LANGUAGE plpgsql;""" |
| 55 |
requires_casted_case_in_updates = True |
| 56 |
supports_over_clause = True |
| 57 |
supports_frame_exclusion = True |
| 58 |
only_supports_unbounded_with_preceding_and_following = True |
| 59 |
supports_aggregate_filter_clause = True |
| 60 |
supported_explain_formats = {"JSON", "TEXT", "XML", "YAML"} |
| 61 |
supports_deferrable_unique_constraints = True |
| 62 |
has_json_operators = True |
| 63 |
json_key_contains_list_matching_requires_list = True |
| 64 |
supports_update_conflicts = True |
| 65 |
supports_update_conflicts_with_target = True |
| 66 |
supports_covering_indexes = True |
| 67 |
supports_stored_generated_columns = True |
| 68 |
supports_virtual_generated_columns = False |
| 69 |
can_rename_index = True |
| 70 |
test_collations = { |
| 71 |
"deterministic": "C", |
| 72 |
"non_default": "sv-x-icu", |
| 73 |
"swedish_ci": "sv-x-icu", |
| 74 |
"virtual": "sv-x-icu", |
| 75 |
} |
| 76 |
test_now_utc_template = "STATEMENT_TIMESTAMP() AT TIME ZONE 'UTC'" |
| 77 |
insert_test_table_with_defaults = "INSERT INTO {} DEFAULT VALUES" |
| 78 |
|
| 79 |
@cached_property |
| 80 |
def django_test_skips(self): |
| 81 |
skips = { |
| 82 |
"opclasses are PostgreSQL only.": { |
| 83 |
"indexes.tests.SchemaIndexesNotPostgreSQLTests." |
| 84 |
"test_create_index_ignores_opclasses", |
| 85 |
}, |
| 86 |
"PostgreSQL requires casting to text.": { |
| 87 |
"lookup.tests.LookupTests.test_textfield_exact_null", |
| 88 |
}, |
| 89 |
} |
| 90 |
if self.connection.settings_dict["OPTIONS"].get("pool"): |
| 91 |
skips.update( |
| 92 |
{ |
| 93 |
"Pool does implicit health checks": { |
| 94 |
"backends.base.test_base.ConnectionHealthChecksTests." |
| 95 |
"test_health_checks_enabled", |
| 96 |
"backends.base.test_base.ConnectionHealthChecksTests." |
| 97 |
"test_set_autocommit_health_checks_enabled", |
| 98 |
}, |
| 99 |
} |
| 100 |
) |
| 101 |
if self.uses_server_side_binding: |
| 102 |
skips.update( |
| 103 |
{ |
| 104 |
"The actual query cannot be determined for server side bindings": { |
| 105 |
"backends.base.test_base.ExecuteWrapperTests." |
| 106 |
"test_wrapper_debug", |
| 107 |
} |
| 108 |
}, |
| 109 |
) |
| 110 |
return skips |
| 111 |
|
| 112 |
@cached_property |
| 113 |
def django_test_expected_failures(self): |
| 114 |
expected_failures = set() |
| 115 |
if self.uses_server_side_binding: |
| 116 |
expected_failures.update( |
| 117 |
{ |
| 118 |
# Parameters passed to expressions in SELECT and GROUP BY |
| 119 |
# clauses are not recognized as the same values when using |
| 120 |
# server-side binding cursors (#34255). |
| 121 |
"aggregation.tests.AggregateTestCase." |
| 122 |
"test_group_by_nested_expression_with_params", |
| 123 |
} |
| 124 |
) |
| 125 |
return expected_failures |
| 126 |
|
| 127 |
@cached_property |
| 128 |
def uses_server_side_binding(self): |
| 129 |
options = self.connection.settings_dict["OPTIONS"] |
| 130 |
return is_psycopg3 and options.get("server_side_binding") is True |
| 131 |
|
| 132 |
@cached_property |
| 133 |
def prohibits_null_characters_in_text_exception(self): |
| 134 |
if is_psycopg3: |
| 135 |
return DataError, "PostgreSQL text fields cannot contain NUL (0x00) bytes" |
| 136 |
else: |
| 137 |
return ValueError, "A string literal cannot contain NUL (0x00) characters." |
| 138 |
|
| 139 |
@cached_property |
| 140 |
def introspected_field_types(self): |
| 141 |
return { |
| 142 |
**super().introspected_field_types, |
| 143 |
"PositiveBigIntegerField": "BigIntegerField", |
| 144 |
"PositiveIntegerField": "IntegerField", |
| 145 |
"PositiveSmallIntegerField": "SmallIntegerField", |
| 146 |
} |
| 147 |
|
| 148 |
@cached_property |
| 149 |
def is_postgresql_15(self): |
| 150 |
return self.connection.pg_version >= 150000 |
| 151 |
|
| 152 |
@cached_property |
| 153 |
def is_postgresql_16(self): |
| 154 |
return self.connection.pg_version >= 160000 |
| 155 |
|
| 156 |
@cached_property |
| 157 |
def is_postgresql_17(self): |
| 158 |
return self.connection.pg_version >= 170000 |
| 159 |
|
| 160 |
supports_unlimited_charfield = True |
| 161 |
supports_nulls_distinct_unique_constraints = property( |
| 162 |
operator.attrgetter("is_postgresql_15") |
| 163 |
) |