# Generated by Django 4.2.5 on 2023-10-01 05:35

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


class Migration(migrations.Migration):
    dependencies = [
        ("formkit_ninja", "0017_nodechildren_change_id"),
    ]

    operations = [
        pgtrigger.migrations.RemoveTrigger(
            model_name="formcomponents",
            name="order_on_insert_option",
        ),
        pgtrigger.migrations.RemoveTrigger(
            model_name="formcomponents",
            name="order_on_update_option",
        ),
        pgtrigger.migrations.RemoveTrigger(
            model_name="nodechildren",
            name="order_on_update_option",
        ),
        pgtrigger.migrations.RemoveTrigger(
            model_name="option",
            name="order_on_update_option",
        ),
        migrations.AddField(
            model_name="nodechildren",
            name="track_change",
            field=models.BigIntegerField(blank=True, null=True),
        ),
        pgtrigger.migrations.AddTrigger(
            model_name="formcomponents",
            trigger=pgtrigger.compiler.Trigger(
                name="order_on_insert_option",
                sql=pgtrigger.compiler.UpsertTriggerSql(
                    func='NEW."order" = (SELECT coalesce(max("order"), 0) + 1 FROM formkit_ninja_formcomponents WHERE formkit_ninja_formcomponents."schema_id" = NEW."schema_id"); RETURN NEW;',
                    hash="77d4e855615a2d36545c807726d62f4bcd4c88c2",
                    operation="INSERT",
                    pgid="pgtrigger_order_on_insert_option_1fbd2",
                    table="formkit_ninja_formcomponents",
                    when="BEFORE",
                ),
            ),
        ),
        pgtrigger.migrations.AddTrigger(
            model_name="formcomponents",
            trigger=pgtrigger.compiler.Trigger(
                name="order_on_update_option",
                sql=pgtrigger.compiler.UpsertTriggerSql(
                    condition="WHEN (pg_trigger_depth() = 0)",
                    func='\n                -- Do not allow a "null" value\n                -- This stops Django from dumbly updating\n                -- which can break the trigger\n                if NEW."order" IS NULL then\n                    NEW."order" = OLD."order";\n                end if;\n                if NEW."order" > OLD."order" then\n                    update formkit_ninja_formcomponents\n                    set "order" = "order"- 1\n                    where "order" <= NEW."order"\n                    and "order" > OLD."order"\n                    and "schema_id" = OLD."schema_id"\n                    and "id" <> NEW."id";\n                else\n                    update formkit_ninja_formcomponents\n                    set "order" = "order"+ 1\n                    where "order" >= NEW."order"\n                    and "order" < OLD."order"\n                    and "schema_id" = OLD."schema_id"\n                    and "id" <> NEW."id";\n                end if;\n                RETURN NEW;\n        ',
                    hash="73f3c187b4413f399667c968c68f1964681f4b70",
                    operation="UPDATE",
                    pgid="pgtrigger_order_on_update_option_a5326",
                    table="formkit_ninja_formcomponents",
                    when="AFTER",
                ),
            ),
        ),
        pgtrigger.migrations.AddTrigger(
            model_name="nodechildren",
            trigger=pgtrigger.compiler.Trigger(
                name="order_on_update_option",
                sql=pgtrigger.compiler.UpsertTriggerSql(
                    condition="WHEN (pg_trigger_depth() = 0)",
                    func='\n                -- Do not allow a "null" value\n                -- This stops Django from dumbly updating\n                -- which can break the trigger\n                if NEW."order" IS NULL then\n                    NEW."order" = OLD."order";\n                end if;\n                if NEW."order" > OLD."order" then\n                    update formkit_ninja_nodechildren\n                    set "order" = "order"- 1\n                    where "order" <= NEW."order"\n                    and "order" > OLD."order"\n                    and "parent_id" = OLD."parent_id"\n                    and "id" <> NEW."id";\n                else\n                    update formkit_ninja_nodechildren\n                    set "order" = "order"+ 1\n                    where "order" >= NEW."order"\n                    and "order" < OLD."order"\n                    and "parent_id" = OLD."parent_id"\n                    and "id" <> NEW."id";\n                end if;\n                RETURN NEW;\n        ',
                    hash="1e4478cd11d7361fe36817fdc7d0bc79a06f43ee",
                    operation="UPDATE",
                    pgid="pgtrigger_order_on_update_option_44228",
                    table="formkit_ninja_nodechildren",
                    when="AFTER",
                ),
            ),
        ),
        pgtrigger.migrations.AddTrigger(
            model_name="nodechildren",
            trigger=pgtrigger.compiler.Trigger(
                name="version_on_update",
                sql=pgtrigger.compiler.UpsertTriggerSql(
                    func="NEW.\"track_change\" = nextval('nodechildren_change_id'); RETURN NEW;",
                    hash="555bcf98d37b335497f307dd7bbb258fae562109",
                    operation="UPDATE OR INSERT",
                    pgid="pgtrigger_version_on_update_9c530",
                    table="formkit_ninja_nodechildren",
                    when="BEFORE",
                ),
            ),
        ),
        pgtrigger.migrations.AddTrigger(
            model_name="option",
            trigger=pgtrigger.compiler.Trigger(
                name="order_on_update_option",
                sql=pgtrigger.compiler.UpsertTriggerSql(
                    condition="WHEN (pg_trigger_depth() = 0)",
                    func='\n                -- Do not allow a "null" value\n                -- This stops Django from dumbly updating\n                -- which can break the trigger\n                if NEW."order" IS NULL then\n                    NEW."order" = OLD."order";\n                end if;\n                if NEW."order" > OLD."order" then\n                    update formkit_ninja_option\n                    set "order" = "order"- 1\n                    where "order" <= NEW."order"\n                    and "order" > OLD."order"\n                    and "group_id" = OLD."group_id"\n                    and "id" <> NEW."id";\n                else\n                    update formkit_ninja_option\n                    set "order" = "order"+ 1\n                    where "order" >= NEW."order"\n                    and "order" < OLD."order"\n                    and "group_id" = OLD."group_id"\n                    and "id" <> NEW."id";\n                end if;\n                RETURN NEW;\n        ',
                    hash="9c254236d010611cd7d5a2bb01b971e1015781ca",
                    operation="UPDATE",
                    pgid="pgtrigger_order_on_update_option_417cb",
                    table="formkit_ninja_option",
                    when="AFTER",
                ),
            ),
        ),
    ]
