2018-08-27 10:25:06 +08:00
|
|
|
from enum import Enum
|
|
|
|
|
2018-12-28 03:21:59 +08:00
|
|
|
from django.db.models.query_utils import Q
|
2016-11-05 21:12:12 +08:00
|
|
|
from django.db.models.sql.query import Query
|
|
|
|
|
2018-08-27 10:25:06 +08:00
|
|
|
__all__ = ['CheckConstraint', 'Deferrable', 'UniqueConstraint']
|
2016-11-05 21:12:12 +08:00
|
|
|
|
|
|
|
|
2018-08-06 10:12:27 +08:00
|
|
|
class BaseConstraint:
|
|
|
|
def __init__(self, name):
|
2016-11-05 21:12:12 +08:00
|
|
|
self.name = name
|
|
|
|
|
|
|
|
def constraint_sql(self, model, schema_editor):
|
2018-08-06 10:12:27 +08:00
|
|
|
raise NotImplementedError('This method must be implemented by a subclass.')
|
2016-11-05 21:12:12 +08:00
|
|
|
|
|
|
|
def create_sql(self, model, schema_editor):
|
2019-01-01 22:39:58 +08:00
|
|
|
raise NotImplementedError('This method must be implemented by a subclass.')
|
2016-11-05 21:12:12 +08:00
|
|
|
|
|
|
|
def remove_sql(self, model, schema_editor):
|
2019-01-01 22:39:58 +08:00
|
|
|
raise NotImplementedError('This method must be implemented by a subclass.')
|
2016-11-05 21:12:12 +08:00
|
|
|
|
2018-08-06 10:12:27 +08:00
|
|
|
def deconstruct(self):
|
|
|
|
path = '%s.%s' % (self.__class__.__module__, self.__class__.__name__)
|
|
|
|
path = path.replace('django.db.models.constraints', 'django.db.models')
|
|
|
|
return (path, (), {'name': self.name})
|
|
|
|
|
|
|
|
def clone(self):
|
|
|
|
_, args, kwargs = self.deconstruct()
|
|
|
|
return self.__class__(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
class CheckConstraint(BaseConstraint):
|
|
|
|
def __init__(self, *, check, name):
|
|
|
|
self.check = check
|
2019-11-15 13:08:36 +08:00
|
|
|
if not getattr(check, 'conditional', False):
|
|
|
|
raise TypeError(
|
|
|
|
'CheckConstraint.check must be a Q instance or boolean '
|
|
|
|
'expression.'
|
|
|
|
)
|
2018-08-06 10:12:27 +08:00
|
|
|
super().__init__(name)
|
|
|
|
|
2019-01-01 22:39:58 +08:00
|
|
|
def _get_check_sql(self, model, schema_editor):
|
2019-09-10 11:58:29 +08:00
|
|
|
query = Query(model=model, alias_cols=False)
|
2018-08-06 10:12:27 +08:00
|
|
|
where = query.build_where(self.check)
|
2019-01-01 22:39:58 +08:00
|
|
|
compiler = query.get_compiler(connection=schema_editor.connection)
|
|
|
|
sql, params = where.as_sql(compiler, schema_editor.connection)
|
|
|
|
return sql % tuple(schema_editor.quote_value(p) for p in params)
|
|
|
|
|
|
|
|
def constraint_sql(self, model, schema_editor):
|
|
|
|
check = self._get_check_sql(model, schema_editor)
|
|
|
|
return schema_editor._check_sql(self.name, check)
|
|
|
|
|
|
|
|
def create_sql(self, model, schema_editor):
|
|
|
|
check = self._get_check_sql(model, schema_editor)
|
|
|
|
return schema_editor._create_check_sql(model, self.name, check)
|
|
|
|
|
|
|
|
def remove_sql(self, model, schema_editor):
|
|
|
|
return schema_editor._delete_check_sql(model, self.name)
|
2018-08-06 10:12:27 +08:00
|
|
|
|
2016-11-05 21:12:12 +08:00
|
|
|
def __repr__(self):
|
2018-08-06 10:15:10 +08:00
|
|
|
return "<%s: check='%s' name=%r>" % (self.__class__.__name__, self.check, self.name)
|
2016-11-05 21:12:12 +08:00
|
|
|
|
|
|
|
def __eq__(self, other):
|
2019-09-03 10:09:31 +08:00
|
|
|
if isinstance(other, CheckConstraint):
|
|
|
|
return self.name == other.name and self.check == other.check
|
|
|
|
return super().__eq__(other)
|
2016-11-05 21:12:12 +08:00
|
|
|
|
|
|
|
def deconstruct(self):
|
2018-08-06 10:12:27 +08:00
|
|
|
path, args, kwargs = super().deconstruct()
|
|
|
|
kwargs['check'] = self.check
|
|
|
|
return path, args, kwargs
|
2018-08-06 10:30:44 +08:00
|
|
|
|
|
|
|
|
2018-08-27 10:25:06 +08:00
|
|
|
class Deferrable(Enum):
|
|
|
|
DEFERRED = 'deferred'
|
|
|
|
IMMEDIATE = 'immediate'
|
|
|
|
|
|
|
|
|
2018-08-06 10:30:44 +08:00
|
|
|
class UniqueConstraint(BaseConstraint):
|
2018-08-27 10:25:06 +08:00
|
|
|
def __init__(self, *, fields, name, condition=None, deferrable=None):
|
2018-08-06 10:30:44 +08:00
|
|
|
if not fields:
|
|
|
|
raise ValueError('At least one field is required to define a unique constraint.')
|
2018-12-28 03:21:59 +08:00
|
|
|
if not isinstance(condition, (type(None), Q)):
|
|
|
|
raise ValueError('UniqueConstraint.condition must be a Q instance.')
|
2018-08-27 10:25:06 +08:00
|
|
|
if condition and deferrable:
|
|
|
|
raise ValueError(
|
|
|
|
'UniqueConstraint with conditions cannot be deferred.'
|
|
|
|
)
|
|
|
|
if not isinstance(deferrable, (type(None), Deferrable)):
|
|
|
|
raise ValueError(
|
|
|
|
'UniqueConstraint.deferrable must be a Deferrable instance.'
|
|
|
|
)
|
2018-08-06 10:30:44 +08:00
|
|
|
self.fields = tuple(fields)
|
2018-12-28 03:21:59 +08:00
|
|
|
self.condition = condition
|
2018-08-27 10:25:06 +08:00
|
|
|
self.deferrable = deferrable
|
2018-08-06 10:30:44 +08:00
|
|
|
super().__init__(name)
|
|
|
|
|
2018-12-28 03:21:59 +08:00
|
|
|
def _get_condition_sql(self, model, schema_editor):
|
|
|
|
if self.condition is None:
|
|
|
|
return None
|
2019-09-10 11:58:29 +08:00
|
|
|
query = Query(model=model, alias_cols=False)
|
2018-12-28 03:21:59 +08:00
|
|
|
where = query.build_where(self.condition)
|
|
|
|
compiler = query.get_compiler(connection=schema_editor.connection)
|
|
|
|
sql, params = where.as_sql(compiler, schema_editor.connection)
|
|
|
|
return sql % tuple(schema_editor.quote_value(p) for p in params)
|
|
|
|
|
2018-08-06 10:30:44 +08:00
|
|
|
def constraint_sql(self, model, schema_editor):
|
2019-01-01 22:39:58 +08:00
|
|
|
fields = [model._meta.get_field(field_name).column for field_name in self.fields]
|
2018-12-28 03:21:59 +08:00
|
|
|
condition = self._get_condition_sql(model, schema_editor)
|
2018-08-27 10:25:06 +08:00
|
|
|
return schema_editor._unique_sql(
|
|
|
|
model, fields, self.name, condition=condition,
|
|
|
|
deferrable=self.deferrable,
|
|
|
|
)
|
2018-08-06 10:30:44 +08:00
|
|
|
|
|
|
|
def create_sql(self, model, schema_editor):
|
2019-01-01 22:39:58 +08:00
|
|
|
fields = [model._meta.get_field(field_name).column for field_name in self.fields]
|
2018-12-28 03:21:59 +08:00
|
|
|
condition = self._get_condition_sql(model, schema_editor)
|
2018-08-27 10:25:06 +08:00
|
|
|
return schema_editor._create_unique_sql(
|
|
|
|
model, fields, self.name, condition=condition,
|
|
|
|
deferrable=self.deferrable,
|
|
|
|
)
|
2019-01-01 22:39:58 +08:00
|
|
|
|
|
|
|
def remove_sql(self, model, schema_editor):
|
2018-12-28 03:21:59 +08:00
|
|
|
condition = self._get_condition_sql(model, schema_editor)
|
2018-08-27 10:25:06 +08:00
|
|
|
return schema_editor._delete_unique_sql(
|
|
|
|
model, self.name, condition=condition, deferrable=self.deferrable,
|
|
|
|
)
|
2018-08-06 10:30:44 +08:00
|
|
|
|
|
|
|
def __repr__(self):
|
2018-08-27 10:25:06 +08:00
|
|
|
return '<%s: fields=%r name=%r%s%s>' % (
|
2018-12-28 03:21:59 +08:00
|
|
|
self.__class__.__name__, self.fields, self.name,
|
|
|
|
'' if self.condition is None else ' condition=%s' % self.condition,
|
2018-08-27 10:25:06 +08:00
|
|
|
'' if self.deferrable is None else ' deferrable=%s' % self.deferrable,
|
2018-12-28 03:21:59 +08:00
|
|
|
)
|
2018-08-06 10:30:44 +08:00
|
|
|
|
|
|
|
def __eq__(self, other):
|
2019-09-03 10:09:31 +08:00
|
|
|
if isinstance(other, UniqueConstraint):
|
|
|
|
return (
|
|
|
|
self.name == other.name and
|
|
|
|
self.fields == other.fields and
|
2018-08-27 10:25:06 +08:00
|
|
|
self.condition == other.condition and
|
|
|
|
self.deferrable == other.deferrable
|
2019-09-03 10:09:31 +08:00
|
|
|
)
|
|
|
|
return super().__eq__(other)
|
2018-08-06 10:30:44 +08:00
|
|
|
|
|
|
|
def deconstruct(self):
|
|
|
|
path, args, kwargs = super().deconstruct()
|
|
|
|
kwargs['fields'] = self.fields
|
2018-12-28 03:21:59 +08:00
|
|
|
if self.condition:
|
|
|
|
kwargs['condition'] = self.condition
|
2018-08-27 10:25:06 +08:00
|
|
|
if self.deferrable:
|
|
|
|
kwargs['deferrable'] = self.deferrable
|
2018-08-06 10:30:44 +08:00
|
|
|
return path, args, kwargs
|