shorthands.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. from typing import Optional, Type
  2. from dateutil.relativedelta import relativedelta
  3. from psqlextra.models import PostgresPartitionedModel
  4. from .config import PostgresPartitioningConfig
  5. from .current_time_strategy import PostgresCurrentTimePartitioningStrategy
  6. from .time_partition_size import PostgresTimePartitionSize
  7. def partition_by_current_time(
  8. model: Type[PostgresPartitionedModel],
  9. count: int,
  10. years: Optional[int] = None,
  11. months: Optional[int] = None,
  12. weeks: Optional[int] = None,
  13. days: Optional[int] = None,
  14. max_age: Optional[relativedelta] = None,
  15. name_format: Optional[str] = None,
  16. ) -> PostgresPartitioningConfig:
  17. """Short-hand for generating a partitioning config that partitions the
  18. specified model by time.
  19. One specifies one of the `years`, `months`, `weeks`
  20. or `days` parameter to indicate the size of each
  21. partition. These parameters cannot be combined.
  22. Arguments:
  23. count:
  24. The amount of partitions to create ahead of
  25. the current date/time.
  26. years:
  27. The amount of years each partition should contain.
  28. months:
  29. The amount of months each partition should contain.
  30. weeks:
  31. The amount of weeks each partition should contain.
  32. days:
  33. The amount of days each partition should contain.
  34. max_age:
  35. The maximum age of a partition (calculated from the
  36. start of the partition).
  37. Partitions older than this are deleted when running
  38. a delete/cleanup run.
  39. name_format:
  40. The datetime format which is being passed to datetime.strftime
  41. to generate the partition name.
  42. """
  43. size = PostgresTimePartitionSize(
  44. years=years, months=months, weeks=weeks, days=days
  45. )
  46. return PostgresPartitioningConfig(
  47. model=model,
  48. strategy=PostgresCurrentTimePartitioningStrategy(
  49. size=size,
  50. count=count,
  51. max_age=max_age,
  52. name_format=name_format,
  53. ),
  54. )
  55. __all_ = ["partition_by_current_time"]