def check_attachment_reference_change(message: Message) -> bool:
    prev_attachments = {a.path_id for a in message.attachment_set.all()}
    new_attachments = set(message.potential_attachment_path_ids)
    if (new_attachments == prev_attachments):
        return bool(prev_attachments)
    to_remove = list((prev_attachments - new_attachments))
    if (len(to_remove) > 0):
        attachments_to_update = Attachment.objects.filter(path_id__in=to_remove).select_for_update()
        message.attachment_set.remove(*attachments_to_update)
    to_add = list((new_attachments - prev_attachments))
    if (len(to_add) > 0):
        do_claim_attachments(message, to_add)
    return message.attachment_set.exists()
def check_attachment_reference_change(message: Message) -> bool:
    prev_attachments = {a.path_id for a in message.attachment_set.all()}
    new_attachments = set(message.potential_attachment_path_ids)
    if (new_attachments == prev_attachments):
        return bool(prev_attachments)
    to_remove = list((prev_attachments - new_attachments))
    if (len(to_remove) > 0):
        attachments_to_update = Attachment.objects.filter(path_id__in=to_remove).select_for_update()
        message.attachment_set.remove(*attachments_to_update)
    to_add = list((new_attachments - prev_attachments))
    if (len(to_add) > 0):
        do_claim_attachments(message, to_add)
    return message.attachment_set.exists()