model_utils.py 967 B

123456789101112131415161718192021222324252627282930313233
  1. from enum import StrEnum
  2. from typing import Union
  3. from django.conf import settings
  4. from django.db import models
  5. from psqlextra.backend.migrations.operations.add_default_partition import (
  6. PostgresAddDefaultPartition,
  7. )
  8. class FromStringIntegerChoices(models.IntegerChoices):
  9. @classmethod
  10. def from_string(cls, string: Union[str, StrEnum]):
  11. for status in cls:
  12. if status.label == string:
  13. return status
  14. class TestDefaultPartition(PostgresAddDefaultPartition):
  15. """Create default partition only on test database"""
  16. def state_forwards(self, *args, **kwargs):
  17. if settings.TESTING:
  18. super().state_forwards(*args, **kwargs)
  19. def database_forwards(self, *args, **kwargs):
  20. if settings.TESTING:
  21. super().database_forwards(*args, **kwargs)
  22. def database_backwards(self, *args, **kwargs):
  23. if settings.TESTING:
  24. super().database_backwards(*args, **kwargs)