diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a57e37c --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea +.pyc \ No newline at end of file diff --git a/django_db_constraints/autodetector.py b/django_db_constraints/autodetector.py index a21d409..ecb1a9b 100644 --- a/django_db_constraints/autodetector.py +++ b/django_db_constraints/autodetector.py @@ -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: @@ -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) @@ -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 diff --git a/django_db_constraints/operations.py b/django_db_constraints/operations.py index 933cef1..8bd851f 100644 --- a/django_db_constraints/operations.py +++ b/django_db_constraints/operations.py @@ -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] @@ -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( @@ -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): diff --git a/setup.py b/setup.py index ff3bfb8..55f6f32 100644 --- a/setup.py +++ b/setup.py @@ -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(),