# Generated by Django 4.2.6 on 2025-04-21 18:39

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


class Migration(migrations.Migration):
    dependencies = [
        ("pghistory", "0007_auto_20250421_0444"),
        ("tests", "0015_denormcontexteventnoidstatement_and_more"),
    ]

    operations = [
        migrations.CreateModel(
            name="CondStatement",
            fields=[
                (
                    "id",
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("int_field1", models.IntegerField()),
                ("int_field2", models.IntegerField()),
            ],
        ),
        migrations.CreateModel(
            name="CondStatementEvent",
            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.IntegerField()),
                ("int_field1", models.IntegerField()),
                ("int_field2", models.IntegerField()),
            ],
            options={
                "abstract": False,
            },
        ),
        pgtrigger.migrations.RemoveTrigger(
            model_name="denormcontextstatement",
            name="snapshot_no_id_insert_insert",
        ),
        pgtrigger.migrations.RemoveTrigger(
            model_name="denormcontextstatement",
            name="snapshot_no_id_update_update",
        ),
        pgtrigger.migrations.RemoveTrigger(
            model_name="denormcontextstatement",
            name="insert_insert",
        ),
        pgtrigger.migrations.RemoveTrigger(
            model_name="denormcontextstatement",
            name="update_update",
        ),
        pgtrigger.migrations.RemoveTrigger(
            model_name="eventmodelstatement",
            name="model_create_insert",
        ),
        pgtrigger.migrations.RemoveTrigger(
            model_name="eventmodelstatement",
            name="before_update_update",
        ),
        pgtrigger.migrations.RemoveTrigger(
            model_name="eventmodelstatement",
            name="before_delete_delete",
        ),
        pgtrigger.migrations.RemoveTrigger(
            model_name="eventmodelstatement",
            name="after_update_update",
        ),
        pgtrigger.migrations.AddTrigger(
            model_name="denormcontextstatement",
            trigger=pgtrigger.compiler.Trigger(
                name="snapshot_no_id_insert_insert",
                sql=pgtrigger.compiler.UpsertTriggerSql(
                    func='WITH _pgh_attached_context AS ( SELECT _pgh_attach_context() AS value ) INSERT INTO "tests_denormcontexteventnoidstatement" ("fk_field_id", "id", "int_field", "pgh_context", "pgh_created_at", "pgh_label", "pgh_obj_id") SELECT new_values."fk_field_id", new_values."id", new_values."int_field", COALESCE(NULLIF(CURRENT_SETTING(\'pghistory.context_metadata\', TRUE), \'\'), NULL)::JSONB, NOW(), \'snapshot_no_id_insert\', new_values."id" FROM ( SELECT new_values.* FROM new_values  ) AS new_values, _pgh_attached_context; RETURN NULL;',  # noqa: E501
                    hash="af58c1947d32a3994b8c335741de2099deac7a8b",
                    level="STATEMENT",
                    operation="INSERT",
                    pgid="pgtrigger_snapshot_no_id_insert_insert_182ba",
                    referencing="REFERENCING NEW TABLE AS new_values ",
                    table="tests_denormcontextstatement",
                    when="AFTER",
                ),
            ),
        ),
        pgtrigger.migrations.AddTrigger(
            model_name="denormcontextstatement",
            trigger=pgtrigger.compiler.Trigger(
                name="snapshot_no_id_update_update",
                sql=pgtrigger.compiler.UpsertTriggerSql(
                    func='WITH _pgh_attached_context AS ( SELECT _pgh_attach_context() AS value ) INSERT INTO "tests_denormcontexteventnoidstatement" ("fk_field_id", "id", "int_field", "pgh_context", "pgh_created_at", "pgh_label", "pgh_obj_id") SELECT new_values."fk_field_id", new_values."id", new_values."int_field", COALESCE(NULLIF(CURRENT_SETTING(\'pghistory.context_metadata\', TRUE), \'\'), NULL)::JSONB, NOW(), \'snapshot_no_id_update\', new_values."id" FROM ( SELECT new_values.* FROM old_values JOIN new_values ON (old_values."id") = (new_values."id") WHERE (old_values.* IS DISTINCT FROM new_values.*) ) AS new_values, _pgh_attached_context; RETURN NULL;',  # noqa: E501
                    hash="c61857b1aa83764928c6a654123c3988d2b6df34",
                    level="STATEMENT",
                    operation="UPDATE",
                    pgid="pgtrigger_snapshot_no_id_update_update_6e163",
                    referencing="REFERENCING OLD TABLE AS old_values  NEW TABLE AS new_values ",
                    table="tests_denormcontextstatement",
                    when="AFTER",
                ),
            ),
        ),
        pgtrigger.migrations.AddTrigger(
            model_name="denormcontextstatement",
            trigger=pgtrigger.compiler.Trigger(
                name="insert_insert",
                sql=pgtrigger.compiler.UpsertTriggerSql(
                    func='WITH _pgh_attached_context AS ( SELECT _pgh_attach_context() AS value ) INSERT INTO "tests_denormcontextstatementevent" ("fk_field_id", "id", "int_field", "pgh_context", "pgh_context_id", "pgh_created_at", "pgh_label", "pgh_obj_id") SELECT new_values."fk_field_id", new_values."id", new_values."int_field", COALESCE(NULLIF(CURRENT_SETTING(\'pghistory.context_metadata\', TRUE), \'\'), NULL)::JSONB, COALESCE(NULLIF(CURRENT_SETTING(\'pghistory.context_id\', TRUE), \'\'), NULL)::UUID, NOW(), \'insert\', new_values."id" FROM ( SELECT new_values.* FROM new_values  ) AS new_values, _pgh_attached_context; RETURN NULL;',  # noqa: E501
                    hash="571508f6b881f0526b1a57c8008b6cd2fb595b6a",
                    level="STATEMENT",
                    operation="INSERT",
                    pgid="pgtrigger_insert_insert_0fdb0",
                    referencing="REFERENCING NEW TABLE AS new_values ",
                    table="tests_denormcontextstatement",
                    when="AFTER",
                ),
            ),
        ),
        pgtrigger.migrations.AddTrigger(
            model_name="denormcontextstatement",
            trigger=pgtrigger.compiler.Trigger(
                name="update_update",
                sql=pgtrigger.compiler.UpsertTriggerSql(
                    func='WITH _pgh_attached_context AS ( SELECT _pgh_attach_context() AS value ) INSERT INTO "tests_denormcontextstatementevent" ("fk_field_id", "id", "int_field", "pgh_context", "pgh_context_id", "pgh_created_at", "pgh_label", "pgh_obj_id") SELECT new_values."fk_field_id", new_values."id", new_values."int_field", COALESCE(NULLIF(CURRENT_SETTING(\'pghistory.context_metadata\', TRUE), \'\'), NULL)::JSONB, COALESCE(NULLIF(CURRENT_SETTING(\'pghistory.context_id\', TRUE), \'\'), NULL)::UUID, NOW(), \'update\', new_values."id" FROM ( SELECT new_values.* FROM old_values JOIN new_values ON (old_values."id") = (new_values."id") WHERE (old_values.* IS DISTINCT FROM new_values.*) ) AS new_values, _pgh_attached_context; RETURN NULL;',  # noqa: E501
                    hash="e1a8bf167417479a2f03def6c518d6dc5cad450d",
                    level="STATEMENT",
                    operation="UPDATE",
                    pgid="pgtrigger_update_update_7c65e",
                    referencing="REFERENCING OLD TABLE AS old_values  NEW TABLE AS new_values ",
                    table="tests_denormcontextstatement",
                    when="AFTER",
                ),
            ),
        ),
        pgtrigger.migrations.AddTrigger(
            model_name="eventmodelstatement",
            trigger=pgtrigger.compiler.Trigger(
                name="model_create_insert",
                sql=pgtrigger.compiler.UpsertTriggerSql(
                    func='WITH _pgh_attached_context AS ( SELECT _pgh_attach_context() AS value ) INSERT INTO "tests_eventmodelstatementevent" ("dt_field", "id", "int_field", "pgh_context_id", "pgh_created_at", "pgh_label", "pgh_obj_id") SELECT new_values."dt_field", new_values."id", new_values."int_field", _pgh_attached_context.value, NOW(), \'model.create\', new_values."id" FROM ( SELECT new_values.* FROM new_values  ) AS new_values, _pgh_attached_context; RETURN NULL;',  # noqa: E501
                    hash="d9fa3b98442d40801e202c1dd07435d44ad17740",
                    level="STATEMENT",
                    operation="INSERT",
                    pgid="pgtrigger_model_create_insert_9bbc0",
                    referencing="REFERENCING NEW TABLE AS new_values ",
                    table="tests_eventmodelstatement",
                    when="AFTER",
                ),
            ),
        ),
        pgtrigger.migrations.AddTrigger(
            model_name="eventmodelstatement",
            trigger=pgtrigger.compiler.Trigger(
                name="before_update_update",
                sql=pgtrigger.compiler.UpsertTriggerSql(
                    func='WITH _pgh_attached_context AS ( SELECT _pgh_attach_context() AS value ) INSERT INTO "tests_eventmodelstatementevent" ("dt_field", "id", "int_field", "pgh_context_id", "pgh_created_at", "pgh_label", "pgh_obj_id") SELECT old_values."dt_field", old_values."id", old_values."int_field", _pgh_attached_context.value, NOW(), \'before_update\', old_values."id" FROM ( SELECT old_values.* FROM old_values JOIN new_values ON (old_values."id") = (new_values."id") WHERE (old_values.* IS DISTINCT FROM new_values.*) ) AS old_values, _pgh_attached_context; RETURN NULL;',  # noqa: E501
                    hash="41b47c35c06f661e36e8bdb380e46e5648133c9a",
                    level="STATEMENT",
                    operation="UPDATE",
                    pgid="pgtrigger_before_update_update_59271",
                    referencing="REFERENCING OLD TABLE AS old_values  NEW TABLE AS new_values ",
                    table="tests_eventmodelstatement",
                    when="AFTER",
                ),
            ),
        ),
        pgtrigger.migrations.AddTrigger(
            model_name="eventmodelstatement",
            trigger=pgtrigger.compiler.Trigger(
                name="before_delete_delete",
                sql=pgtrigger.compiler.UpsertTriggerSql(
                    func='WITH _pgh_attached_context AS ( SELECT _pgh_attach_context() AS value ) INSERT INTO "tests_eventmodelstatementevent" ("dt_field", "id", "int_field", "pgh_context_id", "pgh_created_at", "pgh_label", "pgh_obj_id") SELECT old_values."dt_field", old_values."id", old_values."int_field", _pgh_attached_context.value, NOW(), \'before_delete\', old_values."id" FROM ( SELECT old_values.* FROM old_values  ) AS old_values, _pgh_attached_context; RETURN NULL;',  # noqa: E501
                    hash="856f146c1b8b8301758cde821d78a752d66d5da4",
                    level="STATEMENT",
                    operation="DELETE",
                    pgid="pgtrigger_before_delete_delete_8f0bf",
                    referencing="REFERENCING OLD TABLE AS old_values ",
                    table="tests_eventmodelstatement",
                    when="AFTER",
                ),
            ),
        ),
        pgtrigger.migrations.AddTrigger(
            model_name="eventmodelstatement",
            trigger=pgtrigger.compiler.Trigger(
                name="after_update_update",
                sql=pgtrigger.compiler.UpsertTriggerSql(
                    func='WITH _pgh_attached_context AS ( SELECT _pgh_attach_context() AS value ) INSERT INTO "tests_eventmodelstatementevent" ("dt_field", "id", "int_field", "pgh_context_id", "pgh_created_at", "pgh_label", "pgh_obj_id") SELECT new_values."dt_field", new_values."id", new_values."int_field", _pgh_attached_context.value, NOW(), \'after_update\', new_values."id" FROM ( SELECT new_values.* FROM old_values JOIN new_values ON (old_values."id") = (new_values."id") WHERE (old_values."dt_field" IS DISTINCT FROM (new_values."dt_field")) ) AS new_values, _pgh_attached_context; RETURN NULL;',  # noqa: E501
                    hash="8c16786639e976048b250fd90d4c6fac3a4490cc",
                    level="STATEMENT",
                    operation="UPDATE",
                    pgid="pgtrigger_after_update_update_d16b9",
                    referencing="REFERENCING OLD TABLE AS old_values  NEW TABLE AS new_values ",
                    table="tests_eventmodelstatement",
                    when="AFTER",
                ),
            ),
        ),
        pgtrigger.migrations.AddTrigger(
            model_name="eventmodelstatementevent",
            trigger=pgtrigger.compiler.Trigger(
                name="append_only",
                sql=pgtrigger.compiler.UpsertTriggerSql(
                    func="RAISE EXCEPTION 'pgtrigger: Cannot update or delete rows from % table', TG_TABLE_NAME;",  # noqa: E501
                    hash="2de6a41a6a37d02cf8c5477601a50516e4e9fb98",
                    operation="UPDATE OR DELETE",
                    pgid="pgtrigger_append_only_859aa",
                    table="tests_eventmodelstatementevent",
                    when="BEFORE",
                ),
            ),
        ),
        pgtrigger.migrations.AddTrigger(
            model_name="nopghobjeventstatement",
            trigger=pgtrigger.compiler.Trigger(
                name="append_only",
                sql=pgtrigger.compiler.UpsertTriggerSql(
                    func="RAISE EXCEPTION 'pgtrigger: Cannot update or delete rows from % table', TG_TABLE_NAME;",  # noqa: E501
                    hash="bbc9dd58ecd8a366691bba724fd66824dc9e9aad",
                    operation="UPDATE OR DELETE",
                    pgid="pgtrigger_append_only_46e0e",
                    table="tests_nopghobjeventstatement",
                    when="BEFORE",
                ),
            ),
        ),
        migrations.AddField(
            model_name="condstatementevent",
            name="pgh_context",
            field=models.ForeignKey(
                db_constraint=False,
                null=True,
                on_delete=django.db.models.deletion.DO_NOTHING,
                related_name="+",
                to="pghistory.context",
            ),
        ),
        migrations.AddField(
            model_name="condstatementevent",
            name="pgh_obj",
            field=models.ForeignKey(
                db_constraint=False,
                on_delete=django.db.models.deletion.DO_NOTHING,
                related_name="events",
                to="tests.condstatement",
            ),
        ),
        pgtrigger.migrations.AddTrigger(
            model_name="condstatement",
            trigger=pgtrigger.compiler.Trigger(
                name="int_field1_created_insert",
                sql=pgtrigger.compiler.UpsertTriggerSql(
                    func='WITH _pgh_attached_context AS ( SELECT _pgh_attach_context() AS value ) INSERT INTO "tests_condstatementevent" ("id", "int_field1", "int_field2", "pgh_context_id", "pgh_created_at", "pgh_label", "pgh_obj_id") SELECT new_values."id", new_values."int_field1", new_values."int_field2", _pgh_attached_context.value, NOW(), \'int_field1_created\', new_values."id" FROM ( SELECT new_values.* FROM new_values WHERE (new_values."int_field1" > 100) ) AS new_values, _pgh_attached_context; RETURN NULL;',  # noqa: E501
                    hash="86528417f79cdf0e26825fc24883a111c421c491",
                    level="STATEMENT",
                    operation="INSERT",
                    pgid="pgtrigger_int_field1_created_insert_52c9c",
                    referencing="REFERENCING NEW TABLE AS new_values ",
                    table="tests_condstatement",
                    when="AFTER",
                ),
            ),
        ),
        pgtrigger.migrations.AddTrigger(
            model_name="condstatement",
            trigger=pgtrigger.compiler.Trigger(
                name="int_field1_updated_update",
                sql=pgtrigger.compiler.UpsertTriggerSql(
                    func='WITH _pgh_attached_context AS ( SELECT _pgh_attach_context() AS value ) INSERT INTO "tests_condstatementevent" ("id", "int_field1", "int_field2", "pgh_context_id", "pgh_created_at", "pgh_label", "pgh_obj_id") SELECT new_values."id", new_values."int_field1", new_values."int_field2", _pgh_attached_context.value, NOW(), \'int_field1_updated\', new_values."id" FROM ( SELECT new_values.* FROM old_values JOIN new_values ON (old_values."id") = (new_values."id") WHERE (old_values."int_field1" IS DISTINCT FROM (new_values."int_field1")) ) AS new_values, _pgh_attached_context; RETURN NULL;',  # noqa: E501
                    hash="3f3561b1dd77f1a41889320a4c886e4692c0a27c",
                    level="STATEMENT",
                    operation="UPDATE",
                    pgid="pgtrigger_int_field1_updated_update_1a61e",
                    referencing="REFERENCING OLD TABLE AS old_values  NEW TABLE AS new_values ",
                    table="tests_condstatement",
                    when="AFTER",
                ),
            ),
        ),
        pgtrigger.migrations.AddTrigger(
            model_name="condstatement",
            trigger=pgtrigger.compiler.Trigger(
                name="int_field2_incr_update",
                sql=pgtrigger.compiler.UpsertTriggerSql(
                    func='WITH _pgh_attached_context AS ( SELECT _pgh_attach_context() AS value ) INSERT INTO "tests_condstatementevent" ("id", "int_field1", "int_field2", "pgh_context_id", "pgh_created_at", "pgh_label", "pgh_obj_id") SELECT new_values."id", new_values."int_field1", new_values."int_field2", _pgh_attached_context.value, NOW(), \'int_field2_incr\', new_values."id" FROM ( SELECT new_values.* FROM old_values JOIN new_values ON (old_values."id") = (new_values."id") WHERE (old_values."int_field2" < (new_values."int_field2")) ) AS new_values, _pgh_attached_context; RETURN NULL;',  # noqa: E501
                    hash="485650b6eb060ccaccf55ee5b14ddb72761bf64a",
                    level="STATEMENT",
                    operation="UPDATE",
                    pgid="pgtrigger_int_field2_incr_update_8fd5d",
                    referencing="REFERENCING OLD TABLE AS old_values  NEW TABLE AS new_values ",
                    table="tests_condstatement",
                    when="AFTER",
                ),
            ),
        ),
        pgtrigger.migrations.AddTrigger(
            model_name="condstatement",
            trigger=pgtrigger.compiler.Trigger(
                name="int_field1_deleted_delete",
                sql=pgtrigger.compiler.UpsertTriggerSql(
                    func='WITH _pgh_attached_context AS ( SELECT _pgh_attach_context() AS value ) INSERT INTO "tests_condstatementevent" ("id", "int_field1", "int_field2", "pgh_context_id", "pgh_created_at", "pgh_label", "pgh_obj_id") SELECT old_values."id", old_values."int_field1", old_values."int_field2", _pgh_attached_context.value, NOW(), \'int_field1_deleted\', old_values."id" FROM ( SELECT old_values.* FROM old_values WHERE (old_values."int_field1" < 50) ) AS old_values, _pgh_attached_context; RETURN NULL;',  # noqa: E501
                    hash="3752fc77fe768e346e8c30672bb5a5869bdd5441",
                    level="STATEMENT",
                    operation="DELETE",
                    pgid="pgtrigger_int_field1_deleted_delete_89fe5",
                    referencing="REFERENCING OLD TABLE AS old_values ",
                    table="tests_condstatement",
                    when="AFTER",
                ),
            ),
        ),
    ]
