测试gitnore

This commit is contained in:
ladeng07
2022-05-06 15:45:57 +08:00
parent 12f390949b
commit 51552904f9
2347 changed files with 120102 additions and 53549 deletions
@@ -2,66 +2,64 @@ from django.db import NotSupportedError
from django.db.backends.ddl_references import Statement, Table
from django.db.models import Deferrable, F, Q
from django.db.models.constraints import BaseConstraint
from django.db.models.expressions import Col
from django.db.models.sql import Query
__all__ = ["ExclusionConstraint"]
__all__ = ['ExclusionConstraint']
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.")
if include and index_type and index_type.lower() != "gist":
raise ValueError(
"Covering exclusion constraints only support GiST indexes."
'ExclusionConstraint.include must be a list or tuple.'
)
if include and index_type and index_type.lower() != 'gist':
raise ValueError(
'Covering exclusion constraints only support GiST indexes.'
)
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 ()
@@ -75,16 +73,14 @@ class ExclusionConstraint(BaseConstraint):
expression = F(expression)
expression = expression.resolve_expression(query=query)
sql, params = compiler.compile(expression)
if not isinstance(expression, Col):
sql = f"({sql})"
try:
opclass = self.opclasses[idx]
if opclass:
sql = "%s %s" % (sql, opclass)
sql = '%s %s' % (sql, opclass)
except IndexError:
pass
sql = sql % tuple(schema_editor.quote_value(p) for p in params)
expressions.append("%s WITH %s" % (sql, operator))
expressions.append('%s WITH %s' % (sql, operator))
return expressions
def _get_condition_sql(self, compiler, schema_editor, query):
@@ -99,22 +95,20 @@ class ExclusionConstraint(BaseConstraint):
compiler = query.get_compiler(connection=schema_editor.connection)
expressions = self._get_expression_sql(compiler, schema_editor, query)
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 self.template % {
"name": schema_editor.quote_name(self.name),
"index_type": self.index_type,
"expressions": ", ".join(expressions),
"include": schema_editor._index_include_sql(model, include),
"where": " WHERE (%s)" % condition if condition else "",
"deferrable": schema_editor._deferrable_constraint_sql(self.deferrable),
'name': schema_editor.quote_name(self.name),
'index_type': self.index_type,
'expressions': ', '.join(expressions),
'include': schema_editor._index_include_sql(model, include),
'where': ' WHERE (%s)' % condition if condition else '',
'deferrable': schema_editor._deferrable_constraint_sql(self.deferrable),
}
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),
)
@@ -127,50 +121,46 @@ class ExclusionConstraint(BaseConstraint):
)
def check_supported(self, schema_editor):
if (
self.include
and not schema_editor.connection.features.supports_covering_gist_indexes
):
if self.include and not schema_editor.connection.features.supports_covering_gist_indexes:
raise NotSupportedError(
"Covering exclusion constraints requires PostgreSQL 12+."
'Covering exclusion constraints requires PostgreSQL 12+.'
)
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%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),
self.index_type,
self.expressions,
'' if self.condition is None else ', condition=%s' % self.condition,
'' if self.deferrable is None else ', deferrable=%s' % self.deferrable,
'' if not self.include else ', include=%s' % repr(self.include),
'' if not self.opclasses else ', opclasses=%s' % repr(self.opclasses),
)