SectorLabs/django-postgres-extra

Option to specify monthly partition suffix

mneirynck opened this issue · 3 comments

Right now the monthly partitions are named:
partitions will be named [table_name]_[year]_[3-letter month name].

Which doesn't make sense for use, we would prefer to have the month number instead.
Maybe a toggle to specify which format you want to use might make sense?

I'm also interested in this. Right now I see no "easy" way to customize the naming.

I needed to change the daily partitioning naming from "%Y_%b_%d" to "%Y_%m_%d".

Currently I'm using this as a "workaround" (basically just copy and paste and make the necessary change):

settings.py

PSQLEXTRA_PARTITIONING_MANAGER = 'myapp.core.partitioning.manager'

myapp/core/partitioning.py

from psqlextra.partitioning import PostgresPartitioningManager, PostgresTimePartitionSize
from psqlextra.partitioning.config import PostgresPartitioningConfig

from myapp.core.models import MyModel
from myapp.core.partitioning_custom_current_time_strategy import \
    CustomPostgresCurrentTimePartitioningStrategy

manager = PostgresPartitioningManager([
    PostgresPartitioningConfig(
        model=MyModel,
        strategy=CustomPostgresCurrentTimePartitioningStrategy(
            size=PostgresTimePartitionSize(days=1),
            count=7,
        ),
    ),
])

myapp/core/partitioning_custom_current_time_strategy.py

from typing import Generator

from psqlextra.partitioning.current_time_strategy import PostgresCurrentTimePartitioningStrategy
from psqlextra.partitioning.time_partition import PostgresTimePartition
from psqlextra.partitioning.time_partition_size import PostgresTimePartitionUnit


class CustomPostgresTimePartition(PostgresTimePartition):
    _unit_name_format = {
        PostgresTimePartitionUnit.YEARS: "%Y",
        PostgresTimePartitionUnit.MONTHS: "%Y_%b",
        PostgresTimePartitionUnit.WEEKS: "%Y_week_%W",
        PostgresTimePartitionUnit.DAYS: "%Y_%m_%d",  # --> HERE IS THE CUSTOM CHANGE
    }


class CustomPostgresCurrentTimePartitioningStrategy(
    PostgresCurrentTimePartitioningStrategy
):
    def to_create(self) -> Generator[CustomPostgresTimePartition, None, None]:
        current_datetime = self.size.start(self.get_start_datetime())

        for _ in range(self.count):
            yield CustomPostgresTimePartition(
                start_datetime=current_datetime, size=self.size
            )

            current_datetime += self.size.as_delta()

    def to_delete(self) -> Generator[CustomPostgresTimePartition, None, None]:
        if not self.max_age:
            return

        current_datetime = self.size.start(
            self.get_start_datetime() - self.max_age
        )

        while True:
            yield CustomPostgresTimePartition(
                start_datetime=current_datetime, size=self.size
            )

            current_datetime -= self.size.as_delta()

I am happy to accept a PR that adds parameters to PostgresCurrentTimePartitioningStrategy and PostgresTimePartition to customize the format.

Closed by #200. Thanks @ckrybus!