Ticket #34697: 0002-Fixed-30029-Sort-dependencies-in-MigrationWriter-to-.patch

File 0002-Fixed-30029-Sort-dependencies-in-MigrationWriter-to-.patch, 2.8 KB (added by Yury V. Zaytsev, 16 months ago)
  • django/db/migrations/writer.py

    From 3389652a37906fee16576896ec76f558506a57be Mon Sep 17 00:00:00 2001
    From: Dakota Hawkins <dakotahawkins@gmail.com>
    Date: Fri, 7 Jul 2023 19:52:29 +0200
    Subject: [PATCH 2/2] Fixed #30029 -- Sort dependencies in `MigrationWriter` to
     ensure deterministic output.
    
    ---
     django/db/migrations/writer.py  |  2 +-
     tests/migrations/test_writer.py | 29 +++++++++++++++++++++++++++++
     2 files changed, 30 insertions(+), 1 deletion(-)
    
    diff --git a/django/db/migrations/writer.py b/django/db/migrations/writer.py
    index 79b89b269d..e20359d0d7 100644
    a b class MigrationWriter:  
    154154                imports.add("from django.conf import settings")
    155155            else:
    156156                dependencies.append("        %s," % self.serialize(dependency)[0])
    157         items["dependencies"] = "\n".join(dependencies) + "\n" if dependencies else ""
     157        items["dependencies"] = "\n".join(sorted(dependencies)) + "\n" if dependencies else ""
    158158
    159159        # Format imports nicely, swapping imports of functions from migration files
    160160        # for comments
  • tests/migrations/test_writer.py

    diff --git a/tests/migrations/test_writer.py b/tests/migrations/test_writer.py
    index 4cef9a7bbd..46f7afb9f5 100644
    a b class WriterTests(SimpleTestCase):  
    893893            result["custom_migration_operations"].more_operations.TestOperation,
    894894        )
    895895
     896    def test_sorted_dependencies(self):
     897        """
     898        #30029 - Tests ordering of dependencies.
     899        """
     900
     901        migration = type("Migration", (migrations.Migration,), {
     902            "operations": [
     903                migrations.AddField("mymodel", "myfield", models.DateTimeField(
     904                    default=datetime.datetime(2012, 1, 1, 1, 1, tzinfo=datetime.timezone.utc),
     905                )),
     906            ],
     907            "dependencies": [
     908                ("testapp09", "0005_fifth"),
     909                ("testapp03", "0003_third"),
     910                ("testapp10", "0006_sixth"),
     911                ("testapp05", "0001_initial"),
     912                ("testapp02", "0002_second"),
     913                ("testapp06", "0002_second"),
     914                ("testapp08", "0004_fourth"),
     915                ("testapp07", "0003_third"),
     916                ("testapp04", "0002_second"),
     917                ("testapp01", "0001_initial"),
     918            ],
     919        })
     920        unsorted_output = MigrationWriter(migration, include_header=False).as_string()
     921        migration.dependencies = sorted(migration.dependencies)
     922        sorted_output = MigrationWriter(migration, include_header=False).as_string()
     923        self.assertEqual(unsorted_output, sorted_output)
     924
    896925    def test_sorted_imports(self):
    897926        """
    898927        #24155 - Tests ordering of imports.
Back to Top