partition.py 1019 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. from abc import abstractmethod
  2. from typing import Optional, Type
  3. from psqlextra.backend.schema import PostgresSchemaEditor
  4. from psqlextra.models import PostgresPartitionedModel
  5. class PostgresPartition:
  6. """Base class for a PostgreSQL table partition."""
  7. @abstractmethod
  8. def name(self) -> str:
  9. """Generates/computes the name for this partition."""
  10. @abstractmethod
  11. def create(
  12. self,
  13. model: Type[PostgresPartitionedModel],
  14. schema_editor: PostgresSchemaEditor,
  15. comment: Optional[str] = None,
  16. ) -> None:
  17. """Creates this partition in the database."""
  18. @abstractmethod
  19. def delete(
  20. self,
  21. model: Type[PostgresPartitionedModel],
  22. schema_editor: PostgresSchemaEditor,
  23. ) -> None:
  24. """Deletes this partition from the database."""
  25. def deconstruct(self) -> dict:
  26. """Deconstructs this partition into a dict of attributes/fields."""
  27. return {"name": self.name()}
  28. __all__ = ["PostgresPartition"]