# Generated by Django 3.2.16 on 2022-10-10 20:39
# flake8: noqa

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


class Migration(migrations.Migration):
    dependencies = [
        ("pghistory", "0005_events_middlewareevents"),
        ("tests", "0003_customeventwithcontext_denormcontext_and_more"),
    ]

    operations = [
        migrations.CreateModel(
            name="SnapshotImageField",
            fields=[
                (
                    "id",
                    models.AutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("img_field", models.ImageField(upload_to="")),
            ],
        ),
        migrations.CreateModel(
            name="SnapshotImageFieldEvent",
            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()),
                ("img_field", models.ImageField(upload_to="")),
            ],
            options={
                "abstract": False,
            },
        ),
        pgtrigger.migrations.RemoveTrigger(
            model_name="custommodel",
            name="int_field_updated",
        ),
        pgtrigger.migrations.RemoveTrigger(
            model_name="eventmodel",
            name="after_update",
        ),
        pgtrigger.migrations.RemoveTrigger(
            model_name="snapshotmodel",
            name="dt_field_int_field_snapshot_update",
        ),
        pgtrigger.migrations.RemoveTrigger(
            model_name="snapshotmodel",
            name="dt_field_snapshot_update",
        ),
        pgtrigger.migrations.RemoveTrigger(
            model_name="snapshotmodel",
            name="custom_snapshot_update",
        ),
        pgtrigger.migrations.AddTrigger(
            model_name="custommodel",
            trigger=pgtrigger.compiler.Trigger(
                name="int_field_updated",
                sql=pgtrigger.compiler.UpsertTriggerSql(
                    condition='WHEN (OLD."integer_field" IS DISTINCT FROM NEW."integer_field")',
                    func='INSERT INTO "tests_custommodelevent" ("integer_field", "my_pk", "pgh_context_id", "pgh_created_at", "pgh_label", "pgh_obj_id") VALUES (NEW."integer_field", NEW."my_pk", _pgh_attach_context(), NOW(), \'int_field_updated\', NEW."my_pk"); RETURN NULL;',
                    hash="27914f913dc211bd459c9df206abad31bf36e2e7",
                    operation="UPDATE",
                    pgid="pgtrigger_int_field_updated_a1031",
                    table="tests_custommodel",
                    when="AFTER",
                ),
            ),
        ),
        pgtrigger.migrations.AddTrigger(
            model_name="eventmodel",
            trigger=pgtrigger.compiler.Trigger(
                name="after_update",
                sql=pgtrigger.compiler.UpsertTriggerSql(
                    condition='WHEN (OLD."dt_field" IS DISTINCT FROM NEW."dt_field")',
                    func='INSERT INTO "tests_eventmodelevent" ("dt_field", "id", "int_field", "pgh_context_id", "pgh_created_at", "pgh_label", "pgh_obj_id") VALUES (NEW."dt_field", NEW."id", NEW."int_field", _pgh_attach_context(), NOW(), \'after_update\', NEW."id"); RETURN NULL;',
                    hash="3330db56b51bca0d84933627c942bab95eada0fa",
                    operation="UPDATE",
                    pgid="pgtrigger_after_update_5b8c8",
                    table="tests_eventmodel",
                    when="AFTER",
                ),
            ),
        ),
        pgtrigger.migrations.AddTrigger(
            model_name="snapshotmodel",
            trigger=pgtrigger.compiler.Trigger(
                name="dt_field_int_field_snapshot_update",
                sql=pgtrigger.compiler.UpsertTriggerSql(
                    condition='WHEN (OLD."dt_field" IS DISTINCT FROM NEW."dt_field" OR OLD."int_field" IS DISTINCT FROM NEW."int_field")',
                    func='INSERT INTO "tests_snapshotmodeldtfieldintfieldevent" ("dt_field", "int_field", "pgh_context_id", "pgh_created_at", "pgh_label", "pgh_obj_id") VALUES (NEW."dt_field", NEW."int_field", _pgh_attach_context(), NOW(), \'dt_field_int_field_snapshot\', NEW."id"); RETURN NULL;',
                    hash="4d377399a19b3ae77ff226961197a38617f2fc11",
                    operation="UPDATE",
                    pgid="pgtrigger_dt_field_int_field_snapshot_update_8246d",
                    table="tests_snapshotmodel",
                    when="AFTER",
                ),
            ),
        ),
        pgtrigger.migrations.AddTrigger(
            model_name="snapshotmodel",
            trigger=pgtrigger.compiler.Trigger(
                name="dt_field_snapshot_update",
                sql=pgtrigger.compiler.UpsertTriggerSql(
                    condition='WHEN (OLD."dt_field" IS DISTINCT FROM NEW."dt_field")',
                    func='INSERT INTO "tests_snapshotmodeldtfieldevent" ("dt_field", "pgh_context_id", "pgh_created_at", "pgh_label", "pgh_obj_id") VALUES (NEW."dt_field", _pgh_attach_context(), NOW(), \'dt_field_snapshot\', NEW."id"); RETURN NULL;',
                    hash="31bab347c9e5c370c5637644013c0e9a691dc365",
                    operation="UPDATE",
                    pgid="pgtrigger_dt_field_snapshot_update_22f55",
                    table="tests_snapshotmodel",
                    when="AFTER",
                ),
            ),
        ),
        pgtrigger.migrations.AddTrigger(
            model_name="snapshotmodel",
            trigger=pgtrigger.compiler.Trigger(
                name="custom_snapshot_update",
                sql=pgtrigger.compiler.UpsertTriggerSql(
                    condition='WHEN (OLD."id" IS DISTINCT FROM NEW."id" OR OLD."int_field" IS DISTINCT FROM NEW."int_field" OR OLD."fk_field_id" IS DISTINCT FROM NEW."fk_field_id")',
                    func='INSERT INTO "tests_customsnapshotmodel" ("fk_field_id", "id", "int_field", "pgh_created_at", "pgh_label", "pgh_obj_id") VALUES (NEW."fk_field_id", NEW."id", NEW."int_field", NOW(), \'custom_snapshot\', NEW."id"); RETURN NULL;',
                    hash="f92a06b7747d0a2e649c8f39be8d917052dc7da7",
                    operation="UPDATE",
                    pgid="pgtrigger_custom_snapshot_update_5af3e",
                    table="tests_snapshotmodel",
                    when="AFTER",
                ),
            ),
        ),
        migrations.AddField(
            model_name="snapshotimagefieldevent",
            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="snapshotimagefieldevent",
            name="pgh_obj",
            field=models.ForeignKey(
                db_constraint=False,
                on_delete=django.db.models.deletion.DO_NOTHING,
                related_name="event",
                to="tests.snapshotimagefield",
            ),
        ),
        pgtrigger.migrations.AddTrigger(
            model_name="snapshotimagefield",
            trigger=pgtrigger.compiler.Trigger(
                name="snapshot_insert",
                sql=pgtrigger.compiler.UpsertTriggerSql(
                    func='INSERT INTO "tests_snapshotimagefieldevent" ("id", "img_field", "pgh_context_id", "pgh_created_at", "pgh_label", "pgh_obj_id") VALUES (NEW."id", NEW."img_field", _pgh_attach_context(), NOW(), \'snapshot\', NEW."id"); RETURN NULL;',
                    hash="3fabe63aa36a27866ed3bbae37e0597bfaa7cf35",
                    operation="INSERT",
                    pgid="pgtrigger_snapshot_insert_da4ed",
                    table="tests_snapshotimagefield",
                    when="AFTER",
                ),
            ),
        ),
        pgtrigger.migrations.AddTrigger(
            model_name="snapshotimagefield",
            trigger=pgtrigger.compiler.Trigger(
                name="snapshot_update",
                sql=pgtrigger.compiler.UpsertTriggerSql(
                    condition="WHEN (OLD.* IS DISTINCT FROM NEW.*)",
                    func='INSERT INTO "tests_snapshotimagefieldevent" ("id", "img_field", "pgh_context_id", "pgh_created_at", "pgh_label", "pgh_obj_id") VALUES (NEW."id", NEW."img_field", _pgh_attach_context(), NOW(), \'snapshot\', NEW."id"); RETURN NULL;',
                    hash="f52c9866bb44f6a1337f3ac529272c1b575440b8",
                    operation="UPDATE",
                    pgid="pgtrigger_snapshot_update_ee078",
                    table="tests_snapshotimagefield",
                    when="AFTER",
                ),
            ),
        ),
    ]
