# Generated by Django 5.2 on 2026-01-05 11:15

import django.db.models.deletion
import lamindb.base.fields
from django.db import migrations


def rename_branch_columns(apps, schema_editor):
    """Rename _branch_code to branch_id across all models."""
    tables = [
        "wetlab_biologic",
        "wetlab_biosample",
        "wetlab_combinationperturbation",
        "wetlab_compound",
        "wetlab_compoundperturbation",
        "wetlab_donor",
        "wetlab_environmentalperturbation",
        "wetlab_experiment",
        "wetlab_geneticperturbation",
        "wetlab_perturbationtarget",
        "wetlab_techsample",
        "wetlab_well",
    ]

    with schema_editor.connection.cursor() as cursor:
        if schema_editor.connection.vendor == "postgresql":
            for table in tables:
                cursor.execute(
                    f"ALTER TABLE {table} RENAME COLUMN _branch_code TO branch_id;"
                )

        elif schema_editor.connection.vendor == "sqlite":
            # SQLite 3.25.0+ supports ALTER TABLE RENAME COLUMN
            for table in tables:
                try:
                    cursor.execute(
                        f"ALTER TABLE {table} RENAME COLUMN _branch_code TO branch_id;"
                    )
                except Exception as error:
                    raise NotImplementedError(
                        "This migration requires SQLite 3.25.0 or newer. "
                        "Please upgrade SQLite or manually recreate the tables."
                    ) from error


class Migration(migrations.Migration):
    dependencies = [
        ("lamindb", "0161_rename_branch_code_to_branch_id"),
        ("wetlab", "0047_artifactperturbationtarget_wetlab_arti_artifac_2674c5_idx"),
    ]

    operations = [
        # Rename the columns at the database level
        migrations.RunPython(
            rename_branch_columns,
            migrations.RunPython.noop,
        ),
        migrations.RemoveField(
            model_name="artifactbiologic",
            name="feature_ref_is_name",
        ),
        migrations.RemoveField(
            model_name="artifactbiologic",
            name="label_ref_is_name",
        ),
        migrations.RemoveField(
            model_name="artifactbiosample",
            name="feature_ref_is_name",
        ),
        migrations.RemoveField(
            model_name="artifactbiosample",
            name="label_ref_is_name",
        ),
        migrations.RemoveField(
            model_name="artifactcombinationperturbation",
            name="feature_ref_is_name",
        ),
        migrations.RemoveField(
            model_name="artifactcombinationperturbation",
            name="label_ref_is_name",
        ),
        migrations.RemoveField(
            model_name="artifactcompound",
            name="feature_ref_is_name",
        ),
        migrations.RemoveField(
            model_name="artifactcompound",
            name="label_ref_is_name",
        ),
        migrations.RemoveField(
            model_name="artifactcompoundperturbation",
            name="feature_ref_is_name",
        ),
        migrations.RemoveField(
            model_name="artifactcompoundperturbation",
            name="label_ref_is_name",
        ),
        migrations.RemoveField(
            model_name="artifactdonor",
            name="feature_ref_is_name",
        ),
        migrations.RemoveField(
            model_name="artifactdonor",
            name="label_ref_is_name",
        ),
        migrations.RemoveField(
            model_name="artifactenvironmentalperturbation",
            name="feature_ref_is_name",
        ),
        migrations.RemoveField(
            model_name="artifactenvironmentalperturbation",
            name="label_ref_is_name",
        ),
        migrations.RemoveField(
            model_name="artifactexperiment",
            name="feature_ref_is_name",
        ),
        migrations.RemoveField(
            model_name="artifactexperiment",
            name="label_ref_is_name",
        ),
        migrations.RemoveField(
            model_name="artifactgeneticperturbation",
            name="feature_ref_is_name",
        ),
        migrations.RemoveField(
            model_name="artifactgeneticperturbation",
            name="label_ref_is_name",
        ),
        migrations.RemoveField(
            model_name="artifactperturbationtarget",
            name="feature_ref_is_name",
        ),
        migrations.RemoveField(
            model_name="artifactperturbationtarget",
            name="label_ref_is_name",
        ),
        migrations.RemoveField(
            model_name="artifacttechsample",
            name="feature_ref_is_name",
        ),
        migrations.RemoveField(
            model_name="artifacttechsample",
            name="label_ref_is_name",
        ),
        migrations.RemoveField(
            model_name="artifactwell",
            name="feature_ref_is_name",
        ),
        migrations.RemoveField(
            model_name="artifactwell",
            name="label_ref_is_name",
        ),
        # Update Django's state only (no database operations)
        migrations.SeparateDatabaseAndState(
            database_operations=[],
            state_operations=[
                migrations.AlterField(
                    model_name="biologic",
                    name="branch",
                    field=lamindb.base.fields.ForeignKey(
                        blank=True,
                        db_column="branch_id",
                        db_default=1,
                        default=1,
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="+",
                        to="lamindb.branch",
                    ),
                ),
                migrations.AlterField(
                    model_name="biosample",
                    name="branch",
                    field=lamindb.base.fields.ForeignKey(
                        blank=True,
                        db_column="branch_id",
                        db_default=1,
                        default=1,
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="+",
                        to="lamindb.branch",
                    ),
                ),
                migrations.AlterField(
                    model_name="combinationperturbation",
                    name="branch",
                    field=lamindb.base.fields.ForeignKey(
                        blank=True,
                        db_column="branch_id",
                        db_default=1,
                        default=1,
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="+",
                        to="lamindb.branch",
                    ),
                ),
                migrations.AlterField(
                    model_name="compound",
                    name="branch",
                    field=lamindb.base.fields.ForeignKey(
                        blank=True,
                        db_column="branch_id",
                        db_default=1,
                        default=1,
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="+",
                        to="lamindb.branch",
                    ),
                ),
                migrations.AlterField(
                    model_name="compoundperturbation",
                    name="branch",
                    field=lamindb.base.fields.ForeignKey(
                        blank=True,
                        db_column="branch_id",
                        db_default=1,
                        default=1,
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="+",
                        to="lamindb.branch",
                    ),
                ),
                migrations.AlterField(
                    model_name="donor",
                    name="branch",
                    field=lamindb.base.fields.ForeignKey(
                        blank=True,
                        db_column="branch_id",
                        db_default=1,
                        default=1,
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="+",
                        to="lamindb.branch",
                    ),
                ),
                migrations.AlterField(
                    model_name="environmentalperturbation",
                    name="branch",
                    field=lamindb.base.fields.ForeignKey(
                        blank=True,
                        db_column="branch_id",
                        db_default=1,
                        default=1,
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="+",
                        to="lamindb.branch",
                    ),
                ),
                migrations.AlterField(
                    model_name="experiment",
                    name="branch",
                    field=lamindb.base.fields.ForeignKey(
                        blank=True,
                        db_column="branch_id",
                        db_default=1,
                        default=1,
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="+",
                        to="lamindb.branch",
                    ),
                ),
                migrations.AlterField(
                    model_name="geneticperturbation",
                    name="branch",
                    field=lamindb.base.fields.ForeignKey(
                        blank=True,
                        db_column="branch_id",
                        db_default=1,
                        default=1,
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="+",
                        to="lamindb.branch",
                    ),
                ),
                migrations.AlterField(
                    model_name="perturbationtarget",
                    name="branch",
                    field=lamindb.base.fields.ForeignKey(
                        blank=True,
                        db_column="branch_id",
                        db_default=1,
                        default=1,
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="+",
                        to="lamindb.branch",
                    ),
                ),
                migrations.AlterField(
                    model_name="techsample",
                    name="branch",
                    field=lamindb.base.fields.ForeignKey(
                        blank=True,
                        db_column="branch_id",
                        db_default=1,
                        default=1,
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="+",
                        to="lamindb.branch",
                    ),
                ),
                migrations.AlterField(
                    model_name="well",
                    name="branch",
                    field=lamindb.base.fields.ForeignKey(
                        blank=True,
                        db_column="branch_id",
                        db_default=1,
                        default=1,
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="+",
                        to="lamindb.branch",
                    ),
                ),
            ],
        ),
    ]
