# Generated by Django 4.2.5 on 2023-09-10 16:44

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


class Migration(migrations.Migration):
    dependencies = [
        ("formkit_ninja", "0015_alter_formkitschema_label"),
    ]

    operations = [
        migrations.AlterModelOptions(
            name="nodechildren",
            options={"ordering": ("parent_id", "order")},
        ),
        migrations.AlterField(
            model_name="formcomponents",
            name="order",
            field=models.IntegerField(blank=True, null=True),
        ),
        migrations.AlterField(
            model_name="nodechildren",
            name="order",
            field=models.IntegerField(blank=True, null=True),
        ),
        migrations.AlterField(
            model_name="option",
            name="order",
            field=models.IntegerField(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="32de6ecbd50ae312fcc482cdba82ce45e9b76908",
                    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="e87c38bfcb4fa1b13b8f6f2aa5cb186fe831c8a3",
                    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_insert_option",
                sql=pgtrigger.compiler.UpsertTriggerSql(
                    func='NEW."order" = (SELECT coalesce(max("order"), 0) + 1 FROM formkit_ninja_nodechildren WHERE formkit_ninja_nodechildren."parent_id" = NEW."parent_id"); RETURN NEW;',
                    hash="c66dfe1401c425e23a30d21ed7cd257cfc02b5b5",
                    operation="INSERT",
                    pgid="pgtrigger_order_on_insert_option_1816c",
                    table="formkit_ninja_nodechildren",
                    when="BEFORE",
                ),
            ),
        ),
        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="a421dc6da53fa50d3226e4bb4802866879344941",
                    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_insert_option",
                sql=pgtrigger.compiler.UpsertTriggerSql(
                    func='NEW."order" = (SELECT coalesce(max("order"), 0) + 1 FROM formkit_ninja_option WHERE formkit_ninja_option."group_id" = NEW."group_id"); RETURN NEW;',
                    hash="eff471a7791c174bd5c48d6ebb6baad36aa108d9",
                    operation="INSERT",
                    pgid="pgtrigger_order_on_insert_option_e1443",
                    table="formkit_ninja_option",
                    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="ea7dde4b2c32fe380c79eaf105621171c396fa71",
                    operation="UPDATE",
                    pgid="pgtrigger_order_on_update_option_417cb",
                    table="formkit_ninja_option",
                    when="AFTER",
                ),
            ),
        ),
    ]
