Skip to content

Commit

Permalink
add/remove field complete
Browse files Browse the repository at this point in the history
  • Loading branch information
timgraham committed Dec 30, 2024
1 parent 3d4d020 commit 4602191
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 17 deletions.
6 changes: 2 additions & 4 deletions django_mongodb/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,10 @@ def _remove_model_indexes(self, model, column_prefix="", parent_model=None):
)
# Meta.constraints
for constraint in model._meta.constraints:
self.remove_constraint(
model, constraint, column_prefix=column_prefix, parent_model=parent_model
)
self.remove_constraint(parent_model or model, constraint)
# Meta.indexes
for index in model._meta.indexes:
self.remove_index(model, index, column_prefix=column_prefix, parent_model=parent_model)
self.remove_index(parent_model or model, index)

def alter_index_together(self, model, old_index_together, new_index_together, column_prefix=""):
olds = {tuple(fields) for fields in old_index_together}
Expand Down
109 changes: 96 additions & 13 deletions tests/schema_/test_embedded_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ class Meta:
new_field = EmbeddedModelField(Author)
new_field.set_attributes_from_name("author")
with connection.schema_editor() as editor:
# Create the table amd add the field.
# Create the table and add the field.
editor.create_model(Book)
editor.add_field(Book, new_field)
# Embedded uniques are created.
Expand Down Expand Up @@ -582,40 +582,123 @@ class Meta:
self.assertTableNotExists(Book)

@isolate_apps("schema_")
def _test_add_field_db_index(self):
"""AddField + EmbeddedModelField"""
def test_add_remove_field_indexes(self):
"""AddField/RemoveField + EmbeddedModelField + Meta.indexes."""

class Address(models.Model):
indexed_one = models.CharField(max_length=10)

class Meta:
app_label = "schema_"
indexes = [models.Index(fields=["indexed_one"])]

class Author(models.Model):
address = EmbeddedModelField(Address)
indexed_two = models.CharField(max_length=10)

class Meta:
app_label = "schema_"
indexes = [models.Index(fields=["indexed_two"])]

class Book(models.Model):
name = models.CharField(max_length=100)
author = EmbeddedModelField(Author)

class Meta:
app_label = "schema_"

new_field = EmbeddedModelField(Author)
new_field.set_attributes_from_name("author")

with connection.schema_editor() as editor:
# Create the table amd add the field.
# Create the table and add the field.
editor.create_model(Book)
editor.add_field(Book, new_field)
# Embedded indexes are created.
self.assertEqual(
self.get_constraints_for_columns(Book, ["author.age"]),
["schema__book_author.age_dc08100b"],
self.get_constraints_for_columns(Book, ["author.indexed_two"]),
["schema__aut_indexed_b19137_idx"],
)
self.assertEqual(
self.get_constraints_for_columns(Book, ["author.address.zip_code"]),
["schema__book_author.address.zip_code_7b9a9307"],
self.get_constraints_for_columns(
Book,
["author.address.indexed_one"],
),
["schema__add_indexed_b64972_idx"],
)
editor.remove_field(Book, new_field)
# Embedded indexes are removed.
self.assertEqual(
self.get_constraints_for_columns(Book, ["author.age"]),
self.get_constraints_for_columns(Book, ["author.indexed_two"]),
[],
)
self.assertEqual(
self.get_constraints_for_columns(Book, ["author.address.zip_code"]),
self.get_constraints_for_columns(
Book,
["author.address.indexed_one"],
),
[],
)
editor.delete_model(Book)
editor.delete_model(Author)
self.assertTableNotExists(Author)

@isolate_apps("schema_")
def test_add_remove_field_constraints(self):
"""AddField/RemoveField + EmbeddedModelField + Meta.constraints."""

class Address(models.Model):
unique_constraint_one = models.CharField(max_length=10)

class Meta:
app_label = "schema_"
constraints = [
models.UniqueConstraint(fields=["unique_constraint_one"], name="unique_one")
]

class Author(models.Model):
address = EmbeddedModelField(Address)
unique_constraint_two = models.CharField(max_length=10)

class Meta:
app_label = "schema_"
constraints = [
models.UniqueConstraint(fields=["unique_constraint_two"], name="unique_two")
]

class Book(models.Model):
author = EmbeddedModelField(Author)

class Meta:
app_label = "schema_"

new_field = EmbeddedModelField(Author)
new_field.set_attributes_from_name("author")
with connection.schema_editor() as editor:
# Create the table and add the field.
editor.create_model(Book)
editor.add_field(Book, new_field)
# Embedded constraints are created.
self.assertEqual(
self.get_constraints_for_columns(Book, ["author.unique_constraint_two"]),
["unique_two"],
)
self.assertEqual(
self.get_constraints_for_columns(
Book,
["author.address.unique_constraint_one"],
),
["unique_one"],
)
editor.remove_field(Book, new_field)
# Embedded constraints are removed.
self.assertEqual(
self.get_constraints_for_columns(Book, ["author.unique_constraint_two"]),
[],
)
self.assertEqual(
self.get_constraints_for_columns(
Book,
["author.address.unique_constraint_one"],
),
[],
)
editor.delete_model(Author)
self.assertTableNotExists(Author)

0 comments on commit 4602191

Please sign in to comment.