# Generated by Django 5.1.5 on 2025-01-15 17:10

import oauth2_provider.models
from django.db import migrations, models

import oauth2_provider.models
from django.db import migrations, models
from oauth2_provider.settings import oauth2_settings

# see https://github.com/jazzband/django-oauth-toolkit/blob/master/oauth2_provider/migrations/0012_add_token_checksum.py


def forwards_func(apps, schema_editor):
    """
    Forward migration touches every "old" accesstoken.token which will cause the checksum to be computed.
    """
    AccessToken = apps.get_model(oauth2_settings.ACCESS_TOKEN_MODEL)
    accesstokens = AccessToken._default_manager.iterator()
    for accesstoken in accesstokens:
        accesstoken.save(update_fields=["token_checksum"])


class Migration(migrations.Migration):
    dependencies = [
        ("users", "0025_application_allowed_origins_and_more"),
    ]

    operations = [
        migrations.AddField(
            model_name="accesstoken",
            name="token_checksum",
            field=oauth2_provider.models.TokenChecksumField(
                blank=True, null=True, max_length=64
            ),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name="refreshtoken",
            name="token_family",
            field=models.UUIDField(blank=True, editable=False, null=True),
        ),
        migrations.AlterField(
            model_name="accesstoken",
            name="token",
            field=models.TextField(),
        ),
        migrations.RunPython(forwards_func, migrations.RunPython.noop),
        migrations.AlterField(
            model_name="accesstoken",
            name="token_checksum",
            field=oauth2_provider.models.TokenChecksumField(
                blank=False, max_length=64, db_index=True, unique=True
            ),
        ),
    ]
