# Generated by Django 5.2 on 2025-02-16 21:50

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

import bionty.ids
from bionty._biorecord import encode_uid


def populate_uids(apps, schema_editor):
    Source = apps.get_model("bionty", "Source")
    for source in Source.objects.all():
        # Convert model instance to dictionary of fields
        kwargs = {
            field.name: getattr(source, field.name)
            for field in source._meta.fields
            if field.name != "uid"  # Exclude uid field itself
        }
        # Generate and save new uid
        source.uid = encode_uid(registry=Source, kwargs=kwargs)["uid"]
        source.save()


class Migration(migrations.Migration):
    dependencies = [
        ("bionty", "0049_alter_schemacellmarker_cellmarker_and_more"),
    ]

    operations = [
        migrations.AlterField(
            model_name="source",
            name="uid",
            field=lamindb.base.fields.CharField(
                blank=True, default=bionty.ids.source, max_length=8, unique=True
            ),
        ),
        migrations.RunPython(populate_uids, reverse_code=migrations.RunPython.noop),
    ]
