# Generated by Django 3.1.7 on 2021-10-12 07:07

from django.db import migrations, models

from thunderstore.account.tokens import (
    get_service_account_api_token,
    hash_service_account_api_token,
)


def set_default(apps, schema_editor):
    ServiceAccount = apps.get_model("account", "ServiceAccount")
    objs = list(ServiceAccount.objects.all())

    for obj in objs:
        obj.api_token = hash_service_account_api_token(get_service_account_api_token())

    ServiceAccount.objects.bulk_update(objs, ["api_token"])


def reverse_set_default(apps, schema_editor):
    pass


class Migration(migrations.Migration):

    dependencies = [
        ("account", "0001_add_serviceaccount"),
    ]

    operations = [
        migrations.AddField(
            model_name="serviceaccount",
            name="api_token",
            field=models.CharField(null=True, max_length=255),
        ),
        migrations.RunPython(set_default, reverse_set_default),
        migrations.AlterField(
            model_name="serviceaccount",
            name="api_token",
            field=models.CharField(max_length=255, unique=True),
        ),
    ]
