# Generated by Django 2.2.3 on 2019-07-15 19:24

from django.db import migrations, models


def backpopulate_incomplete_profiles(apps, schema):
    """Backpopulate users who don't have a profile record"""
    User = apps.get_model("users", "User")
    Profile = apps.get_model("users", "Profile")

    for user in User.objects.annotate(
        has_profile=models.Exists(Profile.objects.filter(user=models.OuterRef("pk")))
    ).filter(has_profile=False):
        Profile.objects.get_or_create(user=user)


def remove_incomplete_profiles(apps, schema):
    """Delete records that will cause rollbacks on nullable/blankable fields to fail"""
    Profile = apps.get_model("users", "Profile")

    Profile.objects.filter(
        models.Q(birth_year__isnull=True)
        | models.Q(gender__exact="")
        | models.Q(job_title__exact="")
        | models.Q(company__exact="")
    ).delete()


class Migration(migrations.Migration):

    dependencies = [("users", "0007_validate_country_and_state")]

    operations = [
        migrations.AlterField(
            model_name="profile",
            name="birth_year",
            field=models.IntegerField(blank=True, null=True),
        ),
        migrations.AlterField(
            model_name="profile",
            name="company",
            field=models.CharField(blank=True, default="", max_length=128),
        ),
        migrations.AlterField(
            model_name="profile",
            name="gender",
            field=models.CharField(
                blank=True,
                choices=[
                    ("m", "Male"),
                    ("f", "Female"),
                    ("o", "Other/Prefer Not to Say"),
                ],
                default="",
                max_length=10,
            ),
        ),
        migrations.AlterField(
            model_name="profile",
            name="industry",
            field=models.CharField(blank=True, default="", max_length=60),
        ),
        migrations.AlterField(
            model_name="profile",
            name="job_function",
            field=models.CharField(blank=True, default="", max_length=60),
        ),
        migrations.AlterField(
            model_name="profile",
            name="job_title",
            field=models.CharField(blank=True, default="", max_length=128),
        ),
        migrations.AlterField(
            model_name="profile",
            name="leadership_level",
            field=models.CharField(blank=True, default="", max_length=60),
        ),
        migrations.RunPython(
            backpopulate_incomplete_profiles, reverse_code=remove_incomplete_profiles
        ),
    ]

[((2522, 2622), 'django.db.migrations.RunPython', 'migrations.RunPython', (['backpopulate_incomplete_profiles'], {'reverse_code': 'remove_incomplete_profiles'}), '(backpopulate_incomplete_profiles, reverse_code=\n    remove_incomplete_profiles)\n', (2542, 2622), False, 'from django.db import migrations, models\n'), ((1133, 1175), 'django.db.models.IntegerField', 'models.IntegerField', ([], {'blank': '(True)', 'null': '(True)'}), '(blank=True, null=True)\n', (1152, 1175), False, 'from django.db import migrations, models\n'), ((1299, 1355), 'django.db.models.CharField', 'models.CharField', ([], {'blank': '(True)', 'default': '""""""', 'max_length': '(128)'}), "(blank=True, default='', max_length=128)\n", (1315, 1355), False, 'from django.db import migrations, models\n'), ((1478, 1613), 'django.db.models.CharField', 'models.CharField', ([], {'blank': '(True)', 'choices': "[('m', 'Male'), ('f', 'Female'), ('o', 'Other/Prefer Not to Say')]", 'default': '""""""', 'max_length': '(10)'}), "(blank=True, choices=[('m', 'Male'), ('f', 'Female'), ('o',\n    'Other/Prefer Not to Say')], default='', max_length=10)\n", (1494, 1613), False, 'from django.db import migrations, models\n'), ((1892, 1947), 'django.db.models.CharField', 'models.CharField', ([], {'blank': '(True)', 'default': '""""""', 'max_length': '(60)'}), "(blank=True, default='', max_length=60)\n", (1908, 1947), False, 'from django.db import migrations, models\n'), ((2076, 2131), 'django.db.models.CharField', 'models.CharField', ([], {'blank': '(True)', 'default': '""""""', 'max_length': '(60)'}), "(blank=True, default='', max_length=60)\n", (2092, 2131), False, 'from django.db import migrations, models\n'), ((2257, 2313), 'django.db.models.CharField', 'models.CharField', ([], {'blank': '(True)', 'default': '""""""', 'max_length': '(128)'}), "(blank=True, default='', max_length=128)\n", (2273, 2313), False, 'from django.db import migrations, models\n'), ((2446, 2501), 'django.db.models.CharField', 'models.CharField', ([], {'blank': '(True)', 'default': '""""""', 'max_length': '(60)'}), "(blank=True, default='', max_length=60)\n", (2462, 2501), False, 'from django.db import migrations, models\n'), ((848, 875), 'django.db.models.Q', 'models.Q', ([], {'company__exact': '""""""'}), "(company__exact='')\n", (856, 875), False, 'from django.db import migrations, models\n'), ((808, 837), 'django.db.models.Q', 'models.Q', ([], {'job_title__exact': '""""""'}), "(job_title__exact='')\n", (816, 837), False, 'from django.db import migrations, models\n'), ((727, 760), 'django.db.models.Q', 'models.Q', ([], {'birth_year__isnull': '(True)'}), '(birth_year__isnull=True)\n', (735, 760), False, 'from django.db import migrations, models\n'), ((771, 797), 'django.db.models.Q', 'models.Q', ([], {'gender__exact': '""""""'}), "(gender__exact='')\n", (779, 797), False, 'from django.db import migrations, models\n'), ((399, 420), 'django.db.models.OuterRef', 'models.OuterRef', (['"""pk"""'], {}), "('pk')\n", (414, 420), False, 'from django.db import migrations, models\n')]