Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions libs/shared/shared/django_apps/utils/partitioning.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime, timezone
from dateutil.relativedelta import relativedelta
from psqlextra.partitioning import (
PostgresCurrentTimePartitioningStrategy,
Expand All @@ -10,6 +11,12 @@
from shared.django_apps.upload_breadcrumbs.models import UploadBreadcrumb
from shared.django_apps.user_measurements.models import UserMeasurement

class PostgresTimeUnitNextPartitioningStrategy(PostgresCurrentTimePartitioningStrategy):
def get_start_datetime(self) -> datetime:
""" Add 1 of PostgresTimePartitionUnit"""
unit_plus_one = {self.size.unit: 1}
return datetime.now(timezone.utc) + relativedelta(**unit_plus_one)

# Overlapping partitions will cause errors - https://www.postgresql.org/docs/current/ddl-partitioning.html#DDL-PARTITIONING-DECLARATIVE -> "create partitions"
# Note that the partitioning manager will not perform the actual creation and deletion of partitions automatically, we have a Celery task for that.
manager = PostgresPartitioningManager(
Expand All @@ -19,7 +26,7 @@
# Partitions will be named `[table_name]_[year]_[3-letter month name]`
PostgresPartitioningConfig(
model=UserMeasurement,
strategy=PostgresCurrentTimePartitioningStrategy(
strategy=PostgresTimeUnitNextPartitioningStrategy(
size=PostgresTimePartitionSize(months=1),
count=12,
max_age=relativedelta(months=12),
Expand All @@ -30,7 +37,7 @@
# Partitions will be named `[table_name]_[year]_[3-letter month name]`
PostgresPartitioningConfig(
model=DailyTestRollup,
strategy=PostgresCurrentTimePartitioningStrategy(
strategy=PostgresTimeUnitNextPartitioningStrategy(
size=PostgresTimePartitionSize(months=1),
count=3,
max_age=relativedelta(months=3),
Expand All @@ -41,7 +48,7 @@
# Partitions will be named `[table_name]_[year]_week_[week number]`
PostgresPartitioningConfig(
model=UploadBreadcrumb,
strategy=PostgresCurrentTimePartitioningStrategy(
strategy=PostgresTimeUnitNextPartitioningStrategy(
size=PostgresTimePartitionSize(weeks=1),
count=3,
max_age=relativedelta(months=3),
Expand Down