Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
.pyc
6 changes: 3 additions & 3 deletions django_db_constraints/autodetector.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class MigrationAutodetectorWithDbConstraints(MigrationAutodetector):
db_constraints_operations = []

def generate_created_models(self, *args, **kwargs):
rv = super().generate_created_models(*args, **kwargs)
rv = super(MigrationAutodetectorWithDbConstraints, self).generate_created_models(*args, **kwargs)
for (app_label, migration_operations) in self.generated_operations.items():
for operation in migration_operations:
if isinstance(operation, operations.CreateModel) and 'db_constraints' in operation.options:
Expand All @@ -20,7 +20,7 @@ def generate_created_models(self, *args, **kwargs):
return rv

def generate_altered_unique_together(self, *args, **kwargs):
rv = super().generate_altered_unique_together(*args, **kwargs)
rv = super(MigrationAutodetectorWithDbConstraints, self).generate_altered_unique_together(*args, **kwargs)

for app_label, model_name in sorted(self.kept_model_keys):
old_model_name = self.renamed_models.get((app_label, model_name), model_name)
Expand All @@ -41,7 +41,7 @@ def generate_altered_unique_together(self, *args, **kwargs):
return rv

def _sort_migrations(self, *args, **kwargs):
rv = super()._sort_migrations()
rv = super(MigrationAutodetectorWithDbConstraints, self)._sort_migrations()
for app_label, operation in self.db_constraints_operations:
self.generated_operations.setdefault(app_label, []).append(operation)
return rv
31 changes: 17 additions & 14 deletions django_db_constraints/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class AlterConstraints(ModelOptionOperation):

def __init__(self, name, db_constraints):
self.db_constraints = db_constraints
super().__init__(name)
super(AlterConstraints, self).__init__(name)

def state_forwards(self, app_label, state):
model_state = state.models[app_label, self.name_lower]
Expand All @@ -24,8 +24,8 @@ def database_forwards(self, app_label, schema_editor, from_state, to_state):
if self.allow_migrate_model(schema_editor.connection.alias, to_model):
from_model = from_state.apps.get_model(app_label, self.name)

to_constraints = getattr(to_model._meta, self.option_name, {}).keys()
from_constraints = getattr(from_model._meta, self.option_name, {}).keys()
to_constraints = set(getattr(to_model._meta, self.option_name, {}).keys())
from_constraints = set(getattr(from_model._meta, self.option_name, {}).keys())

table_operations = tuple(
'DROP CONSTRAINT IF EXISTS {name}'.format(
Expand All @@ -38,19 +38,22 @@ def database_forwards(self, app_label, schema_editor, from_state, to_state):
constraint=to_model._meta.db_constraints[constraint_name],
)
for constraint_name in to_constraints - from_constraints
) + tuple(
'DROP CONSTRAINT IF EXISTS {name}, ADD CONSTRAINT {name} {constraint}'.format(
name=schema_editor.connection.ops.quote_name(constraint_name),
constraint=to_model._meta.db_constraints[constraint_name],
)
for constraint_name in to_constraints & from_constraints
if to_model._meta.db_constraints[constraint_name] != from_model._meta.db_constraints[constraint_name]
)

if table_operations:
schema_editor.execute('ALTER TABLE {table} {table_operations}'.format(
for constraint_name in to_constraints & from_constraints:
if to_model._meta.db_constraints[constraint_name] != from_model._meta.db_constraints[constraint_name]:
table_operations += (
'DROP CONSTRAINT IF EXISTS {name}'.format(
name=schema_editor.connection.ops.quote_name(constraint_name)
),
'ADD CONSTRAINT {name} {constraint}'.format(
name=schema_editor.connection.ops.quote_name(constraint_name),
constraint=to_model._meta.db_constraints[constraint_name],
)
)
for table_operation in table_operations:
schema_editor.execute('ALTER TABLE {table} {table_operation}'.format(
table=schema_editor.connection.ops.quote_name(to_model._meta.db_table),
table_operations=', '.join(table_operations),
table_operation=table_operation,
))

def database_backwards(self, app_label, schema_editor, from_state, to_state):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def readme():

setup(
name='django-db-constraints',
version='0.3.0',
version='0.4.0',
author='shangxiao',
description='Add database table-level constraints to your Django model\'s Meta',
long_description=readme(),
Expand Down