def mark_existing_tags_as_permanent(
    apps: Apps, schema_editor: BaseDatabaseSchemaEditor
) -> None:
    """
    Update all tags in the database which the FE treats as 'permanent' through regex matching
    to have the new is_permanent label. The FE will subsequently be updated to use the
    is_permanent attribute instead of regex matching.
    """
    tag_class = apps.get_model("tags", "tag")

    to_update = []

    for tag in filter(
        lambda t: get_sanitised_label(t.label) in PROTECTED_LABELS,
        tag_class.objects.all(),
    ):
        tag.is_permanent = True
        to_update.append(tag)

    tag_class.objects.bulk_update(to_update, fields=["is_permanent"])

    Update all tags in the database which the FE treats as 'permanent' through regex matching
    to have the new is_permanent label. The FE will subsequently be updated to use the
    is_permanent attribute instead of regex matching.
    
api/projects/tags/migrations/0005_add_tag_fields_for_stale_flags_logic.py