# 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.collect_data.models import (
    RDMCollectingDataCommandProgress,
    RDMCollectingDataStage,
    RDMCollectingDataSubStage,
)


def copy_task_manager_function(apps, schema_editor):
    """Копирование данных полей task, manager и function в новые поля не являющиеся FK."""

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

                UPDATE {RDMCollectingDataSubStage._meta.db_table}
                SET temp_function_id=function_id;

                UPDATE {RDMCollectingDataStage._meta.db_table}
                SET temp_manager_id=manager_id;
                """
            )


class Migration(migrations.Migration):
    dependencies = [
        ('edu_rdm_integration_collect_data_stage', '0007_rdmcollectingdatacommandprogress_temp_task_id_and_more'),
    ]

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