# Generated by Django 4.2.5 on 2023-10-03 00:08

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


class Migration(migrations.Migration):
    dependencies = [
        ("formkit_ninja", "0019_formkitschemanode_track_change_and_more"),
    ]

    operations = [
        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",
        ),
        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" = NEW."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" = NEW."schema_id"\n                    and "id" <> NEW."id";\n                end if;\n                RETURN NEW;\n        ',
                    hash="432443481a60d58c40ceb3204bda0e3ad58ce9aa",
                    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" = NEW."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" = NEW."parent_id"\n                    and "id" <> NEW."id";\n                end if;\n                RETURN NEW;\n        ',
                    hash="f8eaa7c49b5c81e7b0fa3830557746f40e2498bd",
                    operation="UPDATE",
                    pgid="pgtrigger_order_on_update_option_44228",
                    table="formkit_ninja_nodechildren",
                    when="AFTER",
                ),
            ),
        ),
        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" = NEW."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" = NEW."group_id"\n                    and "id" <> NEW."id";\n                end if;\n                RETURN NEW;\n        ',
                    hash="3f44c5db88a1344000e61868009e4b424d7c29ef",
                    operation="UPDATE",
                    pgid="pgtrigger_order_on_update_option_417cb",
                    table="formkit_ninja_option",
                    when="AFTER",
                ),
            ),
        ),
    ]
