Ticket #34697: 0001-Fixed-34697-Added-serializer-for-unordered-sequences.patch

File 0001-Fixed-34697-Added-serializer-for-unordered-sequences.patch, 2.7 KB (added by Yury V. Zaytsev, 17 months ago)
  • AUTHORS

    From 75aa4cc6229910b519040e2716b1e1da5ca620cf Mon Sep 17 00:00:00 2001
    From: "Yury V. Zaytsev" <yury.zaytsev@moneymeets.com>
    Date: Thu, 6 Jul 2023 18:09:21 +0200
    Subject: [PATCH] Fixed #34697 -- Added serializer for unordered sequences to
     ensure stable migration writer output.
    
    ---
     AUTHORS                            | 1 +
     django/db/migrations/serializer.py | 9 +++++++--
     tests/migrations/test_writer.py    | 2 ++
     3 files changed, 10 insertions(+), 2 deletions(-)
    
    diff --git a/AUTHORS b/AUTHORS
    index 40df3589c0..00a79b330c 100644
    a b answer newbie questions, and generally made Django that much better:  
    10421042    ye7cakf02@sneakemail.com
    10431043    ymasuda@ethercube.com
    10441044    Yoong Kang Lim <yoongkang.lim@gmail.com>
     1045    Yury V. Zaytsev <yury@shurup.com>
    10451046    Yusuke Miyazaki <miyazaki.dev@gmail.com>
    10461047    yyyyyyyan <contact@yyyyyyyan.tech>
    10471048    Zac Hatfield-Dodds <zac.hatfield.dodds@gmail.com>
  • django/db/migrations/serializer.py

    diff --git a/django/db/migrations/serializer.py b/django/db/migrations/serializer.py
    index 454feaa829..d88cda6e20 100644
    a b class BaseSequenceSerializer(BaseSerializer):  
    4646        return value % (", ".join(strings)), imports
    4747
    4848
     49class BaseUnorderedSequenceSerializer(BaseSequenceSerializer):
     50    def __init__(self, value):
     51        super().__init__(sorted(value, key=repr))
     52
     53
    4954class BaseSimpleSerializer(BaseSerializer):
    5055    def serialize(self):
    5156        return repr(self.value), set()
    class FloatSerializer(BaseSimpleSerializer):  
    151156        return super().serialize()
    152157
    153158
    154 class FrozensetSerializer(BaseSequenceSerializer):
     159class FrozensetSerializer(BaseUnorderedSequenceSerializer):
    155160    def _format(self):
    156161        return "frozenset([%s])"
    157162
    class SequenceSerializer(BaseSequenceSerializer):  
    279284        return "[%s]"
    280285
    281286
    282 class SetSerializer(BaseSequenceSerializer):
     287class SetSerializer(BaseUnorderedSequenceSerializer):
    283288    def _format(self):
    284289        # Serialize as a set literal except when value is empty because {}
    285290        # is an empty dict.
  • tests/migrations/test_writer.py

    diff --git a/tests/migrations/test_writer.py b/tests/migrations/test_writer.py
    index 33b52bd538..4cef9a7bbd 100644
    a b class WriterTests(SimpleTestCase):  
    774774        self.assertSerializedResultEqual(set(), ("set()", set()))
    775775        self.assertSerializedEqual({"a"})
    776776        self.assertSerializedResultEqual({"a"}, ("{'a'}", set()))
     777        self.assertSerializedEqual({"c", "b", "a"})
     778        self.assertSerializedResultEqual({"c", "b", "a"}, ("{'a', 'b', 'c'}", set()))
    777779
    778780    def test_serialize_timedelta(self):
    779781        self.assertSerializedEqual(datetime.timedelta())
Back to Top