# Generated by Django 3.1.8 on 2021-04-21 15:41

from django.db import migrations


def convert_dim_int_to_str(apps, schema_editor):
    Dimension = apps.get_model('logs', 'Dimension')
    DimensionText = apps.get_model('logs', 'DimensionText')
    AccessLog = apps.get_model('logs', 'AccessLog')
    ReportType = apps.get_model('logs', 'ReportType')
    for dim in Dimension.objects.filter(type=1):
        for rt in dim.report_types.all():
            rt2dim = dim.reporttypetodimension_set.filter(report_type=rt).get()
            dim_ref = f'dim{rt2dim.position + 1}'
            values = (
                AccessLog.objects.filter(report_type=rt, **{f'{dim_ref}__isnull': False})
                .values_list(dim_ref, flat=True)
                .distinct()
            )
            value_to_obj = {
                value: DimensionText.objects.create(dimension=dim, text=str(value))
                for value in values
            }
            # we need to update materialized reports as well as they lack their own
            # dimension assignment
            rts = [rt] + list(ReportType.objects.filter(materialization_spec__base_report_type=rt))
            for value, obj in value_to_obj.items():
                AccessLog.objects.filter(report_type__in=rts, **{dim_ref: value}).update(
                    **{dim_ref: obj.pk}
                )
        dim.type = 2
        dim.save()


def noop(apps, schema_editor):
    pass


class Migration(migrations.Migration):

    dependencies = [
        ('logs', '0044_better_source_related_constraints'),
    ]

    operations = [
        migrations.RunPython(convert_dim_int_to_str, noop),
    ]

# Generated by Django 3.1.8 on 2021-04-21 15:41

from django.db import migrations


def convert_dim_int_to_str(apps, schema_editor):
    Dimension = apps.get_model('logs', 'Dimension')
    DimensionText = apps.get_model('logs', 'DimensionText')
    AccessLog = apps.get_model('logs', 'AccessLog')
    ReportType = apps.get_model('logs', 'ReportType')
    for dim in Dimension.objects.filter(type=1):
        for rt in dim.report_types.all():
            rt2dim = dim.reporttypetodimension_set.filter(report_type=rt).get()
            dim_ref = f'dim{rt2dim.position + 1}'
            values = (
                AccessLog.objects.filter(report_type=rt, **{f'{dim_ref}__isnull': False})
                .values_list(dim_ref, flat=True)
                .distinct()
            )
            value_to_obj = {
                value: DimensionText.objects.create(dimension=dim, text=str(value))
                for value in values
            }
            # we need to update materialized reports as well as they lack their own
            # dimension assignment
            rts = [rt] + list(ReportType.objects.filter(materialization_spec__base_report_type=rt))
            for value, obj in value_to_obj.items():
                AccessLog.objects.filter(report_type__in=rts, **{dim_ref: value}).update(
                    **{dim_ref: obj.pk}
                )
        dim.type = 2
        dim.save()


def noop(apps, schema_editor):
    pass


class Migration(migrations.Migration):

    dependencies = [
        ('logs', '0044_better_source_related_constraints'),
    ]

    operations = [
        migrations.RunPython(convert_dim_int_to_str, noop),
    ]

# Generated by Django 3.1.8 on 2021-04-21 15:41 # we need to update materialized reports as well as they lack their own # dimension assignment