# Generated by Django 5.0.10 on 2025-01-06 09:08

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


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

    Realm.objects.filter(moderation_request_channel__deactivated=True).update(
        moderation_request_channel=None
    )
    Realm.objects.filter(new_stream_announcements_stream__deactivated=True).update(
        new_stream_announcements_stream=None
    )
    Realm.objects.filter(signup_announcements_stream__deactivated=True).update(
        signup_announcements_stream=None
    )
    Realm.objects.filter(zulip_update_announcements_stream__deactivated=True).update(
        zulip_update_announcements_stream=None
    )


class Migration(migrations.Migration):
    dependencies = [
        ("zerver", "0661_archivetransaction_protect_from_deletion"),
    ]

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