From 98cfb90182a8baa806fc4e09e294b6cfc5d09eff Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Sun, 9 Jul 2023 12:47:42 -0400 Subject: [PATCH] Reduced unique constraint eligibility code duplication. --- django/db/backends/base/schema.py | 57 ++++++++++++++++++------------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/django/db/backends/base/schema.py b/django/db/backends/base/schema.py index 2742748049d..9329ee09710 100644 --- a/django/db/backends/base/schema.py +++ b/django/db/backends/base/schema.py @@ -1675,6 +1675,25 @@ class BaseDatabaseSchemaEditor: if deferrable == Deferrable.IMMEDIATE: return " DEFERRABLE INITIALLY IMMEDIATE" + def _unique_supported( + self, + condition=None, + deferrable=None, + include=None, + expressions=None, + ): + return ( + (not condition or self.connection.features.supports_partial_indexes) + and ( + not deferrable + or self.connection.features.supports_deferrable_unique_constraints + ) + and (not include or self.connection.features.supports_covering_indexes) + and ( + not expressions or self.connection.features.supports_expression_indexes + ) + ) + def _unique_sql( self, model, @@ -1686,9 +1705,11 @@ class BaseDatabaseSchemaEditor: opclasses=None, expressions=None, ): - if ( - deferrable - and not self.connection.features.supports_deferrable_unique_constraints + if not self._unique_supported( + condition=condition, + deferrable=deferrable, + include=include, + expressions=expressions, ): return None if condition or include or opclasses or expressions: @@ -1726,16 +1747,11 @@ class BaseDatabaseSchemaEditor: opclasses=None, expressions=None, ): - if ( - ( - deferrable - and not self.connection.features.supports_deferrable_unique_constraints - ) - or (condition and not self.connection.features.supports_partial_indexes) - or (include and not self.connection.features.supports_covering_indexes) - or ( - expressions and not self.connection.features.supports_expression_indexes - ) + if not self._unique_supported( + condition=condition, + deferrable=deferrable, + include=include, + expressions=expressions, ): return None @@ -1789,16 +1805,11 @@ class BaseDatabaseSchemaEditor: opclasses=None, expressions=None, ): - if ( - ( - deferrable - and not self.connection.features.supports_deferrable_unique_constraints - ) - or (condition and not self.connection.features.supports_partial_indexes) - or (include and not self.connection.features.supports_covering_indexes) - or ( - expressions and not self.connection.features.supports_expression_indexes - ) + if not self._unique_supported( + condition=condition, + deferrable=deferrable, + include=include, + expressions=expressions, ): return None if condition or include or opclasses or expressions: