model_utils.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  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 is_testing_database(self) -> bool:
  17. return settings.TESTING or settings.ENABLE_TEST_API
  18. def state_forwards(self, *args, **kwargs):
  19. if self.is_testing_database():
  20. super().state_forwards(*args, **kwargs)
  21. def database_forwards(self, *args, **kwargs):
  22. if self.is_testing_database():
  23. super().database_forwards(*args, **kwargs)
  24. def database_backwards(self, *args, **kwargs):
  25. if self.is_testing_database():
  26. super().database_backwards(*args, **kwargs)