# Generated by Django 2.2.11 on 2020-04-12 07:48

from django.db import migrations


def add_users_to_facility_user(apps, *args):
    facility_model = apps.get_model("facility", "Facility")
    facility_user_model = apps.get_model("facility", "FacilityUser")

    facility_user_objs = []
    for facility in facility_model.objects.all():
        facility_user_objs.append(
            facility_user_model(
                facility=facility,
                user=facility.created_by,
                created_by=facility.created_by,
            )
        )
    facility_user_model.objects.bulk_create(facility_user_objs)


def reverse_add_users_to_facility_user(apps, *args):
    facility_user_model = apps.get_model("facility", "FacilityUser")
    facility_user_model.objects.filter(facility_id__isnull=False).delete()


class Migration(migrations.Migration):
    dependencies = [
        ("facility", "0086_auto_20200412_1313"),
    ]

    operations = [
        migrations.RunPython(
            add_users_to_facility_user, reverse_code=reverse_add_users_to_facility_user
        )
    ]
