File tree Expand file tree Collapse file tree 2 files changed +29
-0
lines changed Expand file tree Collapse file tree 2 files changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -797,6 +797,18 @@ class UUIDDefaultModel(models.Model):
797797 history = HistoricalRecords ()
798798
799799
800+ # Set the SIMPLE_HISTORY_HISTORY_ID_UUID_VERSION
801+ setattr (settings , "SIMPLE_HISTORY_HISTORY_ID_UUID_VERSION" , 7 )
802+
803+
804+ class UUIDv7Model (models .Model ):
805+ id = models .UUIDField (primary_key = True , default = uuid .uuid4 , editable = False )
806+ history = HistoricalRecords ()
807+
808+
809+ # Clear the SIMPLE_HISTORY_HISTORY_ID_UUID_VERSION
810+ delattr (settings , "SIMPLE_HISTORY_HISTORY_ID_UUID_VERSION" )
811+
800812# Clear the SIMPLE_HISTORY_HISTORY_ID_USE_UUID
801813delattr (settings , "SIMPLE_HISTORY_HISTORY_ID_USE_UUID" )
802814
Original file line number Diff line number Diff line change 11import dataclasses
2+ import time
23import unittest
34import uuid
45import warnings
122123 UserTextFieldChangeReasonModel ,
123124 UUIDDefaultModel ,
124125 UUIDModel ,
126+ UUIDv7Model ,
125127 WaterLevel ,
126128)
127129from .utils import (
@@ -637,6 +639,21 @@ def test_uuid_default_history_id(self):
637639 history = entry .history .all ()[0 ]
638640 self .assertTrue (isinstance (history .history_id , uuid .UUID ))
639641
642+ def test_uuidv7_history_id (self ):
643+ """
644+ Tests that UUIDv7 is being used for history_id when defined in the settings,
645+ and that consequent PKs are ordered based on creation time.
646+ """
647+ entries = []
648+ for _ in range (10 ):
649+ entries .append (UUIDv7Model .objects .create ())
650+ time .sleep (0.01 ) # Ensure increasing time part of UUIDv7 PKs
651+
652+ history = [entry .history .all ()[0 ] for entry in entries ]
653+ self .assertTrue (all (isinstance (h .history_id , uuid .UUID ) for h in history ))
654+ for h1 , h2 in zip (history [:- 1 ], history [1 :]):
655+ self .assertLess (h1 .history_id , h2 .history_id )
656+
640657 def test_default_history_change_reason (self ):
641658 entry = CharFieldChangeReasonModel .objects .create (greeting = "what's up?" )
642659 history = entry .history .get ()
You can’t perform that action at this time.
0 commit comments