<commit_before>from django.apps import apps
from django.core.management.base import BaseCommand
from django.db.utils import NotSupportedError, OperationalError

from psqlextra.auto_partition import (
    PostgresAutoPartitioningIntervalUnit,
    postgres_auto_partition,
)


class Command(BaseCommand):
    """Creates partitions for future dates for tables that use date/time range
    partitioning."""

    help = "Automatically create PostgreSQL 11.x partitions ahead."

    def add_arguments(self, parser):
        parser.add_argument(
            "app-label",
            type=str,
            help="Label of the app the partitioned model is in.",
        )

        parser.add_argument(
            "model_name",
            type=str,
            help="Name of the partitioned model to auto partition for.",
        )

        parser.add_argument(
            "--count",
            "-c",
            type=int,
            help="Amount of partitions to create in ahead of the current date.",
            default=1,
        )

        parser.add_argument(
            "--interval-unit",
            "-u",
            type=str,
            choices=PostgresAutoPartitioningIntervalUnit.values(),
            help="Unit in which to express the interval (months/weeks/days).",
            default="month",
        )

        parser.add_argument(
            "--interval",
            "-i",
            type=int,
            help="Amount of time in-between partitions (see --interval-unit).",
            default=1,
        )

    def handle(self, *app_labels, **options):
        app_label = options.get("app_label")
        model_name = options.get("model_name")
        count = options.get("count")
        interval_unit = PostgresAutoPartitioningIntervalUnit(
            options.get("interval_unit")
        )
        interval = options.get("interval")

        model = apps.get_model(app_label, model_name)
        if not model:
            raise OperationalError(f"Cannot find a model named '{model_name}'")

        if not issubclass(model, PostgresMaterializedViewModel):
            raise NotSupportedError(
                f"Model {model.__name__} is not a `PostgresPartitionedModel`"
            )

        postgres_auto_partition(
            model, count=count, interval_unit=interval_unit, interval=interval
        )<commit_msg>pgautopartition should check for PostgresPartitionedModel<commit_after>from django.apps import apps
from django.core.management.base import BaseCommand
from django.db.utils import NotSupportedError, OperationalError

from psqlextra.auto_partition import (
    PostgresAutoPartitioningIntervalUnit,
    postgres_auto_partition,
)
from psqlextra.models import PostgresPartitionedModel


class Command(BaseCommand):
    """Creates partitions for future dates for tables that use date/time range
    partitioning."""

    help = "Automatically create PostgreSQL 11.x partitions ahead."

    def add_arguments(self, parser):
        parser.add_argument(
            "app-label",
            type=str,
            help="Label of the app the partitioned model is in.",
        )

        parser.add_argument(
            "model_name",
            type=str,
            help="Name of the partitioned model to auto partition for.",
        )

        parser.add_argument(
            "--count",
            "-c",
            type=int,
            help="Amount of partitions to create in ahead of the current date.",
            default=1,
        )

        parser.add_argument(
            "--interval-unit",
            "-u",
            type=str,
            choices=PostgresAutoPartitioningIntervalUnit.values(),
            help="Unit in which to express the interval (months/weeks/days).",
            default="month",
        )

        parser.add_argument(
            "--interval",
            "-i",
            type=int,
            help="Amount of time in-between partitions (see --interval-unit).",
            default=1,
        )

    def handle(self, *app_labels, **options):
        app_label = options.get("app_label")
        model_name = options.get("model_name")
        count = options.get("count")
        interval_unit = PostgresAutoPartitioningIntervalUnit(
            options.get("interval_unit")
        )
        interval = options.get("interval")

        model = apps.get_model(app_label, model_name)
        if not model:
            raise OperationalError(f"Cannot find a model named '{model_name}'")

        if not issubclass(model, PostgresPartitionedModel):
            raise NotSupportedError(
                f"Model {model.__name__} is not a `PostgresPartitionedModel`"
            )

        postgres_auto_partition(
            model, count=count, interval_unit=interval_unit, interval=interval
        )
{'fraction_non_alphanumeric': 0.0914417242100858, 'fraction_numerical': 0.0016739903745553464, 'mean_word_length': 2.107932379713914, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4662474', 'n_tokens_mistral': 1254, 'n_tokens_neox': 1216, 'n_words': 314}