uuid_compat.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. """UUID7 compatibility layer for Python 3.13+
  2. Python 3.14+ has native uuid7 support. For Python 3.13, we use uuid_extensions.
  3. IMPORTANT: We also monkey-patch uuid.uuid7 for backward compatibility with
  4. migrations that were auto-generated on Python 3.14+ systems.
  5. """
  6. import sys
  7. import uuid
  8. import functools
  9. if sys.version_info >= (3, 14):
  10. from uuid import uuid7 as _uuid7
  11. else:
  12. try:
  13. from uuid_extensions import uuid7 as _uuid7
  14. except ImportError:
  15. raise ImportError(
  16. "uuid_extensions package is required for Python <3.14. "
  17. "Install it with: pip install uuid_extensions"
  18. )
  19. # Monkey-patch uuid module for migrations generated on Python 3.14+
  20. # that reference uuid.uuid7 directly
  21. if not hasattr(uuid, 'uuid7'):
  22. uuid.uuid7 = _uuid7
  23. @functools.wraps(_uuid7)
  24. def uuid7():
  25. """Generate a UUID7 (time-ordered UUID).
  26. This wrapper ensures Django migrations always reference
  27. 'archivebox.uuid_compat.uuid7' regardless of Python version.
  28. """
  29. return _uuid7()
  30. __all__ = ['uuid7']