Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Auto-naming for migrations and some writer fixes

  • Loading branch information...
commit 91c470def50c4de420b0c6ee7debddc5bbd53ec8 1 parent cd80961
Andrew Godwin authored June 07, 2013
91  django/db/migrations/autodetector.py
... ...
@@ -1,3 +1,4 @@
  1
+import re
1 2
 from django.db.migrations import operations
2 3
 from django.db.migrations.migration import Migration
3 4
 
@@ -11,7 +12,7 @@ class MigrationAutodetector(object):
11 12
     Note that this naturally operates on entire projects at a time,
12 13
     as it's likely that changes interact (for example, you can't
13 14
     add a ForeignKey without having a migration to add the table it
14  
-    depends on first). A user interface may offer single-app detection
  15
+    depends on first). A user interface may offer single-app usage
15 16
     if it wishes, with the caveat that it may not always be possible.
16 17
     """
17 18
 
@@ -21,8 +22,12 @@ def __init__(self, from_state, to_state):
21 22
 
22 23
     def changes(self):
23 24
         """
24  
-        Returns a set of migration plans which will achieve the
25  
-        change from from_state to to_state.
  25
+        Returns a dict of migration plans which will achieve the
  26
+        change from from_state to to_state. The dict has app labels
  27
+        as kays and a list of migrations as values.
  28
+
  29
+        The resulting migrations aren't specially named, but the names
  30
+        do matter for dependencies inside the set.
26 31
         """
27 32
         # We'll store migrations as lists by app names for now
28 33
         self.migrations = {}
@@ -53,17 +58,77 @@ def changes(self):
53 58
         for app_label, migrations in self.migrations.items():
54 59
             for m1, m2 in zip(migrations, migrations[1:]):
55 60
                 m2.dependencies.append((app_label, m1.name))
56  
-        # Flatten and return
57  
-        result = set()
58  
-        for app_label, migrations in self.migrations.items():
59  
-            for migration in migrations:
60  
-                subclass = type("Migration", (Migration,), migration)
61  
-                instance = subclass(migration['name'], app_label)
62  
-                result.add(instance)
63  
-        return result
  61
+        return self.migrations
64 62
 
65 63
     def add_to_migration(self, app_label, operation):
66 64
         migrations = self.migrations.setdefault(app_label, [])
67 65
         if not migrations:
68  
-            migrations.append({"name": "auto_%i" % (len(migrations) + 1), "operations": [], "dependencies": []})
69  
-        migrations[-1]['operations'].append(operation)
  66
+            subclass = type("Migration", (Migration,), {"operations": [], "dependencies": []})
  67
+            instance = subclass("auto_%i" % (len(migrations) + 1), app_label)
  68
+            migrations.append(instance)
  69
+        migrations[-1].operations.append(operation)
  70
+
  71
+    @classmethod
  72
+    def suggest_name(cls, ops):
  73
+        """
  74
+        Given a set of operations, suggests a name for the migration
  75
+        they might represent. Names not guaranteed to be unique; they
  76
+        must be prefixed by a number or date.
  77
+        """
  78
+        if len(ops) == 1:
  79
+            if isinstance(ops[0], operations.CreateModel):
  80
+                return ops[0].name.lower()
  81
+            elif isinstance(ops[0], operations.DeleteModel):
  82
+                return "delete_%s" % ops[0].name.lower()
  83
+        elif all(isinstance(o, operations.CreateModel) for o in ops):
  84
+            return "_".join(sorted(o.name.lower() for o in ops))
  85
+        return "auto"
  86
+
  87
+    @classmethod
  88
+    def parse_number(cls, name):
  89
+        """
  90
+        Given a migration name, tries to extract a number from the
  91
+        beginning of it. If no number found, returns None.
  92
+        """
  93
+        if re.match(r"^\d+_", name):
  94
+            return int(name.split("_")[0])
  95
+        return None
  96
+
  97
+    @classmethod
  98
+    def arrange_for_graph(cls, changes, graph):
  99
+        """
  100
+        Takes in a result from changes() and a MigrationGraph,
  101
+        and fixes the names and dependencies of the changes so they
  102
+        extend the graph from the leaf nodes for each app.
  103
+        """
  104
+        leaves = graph.leaf_nodes()
  105
+        name_map = {}
  106
+        for app_label, migrations in changes.items():
  107
+            if not migrations:
  108
+                continue
  109
+            # Find the app label's current leaf node
  110
+            app_leaf = None
  111
+            for leaf in leaves:
  112
+                if leaf[0] == app_label:
  113
+                    app_leaf = leaf
  114
+                    break
  115
+            # Work out the next number in the sequence
  116
+            if app_leaf is None:
  117
+                next_number = 1
  118
+            else:
  119
+                next_number = (cls.parse_number(app_leaf[1]) or 0) + 1
  120
+            # Name each migration
  121
+            for i, migration in enumerate(migrations):
  122
+                if i == 0 and app_leaf:
  123
+                    migration.dependencies.append(app_leaf)
  124
+                if i == 0 and not app_leaf:
  125
+                    new_name = "0001_initial"
  126
+                else:
  127
+                    new_name = "%04i_%s" % (next_number, cls.suggest_name(migration.operations))
  128
+                name_map[(app_label, migration.name)] = (app_label, new_name)
  129
+                migration.name = new_name
  130
+        # Now fix dependencies
  131
+        for app_label, migrations in changes.items():
  132
+            for migration in migrations:
  133
+                migration.dependencies = [name_map.get(d, d) for d in migration.dependencies]
  134
+        return changes
16  django/db/migrations/graph.py
@@ -120,14 +120,20 @@ def _dfs(start, get_children, path):
120 120
     def __str__(self):
121 121
         return "Graph: %s nodes, %s edges" % (len(self.nodes), sum(len(x) for x in self.dependencies.values()))
122 122
 
123  
-    def project_state(self, node, at_end=True):
  123
+    def project_state(self, nodes, at_end=True):
124 124
         """
125  
-        Given a migration node, returns a complete ProjectState for it.
  125
+        Given a migration node or nodes, returns a complete ProjectState for it.
126 126
         If at_end is False, returns the state before the migration has run.
127 127
         """
128  
-        plan = self.forwards_plan(node)
129  
-        if not at_end:
130  
-            plan = plan[:-1]
  128
+        if not isinstance(nodes[0], tuple):
  129
+            nodes = [nodes]
  130
+        plan = []
  131
+        for node in nodes:
  132
+            for migration in self.forwards_plan(node):
  133
+                if migration not in plan:
  134
+                    if not at_end and migration in nodes:
  135
+                        continue
  136
+                    plan.append(migration)
131 137
         project_state = ProjectState()
132 138
         for node in plan:
133 139
             project_state = self.nodes[node].mutate_state(project_state)
12  django/db/migrations/writer.py
... ...
@@ -1,5 +1,7 @@
  1
+from __future__ import unicode_literals
1 2
 import datetime
2 3
 import types
  4
+from django.utils import six
3 5
 from django.db import models
4 6
 
5 7
 
@@ -36,11 +38,12 @@ def as_string(self):
36 38
             operation_strings.append("migrations.%s(%s\n        )" % (name, "".join("\n            %s," % arg for arg in arg_strings)))
37 39
         items["operations"] = "[%s\n    ]" % "".join("\n        %s," % s for s in operation_strings)
38 40
         # Format imports nicely
  41
+        imports.discard("from django.db import models")
39 42
         if not imports:
40 43
             items["imports"] = ""
41 44
         else:
42 45
             items["imports"] = "\n".join(imports) + "\n"
43  
-        return MIGRATION_TEMPLATE % items
  46
+        return (MIGRATION_TEMPLATE % items).encode("utf8")
44 47
 
45 48
     @property
46 49
     def filename(self):
@@ -84,16 +87,17 @@ def serialize(cls, value):
84 87
         elif isinstance(value, (datetime.datetime, datetime.date)):
85 88
             return repr(value), set(["import datetime"])
86 89
         # Simple types
87  
-        elif isinstance(value, (int, long, float, str, unicode, bool, types.NoneType)):
  90
+        elif isinstance(value, (int, long, float, six.binary_type, six.text_type, bool, types.NoneType)):
88 91
             return repr(value), set()
89 92
         # Django fields
90 93
         elif isinstance(value, models.Field):
91 94
             attr_name, path, args, kwargs = value.deconstruct()
92 95
             module, name = path.rsplit(".", 1)
93 96
             if module == "django.db.models":
94  
-                imports = set()
  97
+                imports = set(["from django.db import models"])
  98
+                name = "models.%s" % name
95 99
             else:
96  
-                imports = set("import %s" % module)
  100
+                imports = set(["import %s" % module])
97 101
                 name = path
98 102
             arg_strings = []
99 103
             for arg in args:
33  tests/migrations/test_autodetector.py
@@ -2,6 +2,7 @@
2 2
 from django.test import TransactionTestCase
3 3
 from django.db.migrations.autodetector import MigrationAutodetector
4 4
 from django.db.migrations.state import ProjectState, ModelState
  5
+from django.db.migrations.graph import MigrationGraph
5 6
 from django.db import models
6 7
 
7 8
 
@@ -11,6 +12,8 @@ class AutodetectorTests(TransactionTestCase):
11 12
     """
12 13
 
13 14
     author_empty = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True))])
  15
+    other_pony = ModelState("otherapp", "Pony", [("id", models.AutoField(primary_key=True))])
  16
+    other_stable = ModelState("otherapp", "Stable", [("id", models.AutoField(primary_key=True))])
14 17
 
15 18
     def make_project_state(self, model_states):
16 19
         "Shortcut to make ProjectStates from lists of predefined models"
@@ -19,6 +22,28 @@ def make_project_state(self, model_states):
19 22
             project_state.add_model_state(model_state)
20 23
         return project_state
21 24
 
  25
+    def test_arrange_for_graph(self):
  26
+        "Tests auto-naming of migrations for graph matching."
  27
+        # Make a fake graph
  28
+        graph = MigrationGraph()
  29
+        graph.add_node(("testapp", "0001_initial"), None)
  30
+        graph.add_node(("testapp", "0002_foobar"), None)
  31
+        graph.add_node(("otherapp", "0001_initial"), None)
  32
+        graph.add_dependency(("testapp", "0002_foobar"), ("testapp", "0001_initial"))
  33
+        graph.add_dependency(("testapp", "0002_foobar"), ("otherapp", "0001_initial"))
  34
+        # Use project state to make a new migration change set
  35
+        before = self.make_project_state([])
  36
+        after = self.make_project_state([self.author_empty, self.other_pony, self.other_stable])
  37
+        autodetector = MigrationAutodetector(before, after)
  38
+        changes = autodetector.changes()
  39
+        # Run through arrange_for_graph
  40
+        changes = autodetector.arrange_for_graph(changes, graph)
  41
+        # Make sure there's a new name, deps match, etc.
  42
+        self.assertEqual(changes["testapp"][0].name, "0003_author")
  43
+        self.assertEqual(changes["testapp"][0].dependencies, [("testapp", "0002_foobar")])
  44
+        self.assertEqual(changes["otherapp"][0].name, "0002_pony_stable")
  45
+        self.assertEqual(changes["otherapp"][0].dependencies, [("otherapp", "0001_initial")])
  46
+
22 47
     def test_new_model(self):
23 48
         "Tests autodetection of new models"
24 49
         # Make state
@@ -27,9 +52,9 @@ def test_new_model(self):
27 52
         autodetector = MigrationAutodetector(before, after)
28 53
         changes = autodetector.changes()
29 54
         # Right number of migrations?
30  
-        self.assertEqual(len(changes), 1)
  55
+        self.assertEqual(len(changes['testapp']), 1)
31 56
         # Right number of actions?
32  
-        migration = changes.pop()
  57
+        migration = changes['testapp'][0]
33 58
         self.assertEqual(len(migration.operations), 1)
34 59
         # Right action?
35 60
         action = migration.operations[0]
@@ -44,9 +69,9 @@ def test_old_model(self):
44 69
         autodetector = MigrationAutodetector(before, after)
45 70
         changes = autodetector.changes()
46 71
         # Right number of migrations?
47  
-        self.assertEqual(len(changes), 1)
  72
+        self.assertEqual(len(changes['testapp']), 1)
48 73
         # Right number of actions?
49  
-        migration = changes.pop()
  74
+        migration = changes['testapp'][0]
50 75
         self.assertEqual(len(migration.operations), 1)
51 76
         # Right action?
52 77
         action = migration.operations[0]
41  tests/migrations/test_writer.py
... ...
@@ -1,5 +1,6 @@
1 1
 # encoding: utf8
2 2
 import datetime
  3
+from django.utils import six
3 4
 from django.test import TransactionTestCase
4 5
 from django.db.migrations.writer import MigrationWriter
5 6
 from django.db import models, migrations
@@ -10,23 +11,33 @@ class WriterTests(TransactionTestCase):
10 11
     Tests the migration writer (makes migration files from Migration instances)
11 12
     """
12 13
 
13  
-    def safe_exec(self, value, string):
  14
+    def safe_exec(self, string, value=None):
14 15
         l = {}
15 16
         try:
16  
-            exec(string, {}, l)
17  
-        except:
18  
-            self.fail("Could not serialize %r: failed to exec %r" % (value, string.strip()))
  17
+            exec(string, globals(), l)
  18
+        except Exception as e:
  19
+            if value:
  20
+                self.fail("Could not exec %r (from value %r): %s" % (string.strip(), value, e))
  21
+            else:
  22
+                self.fail("Could not exec %r: %s" % (string.strip(), e))
19 23
         return l
20 24
 
21  
-    def assertSerializedEqual(self, value):
  25
+    def serialize_round_trip(self, value):
22 26
         string, imports = MigrationWriter.serialize(value)
23  
-        new_value = self.safe_exec(value, "%s\ntest_value_result = %s" % ("\n".join(imports), string))['test_value_result']
24  
-        self.assertEqual(new_value, value)
  27
+        return self.safe_exec("%s\ntest_value_result = %s" % ("\n".join(imports), string), value)['test_value_result']
  28
+
  29
+    def assertSerializedEqual(self, value):
  30
+        self.assertEqual(self.serialize_round_trip(value), value)
25 31
 
26 32
     def assertSerializedIs(self, value):
27  
-        string, imports = MigrationWriter.serialize(value)
28  
-        new_value = self.safe_exec(value, "%s\ntest_value_result = %s" % ("\n".join(imports), string))['test_value_result']
29  
-        self.assertIs(new_value, value)
  33
+        self.assertIs(self.serialize_round_trip(value), value)
  34
+
  35
+    def assertSerializedFieldEqual(self, value):
  36
+        new_value = self.serialize_round_trip(value)
  37
+        self.assertEqual(value.__class__, new_value.__class__)
  38
+        self.assertEqual(value.max_length, new_value.max_length)
  39
+        self.assertEqual(value.null, new_value.null)
  40
+        self.assertEqual(value.unique, new_value.unique)
30 41
 
31 42
     def test_serialize(self):
32 43
         """
@@ -48,6 +59,9 @@ def test_serialize(self):
48 59
         self.assertSerializedEqual(datetime.datetime.utcnow)
49 60
         self.assertSerializedEqual(datetime.date.today())
50 61
         self.assertSerializedEqual(datetime.date.today)
  62
+        # Django fields
  63
+        self.assertSerializedFieldEqual(models.CharField(max_length=255))
  64
+        self.assertSerializedFieldEqual(models.TextField(null=True, blank=True))
51 65
 
52 66
     def test_simple_migration(self):
53 67
         """
@@ -62,4 +76,9 @@ def test_simple_migration(self):
62 76
         })
63 77
         writer = MigrationWriter(migration)
64 78
         output = writer.as_string()
65  
-        print output
  79
+        # It should NOT be unicode.
  80
+        self.assertIsInstance(output, six.binary_type, "Migration as_string returned unicode")
  81
+        # We don't test the output formatting - that's too fragile.
  82
+        # Just make sure it runs for now, and that things look alright.
  83
+        result = self.safe_exec(output)
  84
+        self.assertIn("Migration", result)

0 notes on commit 91c470d

Please sign in to comment.
Something went wrong with that request. Please try again.