range_partition.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. from typing import Any, Optional, Type
  2. from psqlextra.backend.schema import PostgresSchemaEditor
  3. from psqlextra.models import PostgresPartitionedModel
  4. from .partition import PostgresPartition
  5. class PostgresRangePartition(PostgresPartition):
  6. """Base class for a PostgreSQL table partition in a range partitioned
  7. table."""
  8. def __init__(self, from_values: Any, to_values: Any) -> None:
  9. self.from_values = from_values
  10. self.to_values = to_values
  11. def deconstruct(self) -> dict:
  12. return {
  13. **super().deconstruct(),
  14. "from_values": self.from_values,
  15. "to_values": self.to_values,
  16. }
  17. def create(
  18. self,
  19. model: Type[PostgresPartitionedModel],
  20. schema_editor: PostgresSchemaEditor,
  21. comment: Optional[str] = None,
  22. ) -> None:
  23. schema_editor.add_range_partition(
  24. model=model,
  25. name=self.name(),
  26. from_values=self.from_values,
  27. to_values=self.to_values,
  28. comment=comment,
  29. )
  30. def delete(
  31. self,
  32. model: Type[PostgresPartitionedModel],
  33. schema_editor: PostgresSchemaEditor,
  34. ) -> None:
  35. schema_editor.delete_partition(model, self.name())
  36. __all__ = ["PostgresRangePartition"]