# Generated by Django 5.2.3 on 2025-06-26 16:30

from django.conf import settings
import django.db.models.deletion
import psql_partition.backend.migrations.operations.create_partitioned_model
import psql_partition.manager.manager
import psql_partition.models.partitioned
import psql_partition.types
from django.db import migrations, models

from glitchtip.model_utils import TestDefaultPartition


# --- Define SQL for the Advanced Path ---
CREATE_ADVANCED_PARTITION_SQL = """
    CREATE TABLE "issue_events_issueaggregate" (
        "issue_id" BIGINT NOT NULL,
        "organization_id" INTEGER NOT NULL,
        "date" TIMESTAMPTZ NOT NULL,
        "count" INTEGER NOT NULL,
        PRIMARY KEY ("issue_id", "organization_id", "date")
    ) PARTITION BY HASH (organization_id);

    CREATE TABLE issue_events_issueaggregate_p0 PARTITION OF issue_events_issueaggregate FOR VALUES WITH (MODULUS 4, REMAINDER 0) PARTITION BY RANGE (date);
    CREATE TABLE issue_events_issueaggregate_p1 PARTITION OF issue_events_issueaggregate FOR VALUES WITH (MODULUS 4, REMAINDER 1) PARTITION BY RANGE (date);
    CREATE TABLE issue_events_issueaggregate_p2 PARTITION OF issue_events_issueaggregate FOR VALUES WITH (MODULUS 4, REMAINDER 2) PARTITION BY RANGE (date);
    CREATE TABLE issue_events_issueaggregate_p3 PARTITION OF issue_events_issueaggregate FOR VALUES WITH (MODULUS 4, REMAINDER 3) PARTITION BY RANGE (date);
"""
DROP_TABLE_SQL = 'DROP TABLE IF EXISTS "issue_events_issueaggregate";'

base_operations = [
    psql_partition.backend.migrations.operations.create_partitioned_model.PostgresCreatePartitionedModel(
        name="IssueAggregate",
        fields=[
            ("date", models.DateTimeField()),
            ("count", models.PositiveIntegerField()),
            (
                "pk",
                models.CompositePrimaryKey(
                    "issue",
                    "organization",
                    "date",
                    blank=True,
                    editable=False,
                    primary_key=True,
                    serialize=False,
                ),
            ),
            (
                "issue",
                models.ForeignKey(
                    on_delete=django.db.models.deletion.CASCADE,
                    to="issue_events.issue",
                ),
            ),
            (
                "organization",
                models.ForeignKey(
                    on_delete=django.db.models.deletion.CASCADE,
                    to="organizations_ext.organization",
                ),
            ),
        ],
        options={
            "abstract": False,
        },
        partitioning_options={
            "method": psql_partition.types.PostgresPartitioningMethod["RANGE"],
            "key": ["date"],
        },
        bases=(psql_partition.models.partitioned.PostgresPartitionedModel,),
        managers=[
            ("objects", psql_partition.manager.manager.PostgresManager()),
        ],
    ),
    TestDefaultPartition(
        model_name="IssueAggregate",
        name="default",
    ),
]

if settings.GLITCHTIP_ADVANCED_PARTITIONING:
    operations = [
        migrations.SeparateDatabaseAndState(
            state_operations=base_operations,
            database_operations=[
                migrations.RunSQL(
                    CREATE_ADVANCED_PARTITION_SQL, reverse_sql=DROP_TABLE_SQL
                ),
            ],
        ),
    ]
else:
    operations = base_operations


class Migration(migrations.Migration):
    dependencies = [
        ("issue_events", "0003_auto_20250527_2204"),
        ("organizations_ext", "0010_alter_organization_id"),
    ]

    operations = operations
