# Generated by Django 4.1.13 on 2025-08-14 06:39

import authentication.models.access_key
import common.db.fields
from django.db import migrations

old_access_key_secrets_mapper = {}

def fetch_access_key_secrets(apps, schema_editor):
    AccessKey = apps.get_model("authentication", "AccessKey")

    for id, secret in AccessKey.objects.all().values_list('id', 'secret'):
        old_access_key_secrets_mapper[str(id)] = secret


def save_access_key_secrets(apps, schema_editor):
    AccessKey = apps.get_model("authentication", "AccessKey")
    aks = AccessKey.objects.filter(id__in=list(old_access_key_secrets_mapper.keys()))
    for ak in aks:
        old_value = old_access_key_secrets_mapper.get(str(ak.id))
        if not old_value:
            continue
        ak.secret = old_value
        ak.save(update_fields=["secret"])

class Migration(migrations.Migration):

    dependencies = [
        ("authentication", "0007_connectiontoken_remote_addr"),
    ]

    operations = [
        migrations.RunPython(fetch_access_key_secrets),
        migrations.AlterField(
            model_name="accesskey",
            name="secret",
            field=common.db.fields.EncryptTextField(
                default=authentication.models.access_key.default_secret,
                verbose_name="AccessKeySecret",
            ),
        ),
        migrations.AlterField(
            model_name="temptoken",
            name="secret",
            field=common.db.fields.EncryptTextField(
                verbose_name="Secret"
            ),
        ),
        migrations.RunPython(save_access_key_secrets),
    ]
