# Generated by Django 3.0.4 on 2020-12-11 15:54

import django.db.models.deletion
from django.db import migrations, models


def forwards(apps, schema_editor):
    Community = apps.get_model("community", "Community")
    PackageListing = apps.get_model("community", "PackageListing")
    PackageCategory = apps.get_model("community", "PackageCategory")
    default_community = Community.objects.create(
        identifier="riskofrain2", name="Risk of Rain 2"
    )
    for entry in PackageListing.objects.all():
        entry.community = default_community
        entry.save(update_fields=("community",))
    for entry in PackageCategory.objects.all():
        entry.community = default_community
        entry.save(update_fields=("community",))


def backwards(apps, schema_editor):
    Community = apps.get_model("community", "Community")
    PackageListing = apps.get_model("community", "PackageListing")
    PackageCategory = apps.get_model("community", "PackageCategory")
    for entry in PackageListing.objects.all():
        entry.community = None
        entry.save(update_fields=("community",))
    for entry in PackageCategory.objects.all():
        entry.community = None
        entry.save(update_fields=("community",))
    Community.objects.filter(identifier="riskofrain2").first().delete()


class Migration(migrations.Migration):

    dependencies = [
        ("community", "0003_add_community"),
    ]

    operations = [
        migrations.RunPython(forwards, backwards),
        migrations.AlterField(
            model_name="packagecategory",
            name="community",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.CASCADE,
                related_name="package_categories",
                to="community.Community",
            ),
        ),
        migrations.AlterField(
            model_name="packagelisting",
            name="community",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.CASCADE,
                related_name="package_listings",
                to="community.Community",
            ),
        ),
    ]
