Skip to content

Commit

Permalink
Properly implement deconstruct for custom operations.
Browse files Browse the repository at this point in the history
The full import path must be specified for operations that are not part of
django.db.migrations.
  • Loading branch information
charettes committed Mar 5, 2021
1 parent 8251d05 commit f24eb71
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
12 changes: 12 additions & 0 deletions syzygy/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ def describe(self):
)
return "Set field %s of %s NULLable" % (self.name, self.model_name)

def deconstruct(self):
_, args, kwargs = super().deconstruct()
return f"{__name__}.{self.__class__.__name__}", args, kwargs


class AddField(migrations.AddField):
"""
Expand Down Expand Up @@ -168,6 +172,10 @@ def database_forwards(self, app_label, schema_editor, from_state, to_state):
app_label, schema_editor, from_state, to_state
)

def deconstruct(self):
_, args, kwargs = super().deconstruct()
return f"{__name__}.{self.__class__.__name__}", args, kwargs


class PostAddField(migrations.AlterField):
"""
Expand Down Expand Up @@ -219,3 +227,7 @@ def describe(self):
self.name,
self.model_name,
)

def deconstruct(self):
_, args, kwargs = super().deconstruct()
return f"{__name__}.{self.__class__.__name__}", args, kwargs
47 changes: 47 additions & 0 deletions tests/test_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ def test_database_forwards(self, preserve_default=True):
def test_database_forwards_discard_default(self):
self.test_database_forwards(preserve_default=False)

def test_deconstruct(self):
model_name = "TestModel"
field_name = "foo"
field = models.IntegerField(default=42)
operation = AddField(model_name, field_name, field)
self.assertEqual(
operation.deconstruct(),
(
"syzygy.operations.AddField",
[],
{"model_name": model_name, "name": field_name, "field": field},
),
)


class PostAddFieldTests(OperationTestCase):
def test_database_forwards(
Expand Down Expand Up @@ -182,6 +196,20 @@ def test_describe(self):
"Drop database DEFAULT of field foo on TestModel",
)

def test_deconstruct(self):
model_name = "TestModel"
field_name = "foo"
field = models.IntegerField(default=42)
operation = PostAddField(model_name, field_name, field)
self.assertEqual(
operation.deconstruct(),
(
"syzygy.operations.PostAddField",
[],
{"model_name": model_name, "name": field_name, "field": field},
),
)


class PreRemoveFieldTests(OperationTestCase):
def test_database_forwards_null(self):
Expand Down Expand Up @@ -262,3 +290,22 @@ def test_describe(self):
PreRemoveField("TestModel", "foo", models.IntegerField()).describe(),
"Set field foo of TestModel NULLable",
)

def test_deconstruct(self):
model_name = "TestModel"
field_name = "foo"
field = models.IntegerField(default=42)
operations = PreRemoveField(
model_name,
field_name,
field,
)
self.assertEqual(
operations.deconstruct(),
(
"syzygy.operations.PreRemoveField",
[],
{"model_name": model_name, "name": field_name, "field": field},
),
)

0 comments on commit f24eb71

Please sign in to comment.