# Generated by Django 3.1.4 on 2020-12-03 20:09

from django.apps.registry import Apps
from django.db import migrations
from django.db.backends.base.schema import BaseDatabaseSchemaEditor


def remove_pb_prefix_users(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
    alias = schema_editor.connection.alias
    User = apps.get_model("authentik_core", "User")
    Outpost = apps.get_model("authentik_outposts", "Outpost")

    for outpost in Outpost.objects.using(alias).all():
        matching = User.objects.using(alias).filter(
            username=f"pb-outpost-{outpost.uuid.hex}"
        )
        if matching.exists():
            matching.delete()


class Migration(migrations.Migration):

    dependencies = [
        ("authentik_core", "0016_auto_20201202_2234"),
        ("authentik_outposts", "0012_service_connection_non_unique"),
    ]

    operations = [
        migrations.RunPython(remove_pb_prefix_users),
    ]
