# Generated by Django 2.2.6 on 2019-10-28 03:47

from django.db import migrations


def update_association(apps, schema_editor):
    """ Update the assoc value on all TreeComments """

    CommentAssociation = apps.get_model('django_comments_tree', 'CommentAssociation')
    TreeComment = apps.get_model('django_comments_tree', 'TreeComment')
    for root in TreeComment.objects.filter(depth=1).all():
        assoc = CommentAssociation.objects.get(root=root)
        TreeComment.objects.filter(
            path__startswith=root.path,
            depth__gte=root.depth).update(assoc=assoc)


def reverse_associations(apps, schema_editor):
    """ Set associations to null """
    TreeComment = apps.get_model('django_comments_tree', 'TreeComment')
    for root in TreeComment.objects.filter(depth=1).all():
        TreeComment.objects.filter(
            path__startswith=root.path,
            depth__gte=root.depth).update(assoc=None)


class Migration(migrations.Migration):
    dependencies = [
        ('django_comments_tree', '0007_auto_20191023_1440'),
    ]

    operations = [
        migrations.RunPython(
            update_association,
            reverse_associations
        ),
    ]
