# Generated by Django 2.0.8 on 2018-08-27 09:48

from django.db import migrations, models


def create_trigger(apps, schema_editor):
    if schema_editor.connection.vendor == "postgresql":
        schema_editor.execute(
            """
            CREATE OR REPLACE FUNCTION update_full_account_codes()
                RETURNS TRIGGER AS
            $$
            BEGIN
                -- Set empty string codes to be NULL
                UPDATE hordak_account SET code = NULL where code = '';

                -- Set full code to the combination of the parent account's codes
                UPDATE
                    hordak_account AS a
                SET
                    full_code = (
                        SELECT string_agg(code, '' order by lft)
                        FROM hordak_account AS a2
                        WHERE a2.lft <= a.lft AND a2.rght >= a.rght AND a.tree_id = a2.tree_id
                    );

                -- Set full codes to NULL where a parent account includes a NULL code
                UPDATE
                    hordak_account AS a
                SET
                    full_code = NULL
                WHERE
                    (
                        SELECT COUNT(*)
                        FROM hordak_account AS a2
                        WHERE a2.lft <= a.lft AND a2.rght >= a.rght AND a.tree_id = a2.tree_id AND a2.code IS NULL
                    ) > 0;
                RETURN NULL;
            END;
            $$
            LANGUAGE plpgsql;
        """
        )
    elif schema_editor.connection.vendor == "mysql":
        pass  # we don't care about MySQL here since support is added in 0027
    else:
        raise NotImplementedError(
            "Don't know how to create trigger for %s" % schema_editor.connection.vendor
        )


def drop_trigger(apps, schema_editor):
    if schema_editor.connection.vendor == "postgresql":
        # Recreate update_full_account_codes as it was in migration 0013
        schema_editor.execute(
            """
            CREATE OR REPLACE FUNCTION update_full_account_codes()
                RETURNS TRIGGER AS
            $$
            BEGIN
                UPDATE
                    hordak_account AS a
                SET
                    full_code = (
                        SELECT string_agg(code, '' order by lft)
                        FROM hordak_account AS a2
                        WHERE a2.lft <= a.lft AND a2.rght >= a.rght AND a.tree_id = a2.tree_id
                    );
                RETURN NULL;
            END;
            $$
            LANGUAGE plpgsql;
        """
        )
    elif schema_editor.connection.vendor == "mysql":
        pass
    else:
        raise NotImplementedError(
            "Don't know how to drop trigger for %s" % schema_editor.connection.vendor
        )


class Migration(migrations.Migration):
    dependencies = [("hordak", "0023_auto_20180825_1029")]

    operations = [
        migrations.AlterField(
            model_name="account",
            name="code",
            field=models.CharField(blank=True, max_length=3, null=True),
        ),
        migrations.AlterField(
            model_name="account",
            name="full_code",
            field=models.CharField(
                blank=True, db_index=True, max_length=100, null=True, unique=True
            ),
        ),
        migrations.RunPython(create_trigger, reverse_code=drop_trigger),
    ]
