Ticket #26149: migration-bug-twotest.patch

File migration-bug-twotest.patch, 6.7 KB (added by anabelensc, 8 years ago)
  • django/db/migrations/autodetector.py

    diff --git a/django/db/migrations/autodetector.py b/django/db/migrations/autodetector.py
    index f28c89c..4f2391d 100644
    a b class MigrationAutodetector(object):  
    176176        self.generate_removed_fields()
    177177        self.generate_added_fields()
    178178        self.generate_altered_fields()
     179        self.generate_altered_order_with_respect_to()
    179180        self.generate_altered_unique_together()
    180181        self.generate_altered_index_together()
    181182        self.generate_altered_db_table()
    182         self.generate_altered_order_with_respect_to()
    183183
    184184        self._sort_migrations()
    185185        self._build_migration_list(graph)
    class MigrationAutodetector(object):  
    518518                if getattr(field.remote_field, "through", None) and not field.remote_field.through._meta.auto_created:
    519519                    related_fields[field.name] = field
    520520            # Are there unique/index_together to defer?
     521            order_with_respect_to = model_state.options.pop('order_with_respect_to', None)
    521522            unique_together = model_state.options.pop('unique_together', None)
    522523            index_together = model_state.options.pop('index_together', None)
    523             order_with_respect_to = model_state.options.pop('order_with_respect_to', None)
    524524            # Depend on the deletion of any possible proxy version of us
    525525            dependencies = [
    526526                (app_label, model_name, None, False),
    class MigrationAutodetector(object):  
    577577                for name, field in sorted(related_fields.items())
    578578            ]
    579579            related_dependencies.append((app_label, model_name, None, True))
     580            if order_with_respect_to:
     581                self.add_operation(
     582                    app_label,
     583                    operations.AlterOrderWithRespectTo(
     584                        name=model_name,
     585                        order_with_respect_to=order_with_respect_to,
     586                    ),
     587                    dependencies=[
     588                        (app_label, model_name, order_with_respect_to, True),
     589                        (app_label, model_name, None, True),
     590                    ]
     591                )
    580592            if unique_together:
    581593                self.add_operation(
    582594                    app_label,
    class MigrationAutodetector(object):  
    595607                    ),
    596608                    dependencies=related_dependencies
    597609                )
    598             if order_with_respect_to:
    599                 self.add_operation(
    600                     app_label,
    601                     operations.AlterOrderWithRespectTo(
    602                         name=model_name,
    603                         order_with_respect_to=order_with_respect_to,
    604                     ),
    605                     dependencies=[
    606                         (app_label, model_name, order_with_respect_to, True),
    607                         (app_label, model_name, None, True),
    608                     ]
    609                 )
    610610
    611611    def generate_created_proxies(self):
    612612        """
  • tests/migrations/test_autodetector.py

    diff --git a/tests/migrations/test_autodetector.py b/tests/migrations/test_autodetector.py
    index fd34414..f2bc6dd 100644
    a b class AutodetectorTests(TestCase):  
    195195        ("name", models.CharField(max_length=200)),
    196196        ("book", models.ForeignKey("otherapp.Book", models.CASCADE)),
    197197    ], options={"order_with_respect_to": "book"})
     198    author_with_book_order_wrt_together = ModelState("testapp", "Author", [
     199        ("id", models.AutoField(primary_key=True)),
     200        ("name", models.CharField(max_length=200)),
     201        ("book", models.ForeignKey("otherapp.Book", models.CASCADE)),
     202    ], options={"order_with_respect_to": "book",
     203        "unique_together": {("book", "_order")},
     204    })
    198205    author_renamed_with_book = ModelState("testapp", "Writer", [
    199206        ("id", models.AutoField(primary_key=True)),
    200207        ("name", models.CharField(max_length=200)),
    class AutodetectorTests(TestCase):  
    18181825        self.assertOperationAttributes(changes, 'testapp', 0, 1, name="author", order_with_respect_to="book")
    18191826        self.assertNotIn("_order", [name for name, field in changes['testapp'][0].operations[0].fields])
    18201827
     1828    def test_create_add_order_with_respect_to_unique_together(self):
     1829        """
     1830        Tests that setting create a model and order_with_respect_to option set
     1831        along with a unique_together which involves the implicit _order
     1832        field created by order_with_respect_to.
     1833        """
     1834        # Make state
     1835        before = self.make_project_state([])
     1836        after = self.make_project_state([
     1837            self.book,
     1838            self.author_with_book_order_wrt_together
     1839        ])
     1840        autodetector = MigrationAutodetector(before, after)
     1841        changes = autodetector._detect_changes()
     1842        # Right number/type of migrations?
     1843        self.assertNumberMigrations(changes, 'testapp', 1)
     1844        self.assertOperationTypes(changes, 'testapp', 0, [
     1845            "CreateModel",
     1846            "AlterOrderWithRespectTo",
     1847            "AlterUniqueTogether"
     1848        ])
     1849        self.assertOperationAttributes(
     1850            changes, 'testapp', 0, 1, name="author",
     1851            order_with_respect_to="book"
     1852        )
     1853        self.assertOperationAttributes(
     1854            changes, 'testapp', 0, 2, name="author",
     1855            unique_together={("book", "_order")}
     1856        )
     1857
     1858    def test_add_order_with_respect_to_and_unique_together(self):
     1859        """
     1860        Tests that setting order_with_respect_to option set along
     1861        with a unique_together which involves the implicit _order
     1862        field created by order_with_respect_to
     1863        """
     1864        # Make state
     1865        before = self.make_project_state([
     1866            self.book,
     1867            self.author_with_book
     1868        ])
     1869        after = self.make_project_state([
     1870            self.book,
     1871            self.author_with_book_order_wrt_together
     1872        ])
     1873        autodetector = MigrationAutodetector(before, after)
     1874        changes = autodetector._detect_changes()
     1875        # Right number/type of migrations?
     1876        self.assertNumberMigrations(changes, 'testapp', 1)
     1877        self.assertOperationTypes(changes, 'testapp', 0, [
     1878            "AlterOrderWithRespectTo",
     1879            "AlterUniqueTogether"
     1880        ])
     1881        self.assertOperationAttributes(
     1882            changes, 'testapp', 0, 0, name="author",
     1883            order_with_respect_to="book"
     1884        )
     1885        self.assertOperationAttributes(
     1886            changes, 'testapp', 0, 1, name="author",
     1887            unique_together={("book", "_order")}
     1888        )
     1889
    18211890    def test_alter_model_managers(self):
    18221891        """
    18231892        Tests that changing the model managers adds a new operation.
Back to Top