# Generated by Django 5.0.3 on 2024-12-03 14:01

from django.db import migrations, models


def _migrate_from_old_column(apps, schema_editor) -> None:
    UpdateState = apps.get_model("django_telethon", "UpdateState")
    i = 0
    for update_state in UpdateState.objects.all():
        i += 1
        UpdateState.objects.filter(id=update_state.id).update(id=i, entity_id=update_state.id)

    # Step 2: Reset primary keys
    with schema_editor.connection.cursor() as cursor:
        cursor.execute(f"ALTER SEQUENCE {UpdateState._meta.db_table}_id_seq RESTART WITH {i +  1}")


class Migration(migrations.Migration):
    dependencies = [
        ('django_telethon', '0001_initial'),
    ]

    operations = [
        migrations.AddField(
            model_name='updatestate',
            name='entity_id',
            field=models.BigIntegerField(null=True),
        ),
        migrations.RunPython(
            _migrate_from_old_column,
            reverse_code=lambda x, y: None,
        ),
    ]
