# Generated by Django 5.2.9 on 2026-01-02 00:33

import django.db.models.deletion
import pgtrigger.compiler
import pgtrigger.migrations
from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ("pghistory", "0006_delete_aggregateevent"),
        ("rides", "0030_add_kiddie_and_transportation_stats"),
    ]

    operations = [
        migrations.CreateModel(
            name="RideNameHistory",
            fields=[
                ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                ("former_name", models.CharField(help_text="The previous name of the ride", max_length=200)),
                (
                    "from_year",
                    models.PositiveSmallIntegerField(
                        blank=True, help_text="Year the ride started using this name", null=True
                    ),
                ),
                (
                    "to_year",
                    models.PositiveSmallIntegerField(
                        blank=True, help_text="Year the ride stopped using this name", null=True
                    ),
                ),
                (
                    "reason",
                    models.CharField(
                        blank=True, help_text="Reason for the name change (e.g., 'Retheme to Peanuts')", max_length=500
                    ),
                ),
                (
                    "ride",
                    models.ForeignKey(
                        help_text="The ride this name history entry belongs to",
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="former_names",
                        to="rides.ride",
                    ),
                ),
            ],
            options={
                "verbose_name": "Ride Name History",
                "verbose_name_plural": "Ride Name Histories",
                "ordering": ["-to_year", "-from_year"],
                "abstract": False,
            },
        ),
        migrations.CreateModel(
            name="RideNameHistoryEvent",
            fields=[
                ("pgh_id", models.AutoField(primary_key=True, serialize=False)),
                ("pgh_created_at", models.DateTimeField(auto_now_add=True)),
                ("pgh_label", models.TextField(help_text="The event label.")),
                ("id", models.BigIntegerField()),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                ("former_name", models.CharField(help_text="The previous name of the ride", max_length=200)),
                (
                    "from_year",
                    models.PositiveSmallIntegerField(
                        blank=True, help_text="Year the ride started using this name", null=True
                    ),
                ),
                (
                    "to_year",
                    models.PositiveSmallIntegerField(
                        blank=True, help_text="Year the ride stopped using this name", null=True
                    ),
                ),
                (
                    "reason",
                    models.CharField(
                        blank=True, help_text="Reason for the name change (e.g., 'Retheme to Peanuts')", max_length=500
                    ),
                ),
                (
                    "pgh_context",
                    models.ForeignKey(
                        db_constraint=False,
                        null=True,
                        on_delete=django.db.models.deletion.DO_NOTHING,
                        related_name="+",
                        to="pghistory.context",
                    ),
                ),
                (
                    "pgh_obj",
                    models.ForeignKey(
                        db_constraint=False,
                        on_delete=django.db.models.deletion.DO_NOTHING,
                        related_name="events",
                        to="rides.ridenamehistory",
                    ),
                ),
                (
                    "ride",
                    models.ForeignKey(
                        db_constraint=False,
                        help_text="The ride this name history entry belongs to",
                        on_delete=django.db.models.deletion.DO_NOTHING,
                        related_name="+",
                        related_query_name="+",
                        to="rides.ride",
                    ),
                ),
            ],
            options={
                "abstract": False,
            },
        ),
        migrations.AddIndex(
            model_name="ridenamehistory",
            index=models.Index(fields=["ride", "-to_year"], name="rides_riden_ride_id_b546e5_idx"),
        ),
        pgtrigger.migrations.AddTrigger(
            model_name="ridenamehistory",
            trigger=pgtrigger.compiler.Trigger(
                name="insert_insert",
                sql=pgtrigger.compiler.UpsertTriggerSql(
                    func='INSERT INTO "rides_ridenamehistoryevent" ("created_at", "former_name", "from_year", "id", "pgh_context_id", "pgh_created_at", "pgh_label", "pgh_obj_id", "reason", "ride_id", "to_year", "updated_at") VALUES (NEW."created_at", NEW."former_name", NEW."from_year", NEW."id", _pgh_attach_context(), NOW(), \'insert\', NEW."id", NEW."reason", NEW."ride_id", NEW."to_year", NEW."updated_at"); RETURN NULL;',
                    hash="b79231914244e431999014db94fd52759cc41541",
                    operation="INSERT",
                    pgid="pgtrigger_insert_insert_ca1f7",
                    table="rides_ridenamehistory",
                    when="AFTER",
                ),
            ),
        ),
        pgtrigger.migrations.AddTrigger(
            model_name="ridenamehistory",
            trigger=pgtrigger.compiler.Trigger(
                name="update_update",
                sql=pgtrigger.compiler.UpsertTriggerSql(
                    condition="WHEN (OLD.* IS DISTINCT FROM NEW.*)",
                    func='INSERT INTO "rides_ridenamehistoryevent" ("created_at", "former_name", "from_year", "id", "pgh_context_id", "pgh_created_at", "pgh_label", "pgh_obj_id", "reason", "ride_id", "to_year", "updated_at") VALUES (NEW."created_at", NEW."former_name", NEW."from_year", NEW."id", _pgh_attach_context(), NOW(), \'update\', NEW."id", NEW."reason", NEW."ride_id", NEW."to_year", NEW."updated_at"); RETURN NULL;',
                    hash="c760d29ecb57f1f3c92e76c7f5d45027db136b84",
                    operation="UPDATE",
                    pgid="pgtrigger_update_update_99e4e",
                    table="rides_ridenamehistory",
                    when="AFTER",
                ),
            ),
        ),
    ]
