# Generated by Django 3.1.2 on 2020-10-18 11:58
from django.apps.registry import Apps
from django.db import migrations, models
from django.db.backends.base.schema import BaseDatabaseSchemaEditor

import authentik.core.models


def set_default_token_key(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
    db_alias = schema_editor.connection.alias
    Token = apps.get_model("authentik_core", "Token")

    for token in Token.objects.using(db_alias).all():
        token.key = token.pk.hex
        token.save()


class Migration(migrations.Migration):

    dependencies = [
        ("authentik_core", "0013_auto_20201003_2132"),
    ]

    operations = [
        migrations.AddField(
            model_name="token",
            name="key",
            field=models.TextField(default=authentik.core.models.default_token_key),
        ),
        migrations.AlterUniqueTogether(
            name="token",
            unique_together=set(),
        ),
        migrations.AlterField(
            model_name="token",
            name="identifier",
            field=models.SlugField(max_length=255),
        ),
        migrations.AddIndex(
            model_name="token",
            index=models.Index(fields=["key"], name="authentik_co_key_e45007_idx"),
        ),
        migrations.AddIndex(
            model_name="token",
            index=models.Index(
                fields=["identifier"], name="authentik_co_identif_1a34a8_idx"
            ),
        ),
        migrations.RunPython(set_default_token_key),
    ]
