# Generated by Django 3.2.13 on 2023-01-16 16:51

from django.db import migrations, models


class Migration(migrations.Migration):

    """
    This migration handles a special case during the "group" to "workspace" rename.

    When `TrashEntry.group` is renamed to `workspace`, we *don't* want
    to re-create the index, which has `group` in its `fields`.

    Unfortunately setting the `Index`'s `name` to the original `name` doesn't work,
    as Django will still want to drop the index, rename the field, and then re-create
    the index afterwards.

    This migration accomplishes two tasks:

    1. In `database_operations` it handles the forwards/backwards migration to rename
       the `group` column to/from `workspace`.
    2. In `state_operations` we inform Django that the state is changing so that the
       index is dropped, the field is renamed, and the index is recreated with a `name`
       that matches the fields in `fields`.

    """

    dependencies = [
        ("core", "0052_rename_group_template_workspace"),
    ]

    forwards = """
        ALTER TABLE core_trashentry
        RENAME COLUMN group_id TO workspace_id;
        ALTER TABLE core_trashentry
            DROP CONSTRAINT core_trashentry_group_id_ed3bd1dd_fk_core_workspace_id,
            ADD CONSTRAINT core_trashentry_workspace_id_4c567279_fk_core_workspace_id
            FOREIGN KEY (workspace_id) REFERENCES core_workspace(id)
            DEFERRABLE INITIALLY DEFERRED;
        ALTER INDEX core_trashentry_group_id_ed3bd1dd
        RENAME TO core_trashentry_workspace_id_ed3bd1dd;
    """

    backwards = """
        ALTER TABLE core_trashentry
        RENAME COLUMN workspace_id TO group_id;
        ALTER TABLE core_trashentry
            DROP CONSTRAINT core_trashentry_workspace_id_4c567279_fk_core_workspace_id,
            ADD CONSTRAINT core_trashentry_group_id_ed3bd1dd_fk_core_workspace_id
            FOREIGN KEY (group_id) REFERENCES core_workspace(id)
            DEFERRABLE INITIALLY DEFERRED;
        ALTER INDEX core_trashentry_workspace_id_ed3bd1dd
        RENAME TO core_trashentry_group_id_ed3bd1dd;
    """

    operations = [
        migrations.SeparateDatabaseAndState(
            database_operations=[
                migrations.RunSQL(
                    sql=forwards,
                    reverse_sql=backwards,
                ),
            ],
            state_operations=[
                migrations.RemoveIndex(
                    model_name="trashentry",
                    name="core_trashe_trashed_0bf61a_idx",
                ),
                migrations.RenameField(
                    model_name="trashentry",
                    old_name="group",
                    new_name="workspace",
                ),
                migrations.AddIndex(
                    model_name="trashentry",
                    index=models.Index(
                        fields=[
                            "-trashed_at",
                            "trash_item_type",
                            "workspace",
                            "application",
                        ],
                        name="core_trashe_trashed_fd28ba_idx",
                    ),
                ),
            ],
        ),
    ]
