# Generated by Django 5.0.13 on 2025-03-13 07:51

import django.db.models.deletion
from django.db import migrations, models
from tqdm import tqdm
def migrate_risk_check(apps, schema_editor):

    RiskCheck = apps.get_model("wbcompliance", "RiskCheck")
    objs = []
    qs = RiskCheck.objects.all()
    for check in tqdm(qs, total=qs.count()):
        check.rule_id = check.rule_checked_object_relationship.rule.id
        check.checked_object_content_type_id = check.rule_checked_object_relationship.checked_object_content_type.id
        check.checked_object_id = check.rule_checked_object_relationship.checked_object_id
        check.save()
    #     objs.append(check)
    #     if len(objs) > 1000:
    #         RiskCheck.objects.bulk_update(objs, ["rule_id", "checked_object_content_type_id", "checked_object_id"])
    #         objs = []
    # RiskCheck.objects.bulk_update(objs, ["rule_id", "checked_object_content_type_id", "checked_object_id"])

class Migration(migrations.Migration):

    dependencies = [
        ('wbcompliance', '0019_rulegroup_riskrule_activation_date_and_more'),
    ]

    operations = [
        migrations.AddField(
            model_name='riskcheck',
            name='rule',
            field=models.ForeignKey(null=True, blank=True, on_delete=django.db.models.deletion.CASCADE, related_name='checks',
                                    to='wbcompliance.riskrule'),
            preserve_default=False,
        ),
        migrations.RenameField(
            model_name='riskcheck',
            old_name='trigger_content_type',
            new_name='checked_object_content_type',
        ),
        migrations.RenameField(
            model_name='riskcheck',
            old_name='trigger_id',
            new_name='checked_object_id',
        ),
        migrations.AddField(
            model_name='riskcheck',
            name='checked_object_repr',
            field=models.CharField(blank=True, max_length=256, null=True),
        ),
        migrations.RunSQL(sql="SET CONSTRAINTS ALL IMMEDIATE;"),
        migrations.RunPython(migrate_risk_check),
        migrations.RunSQL(sql="SET CONSTRAINTS ALL DEFERRED;"),
        migrations.RemoveField(
            model_name='riskcheck',
            name='rule_checked_object_relationship',
        ),
        migrations.AlterField(
            model_name='riskcheck',
            name='rule',
            field=models.ForeignKey( on_delete=django.db.models.deletion.CASCADE, related_name='checks', to='wbcompliance.riskrule'),
            preserve_default=False,
        ),
        migrations.AlterField(
            model_name='riskcheck',
            name='checked_object_content_type',
            field=models.ForeignKey(default=None, on_delete=django.db.models.deletion.CASCADE,
                                    related_name='triggered_checks', to='contenttypes.contenttype'),
            preserve_default=False,
        ),
        migrations.AlterField(
            model_name='riskcheck',
            name='checked_object_id',
            field=models.PositiveIntegerField(default=None),
            preserve_default=False,
        ),
    ]
