# Generated by Django 4.2.23 on 2025-11-06 10:26

from django.db import (
    migrations,
    models,
)
from django.db.utils import (
    DEFAULT_DB_ALIAS,
)

from edu_rdm_integration.stages.upload_data.models import (
    RDMExportingDataSubStageUploaderClientLog,
    RDMUploadStatusRequestLog,
)


def copy_entry(apps, schema_editor):
    """Копирование данных из поля entry_id во временное поле temp_entry_id."""

    # Проверяем, что применяем для основной БД
    if schema_editor.connection.alias == DEFAULT_DB_ALIAS:
        with schema_editor.connection.cursor() as cursor:
            cursor.execute(
                f"""
                UPDATE {RDMExportingDataSubStageUploaderClientLog._meta.db_table}
                SET temp_entry_id=entry_id;

                UPDATE {RDMUploadStatusRequestLog._meta.db_table}
                SET temp_entry_id=entry_id;
                """
            )


class Migration(migrations.Migration):
    dependencies = [
        (
            'edu_rdm_integration_upload_data_stage',
            '0005_rdmexportingdatasubstageuploaderclientlog_temp_entry_id_and_more',
        ),
    ]

    operations = [
        migrations.RunPython(copy_entry, reverse_code=migrations.RunPython.noop),
    ]
