# Generated by Django 4.2.6 on 2024-09-11 00:46

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"),
        ("tests", "0012_alter_bigautofieldmodelevent_pgh_context_and_more"),
    ]

    operations = [
        migrations.CreateModel(
            name="ConcreteParent",
            fields=[
                (
                    "id",
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("name", models.CharField(max_length=32)),
            ],
        ),
        migrations.CreateModel(
            name="ConcreteChild",
            fields=[
                (
                    "concreteparent_ptr",
                    models.OneToOneField(
                        auto_created=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        parent_link=True,
                        primary_key=True,
                        serialize=False,
                        to="tests.concreteparent",
                    ),
                ),
                ("age", models.IntegerField()),
            ],
            bases=("tests.concreteparent",),
        ),
        migrations.CreateModel(
            name="ConcreteChildEvent",
            fields=[
                (
                    "concreteparent_ptr",
                    models.ForeignKey(
                        auto_created=True,
                        db_constraint=False,
                        on_delete=django.db.models.deletion.DO_NOTHING,
                        parent_link=True,
                        related_name="+",
                        related_query_name="+",
                        serialize=False,
                        to="tests.concreteparent",
                    ),
                ),
                ("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.")),
                ("age", models.IntegerField()),
                (
                    "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="tests.concretechild",
                    ),
                ),
            ],
            options={
                "abstract": False,
            },
        ),
        pgtrigger.migrations.AddTrigger(
            model_name="concretechild",
            trigger=pgtrigger.compiler.Trigger(
                name="insert_insert",
                sql=pgtrigger.compiler.UpsertTriggerSql(
                    func='INSERT INTO "tests_concretechildevent" ("age", "concreteparent_ptr_id", "pgh_context_id", "pgh_created_at", "pgh_label", "pgh_obj_id") VALUES (NEW."age", NEW."concreteparent_ptr_id", _pgh_attach_context(), NOW(), \'insert\', NEW."concreteparent_ptr_id"); RETURN NULL;',  # noqa: E501
                    hash="5740a1af0415e809c3fec2214084f412670d95f9",
                    operation="INSERT",
                    pgid="pgtrigger_insert_insert_a6cf2",
                    table="tests_concretechild",
                    when="AFTER",
                ),
            ),
        ),
        pgtrigger.migrations.AddTrigger(
            model_name="concretechild",
            trigger=pgtrigger.compiler.Trigger(
                name="update_update",
                sql=pgtrigger.compiler.UpsertTriggerSql(
                    condition='WHEN (OLD."age" IS DISTINCT FROM (NEW."age") OR OLD."concreteparent_ptr_id" IS DISTINCT FROM (NEW."concreteparent_ptr_id"))',  # noqa: E501
                    func='INSERT INTO "tests_concretechildevent" ("age", "concreteparent_ptr_id", "pgh_context_id", "pgh_created_at", "pgh_label", "pgh_obj_id") VALUES (NEW."age", NEW."concreteparent_ptr_id", _pgh_attach_context(), NOW(), \'update\', NEW."concreteparent_ptr_id"); RETURN NULL;',  # noqa: E501
                    hash="3bbc47abd3ad6a8677edfd77204000b426c45357",
                    operation="UPDATE",
                    pgid="pgtrigger_update_update_1163f",
                    table="tests_concretechild",
                    when="AFTER",
                ),
            ),
        ),
    ]
