Commit 67dcea71 authored by Andrew Godwin's avatar Andrew Godwin
Browse files

Add unique_together altering operation

parent 310cdf49
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
from .models import CreateModel, DeleteModel, AlterModelTable
from .models import CreateModel, DeleteModel, AlterModelTable, AlterUniqueTogether
from .fields import AddField, RemoveField, AlterField, RenameField
+4 −4
Original line number Diff line number Diff line
@@ -7,7 +7,7 @@ class AddField(Operation):
    """

    def __init__(self, model_name, name, field):
        self.model_name = model_name
        self.model_name = model_name.lower()
        self.name = name
        self.field = field

@@ -33,7 +33,7 @@ class RemoveField(Operation):
    """

    def __init__(self, model_name, name):
        self.model_name = model_name
        self.model_name = model_name.lower()
        self.name = name

    def state_forwards(self, app_label, state):
@@ -62,7 +62,7 @@ class AlterField(Operation):
    """

    def __init__(self, model_name, name, field):
        self.model_name = model_name
        self.model_name = model_name.lower()
        self.name = name
        self.field = field

@@ -93,7 +93,7 @@ class RenameField(Operation):
    """

    def __init__(self, model_name, old_name, new_name):
        self.model_name = model_name
        self.model_name = model_name.lower()
        self.old_name = old_name
        self.new_name = new_name

+33 −3
Original line number Diff line number Diff line
@@ -9,7 +9,7 @@ class CreateModel(Operation):
    """

    def __init__(self, name, fields, options=None, bases=None):
        self.name = name
        self.name = name.lower()
        self.fields = fields
        self.options = options or {}
        self.bases = bases or (models.Model,)
@@ -35,7 +35,7 @@ class DeleteModel(Operation):
    """

    def __init__(self, name):
        self.name = name
        self.name = name.lower()

    def state_forwards(self, app_label, state):
        del state.models[app_label, self.name.lower()]
@@ -58,7 +58,7 @@ class AlterModelTable(Operation):
    """

    def __init__(self, name, table):
        self.name = name
        self.name = name.lower()
        self.table = table

    def state_forwards(self, app_label, state):
@@ -78,3 +78,33 @@ class AlterModelTable(Operation):

    def describe(self):
        return "Rename table for %s to %s" % (self.name, self.table)


class AlterUniqueTogether(Operation):
    """
    Changes the value of unique_together to the target one.
    Input value of unique_together must be a set of tuples.
    """

    def __init__(self, name, unique_together):
        self.name = name.lower()
        self.unique_together = set(tuple(cons) for cons in unique_together)

    def state_forwards(self, app_label, state):
        model_state = state.models[app_label, self.name.lower()]
        model_state.options["unique_together"] = self.unique_together

    def database_forwards(self, app_label, schema_editor, from_state, to_state):
        old_app_cache = from_state.render()
        new_app_cache = to_state.render()
        schema_editor.alter_unique_together(
            new_app_cache.get_model(app_label, self.name),
            getattr(old_app_cache.get_model(app_label, self.name)._meta, "unique_together", set()),
            getattr(new_app_cache.get_model(app_label, self.name)._meta, "unique_together", set()),
        )

    def database_backwards(self, app_label, schema_editor, from_state, to_state):
        return self.database_forwards(app_label, schema_editor, from_state, to_state)

    def describe(self):
        return "Alter unique_together for %s (%s constraints)" % (self.name, len(self.unique_together))
+7 −2
Original line number Diff line number Diff line
@@ -80,7 +80,10 @@ class ModelState(object):
            # Ignore some special options
            if name in ["app_cache", "app_label"]:
                continue
            if name in model._meta.original_attrs:
            elif name in model._meta.original_attrs:
                if name == "unique_together":
                    options[name] = set(model._meta.original_attrs["unique_together"])
                else:
                    options[name] = model._meta.original_attrs[name]
        # Make our record
        bases = tuple(model for model in model.__bases__ if (not hasattr(model, "_meta") or not model._meta.abstract))
@@ -116,6 +119,8 @@ class ModelState(object):
        # First, make a Meta object
        meta_contents = {'app_label': self.app_label, "app_cache": app_cache}
        meta_contents.update(self.options)
        if "unique_together" in meta_contents:
            meta_contents["unique_together"] = list(meta_contents["unique_together"])
        meta = type("Meta", tuple(), meta_contents)
        # Then, work out our bases
        # TODO: Use the actual bases
+2 −2
Original line number Diff line number Diff line
@@ -83,7 +83,7 @@ class AutodetectorTests(TestCase):
        # Right action?
        action = migration.operations[0]
        self.assertEqual(action.__class__.__name__, "CreateModel")
        self.assertEqual(action.name, "Author")
        self.assertEqual(action.name, "author")

    def test_old_model(self):
        "Tests deletion of old models"
@@ -100,7 +100,7 @@ class AutodetectorTests(TestCase):
        # Right action?
        action = migration.operations[0]
        self.assertEqual(action.__class__.__name__, "DeleteModel")
        self.assertEqual(action.name, "Author")
        self.assertEqual(action.name, "author")

    def test_add_field(self):
        "Tests autodetection of new fields"
Loading