# Generated by Django 4.2.1 on 2023-06-12 10:47

from django.db import migrations
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
from django.db.migrations.state import StateApps
from django.db.models import OuterRef


def set_default_value_for_can_delete_any_message_group(
    apps: StateApps, schema_editor: BaseDatabaseSchemaEditor
) -> None:
    Realm = apps.get_model("zerver", "Realm")
    NamedUserGroup = apps.get_model("zerver", "NamedUserGroup")

    ADMINISTRATORS_GROUP_NAME = "role:administrators"

    Realm.objects.filter(
        can_delete_any_message_group=None,
    ).update(
        can_delete_any_message_group=NamedUserGroup.objects.filter(
            name=ADMINISTRATORS_GROUP_NAME, realm=OuterRef("id"), is_system_group=True
        ).values("pk")
    )


class Migration(migrations.Migration):
    atomic = False

    dependencies = [
        ("zerver", "0565_realm_can_delete_any_message_group"),
    ]

    operations = [
        migrations.RunPython(
            set_default_value_for_can_delete_any_message_group,
            elidable=True,
            reverse_code=migrations.RunPython.noop,
        )
    ]
