diff options
author | django-bot <ops@djangoproject.com> | 2022-02-03 20:24:19 +0100 |
---|---|---|
committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2022-02-07 20:37:05 +0100 |
commit | 9c19aff7c7561e3a82978a272ecdaad40dda5c00 (patch) | |
tree | f0506b668a013d0063e5fba3dbf4863b466713ba /django/contrib/postgres/constraints.py | |
parent | f68fa8b45dfac545cfc4111d4e52804c86db68d3 (diff) | |
download | django-9c19aff7c7561e3a82978a272ecdaad40dda5c00.tar.gz |
Refs #33476 -- Reformatted code with Black.
Diffstat (limited to 'django/contrib/postgres/constraints.py')
-rw-r--r-- | django/contrib/postgres/constraints.py | 132 |
1 files changed, 67 insertions, 65 deletions
diff --git a/django/contrib/postgres/constraints.py b/django/contrib/postgres/constraints.py index 06ccd3616e..c0bc0c444f 100644 --- a/django/contrib/postgres/constraints.py +++ b/django/contrib/postgres/constraints.py @@ -10,71 +10,69 @@ from django.db.models.indexes import IndexExpression from django.db.models.sql import Query from django.utils.deprecation import RemovedInDjango50Warning -__all__ = ['ExclusionConstraint'] +__all__ = ["ExclusionConstraint"] class ExclusionConstraintExpression(IndexExpression): - template = '%(expressions)s WITH %(operator)s' + template = "%(expressions)s WITH %(operator)s" class ExclusionConstraint(BaseConstraint): - template = 'CONSTRAINT %(name)s EXCLUDE USING %(index_type)s (%(expressions)s)%(include)s%(where)s%(deferrable)s' + template = "CONSTRAINT %(name)s EXCLUDE USING %(index_type)s (%(expressions)s)%(include)s%(where)s%(deferrable)s" def __init__( - self, *, name, expressions, index_type=None, condition=None, - deferrable=None, include=None, opclasses=(), + self, + *, + name, + expressions, + index_type=None, + condition=None, + deferrable=None, + include=None, + opclasses=(), ): - if index_type and index_type.lower() not in {'gist', 'spgist'}: + if index_type and index_type.lower() not in {"gist", "spgist"}: raise ValueError( - 'Exclusion constraints only support GiST or SP-GiST indexes.' + "Exclusion constraints only support GiST or SP-GiST indexes." ) if not expressions: raise ValueError( - 'At least one expression is required to define an exclusion ' - 'constraint.' + "At least one expression is required to define an exclusion " + "constraint." ) if not all( - isinstance(expr, (list, tuple)) and len(expr) == 2 - for expr in expressions + isinstance(expr, (list, tuple)) and len(expr) == 2 for expr in expressions ): - raise ValueError('The expressions must be a list of 2-tuples.') + raise ValueError("The expressions must be a list of 2-tuples.") if not isinstance(condition, (type(None), Q)): - raise ValueError( - 'ExclusionConstraint.condition must be a Q instance.' - ) + raise ValueError("ExclusionConstraint.condition must be a Q instance.") if condition and deferrable: - raise ValueError( - 'ExclusionConstraint with conditions cannot be deferred.' - ) + raise ValueError("ExclusionConstraint with conditions cannot be deferred.") if not isinstance(deferrable, (type(None), Deferrable)): raise ValueError( - 'ExclusionConstraint.deferrable must be a Deferrable instance.' + "ExclusionConstraint.deferrable must be a Deferrable instance." ) if not isinstance(include, (type(None), list, tuple)): - raise ValueError( - 'ExclusionConstraint.include must be a list or tuple.' - ) + raise ValueError("ExclusionConstraint.include must be a list or tuple.") if not isinstance(opclasses, (list, tuple)): - raise ValueError( - 'ExclusionConstraint.opclasses must be a list or tuple.' - ) + raise ValueError("ExclusionConstraint.opclasses must be a list or tuple.") if opclasses and len(expressions) != len(opclasses): raise ValueError( - 'ExclusionConstraint.expressions and ' - 'ExclusionConstraint.opclasses must have the same number of ' - 'elements.' + "ExclusionConstraint.expressions and " + "ExclusionConstraint.opclasses must have the same number of " + "elements." ) self.expressions = expressions - self.index_type = index_type or 'GIST' + self.index_type = index_type or "GIST" self.condition = condition self.deferrable = deferrable self.include = tuple(include) if include else () self.opclasses = opclasses if self.opclasses: warnings.warn( - 'The opclasses argument is deprecated in favor of using ' - 'django.contrib.postgres.indexes.OpClass in ' - 'ExclusionConstraint.expressions.', + "The opclasses argument is deprecated in favor of using " + "django.contrib.postgres.indexes.OpClass in " + "ExclusionConstraint.expressions.", category=RemovedInDjango50Warning, stacklevel=2, ) @@ -107,14 +105,18 @@ class ExclusionConstraint(BaseConstraint): expressions = self._get_expressions(schema_editor, query) table = model._meta.db_table condition = self._get_condition_sql(compiler, schema_editor, query) - include = [model._meta.get_field(field_name).column for field_name in self.include] + include = [ + model._meta.get_field(field_name).column for field_name in self.include + ] return Statement( self.template, table=Table(table, schema_editor.quote_name), name=schema_editor.quote_name(self.name), index_type=self.index_type, - expressions=Expressions(table, expressions, compiler, schema_editor.quote_value), - where=' WHERE (%s)' % condition if condition else '', + expressions=Expressions( + table, expressions, compiler, schema_editor.quote_value + ), + where=" WHERE (%s)" % condition if condition else "", include=schema_editor._index_include_sql(model, include), deferrable=schema_editor._deferrable_constraint_sql(self.deferrable), ) @@ -122,7 +124,7 @@ class ExclusionConstraint(BaseConstraint): def create_sql(self, model, schema_editor): self.check_supported(schema_editor) return Statement( - 'ALTER TABLE %(table)s ADD %(constraint)s', + "ALTER TABLE %(table)s ADD %(constraint)s", table=Table(model._meta.db_table, schema_editor.quote_name), constraint=self.constraint_sql(model, schema_editor), ) @@ -136,60 +138,60 @@ class ExclusionConstraint(BaseConstraint): def check_supported(self, schema_editor): if ( - self.include and - self.index_type.lower() == 'gist' and - not schema_editor.connection.features.supports_covering_gist_indexes + self.include + and self.index_type.lower() == "gist" + and not schema_editor.connection.features.supports_covering_gist_indexes ): raise NotSupportedError( - 'Covering exclusion constraints using a GiST index require ' - 'PostgreSQL 12+.' + "Covering exclusion constraints using a GiST index require " + "PostgreSQL 12+." ) if ( - self.include and - self.index_type.lower() == 'spgist' and - not schema_editor.connection.features.supports_covering_spgist_indexes + self.include + and self.index_type.lower() == "spgist" + and not schema_editor.connection.features.supports_covering_spgist_indexes ): raise NotSupportedError( - 'Covering exclusion constraints using an SP-GiST index ' - 'require PostgreSQL 14+.' + "Covering exclusion constraints using an SP-GiST index " + "require PostgreSQL 14+." ) def deconstruct(self): path, args, kwargs = super().deconstruct() - kwargs['expressions'] = self.expressions + kwargs["expressions"] = self.expressions if self.condition is not None: - kwargs['condition'] = self.condition - if self.index_type.lower() != 'gist': - kwargs['index_type'] = self.index_type + kwargs["condition"] = self.condition + if self.index_type.lower() != "gist": + kwargs["index_type"] = self.index_type if self.deferrable: - kwargs['deferrable'] = self.deferrable + kwargs["deferrable"] = self.deferrable if self.include: - kwargs['include'] = self.include + kwargs["include"] = self.include if self.opclasses: - kwargs['opclasses'] = self.opclasses + kwargs["opclasses"] = self.opclasses return path, args, kwargs def __eq__(self, other): if isinstance(other, self.__class__): return ( - self.name == other.name and - self.index_type == other.index_type and - self.expressions == other.expressions and - self.condition == other.condition and - self.deferrable == other.deferrable and - self.include == other.include and - self.opclasses == other.opclasses + self.name == other.name + and self.index_type == other.index_type + and self.expressions == other.expressions + and self.condition == other.condition + and self.deferrable == other.deferrable + and self.include == other.include + and self.opclasses == other.opclasses ) return super().__eq__(other) def __repr__(self): - return '<%s: index_type=%s expressions=%s name=%s%s%s%s%s>' % ( + return "<%s: index_type=%s expressions=%s name=%s%s%s%s%s>" % ( self.__class__.__qualname__, repr(self.index_type), repr(self.expressions), repr(self.name), - '' if self.condition is None else ' condition=%s' % self.condition, - '' if self.deferrable is None else ' deferrable=%r' % self.deferrable, - '' if not self.include else ' include=%s' % repr(self.include), - '' if not self.opclasses else ' opclasses=%s' % repr(self.opclasses), + "" if self.condition is None else " condition=%s" % self.condition, + "" if self.deferrable is None else " deferrable=%r" % self.deferrable, + "" if not self.include else " include=%s" % repr(self.include), + "" if not self.opclasses else " opclasses=%s" % repr(self.opclasses), ) |