# Generated by Django 5.2.8 on 2026-01-07

from django.db import migrations


def _table_exists(schema_editor, table_name):
    return table_name in schema_editor.connection.introspection.table_names()


def _column_exists(schema_editor, table_name, column_name):
    with schema_editor.connection.cursor() as cursor:
        columns = schema_editor.connection.introspection.get_table_description(cursor, table_name)
    return column_name in {column.name for column in columns}


def create_user_model_if_missing(apps, schema_editor):
    User = apps.get_model('accounts', 'User')
    table_name = User._meta.db_table
    if _table_exists(schema_editor, table_name):
        return
    schema_editor.create_model(User)


def add_entry_uuid_if_missing(apps, schema_editor):
    User = apps.get_model('accounts', 'User')
    table_name = User._meta.db_table
    if not _table_exists(schema_editor, table_name):
        return
    if _column_exists(schema_editor, table_name, 'entry_uuid'):
        return
    field = User._meta.get_field('entry_uuid')
    schema_editor.add_field(User, field)


class Migration(migrations.Migration):
    dependencies = [
        ('accounts', '0005_rename_usersettings_userprofile'),
        ('auth', '0012_alter_user_first_name_max_length'),
    ]

    operations = [
        migrations.RunPython(create_user_model_if_missing, migrations.RunPython.noop),
        migrations.RunPython(add_entry_uuid_if_missing, migrations.RunPython.noop),
    ]
