1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Repo.Migrations.RemoveUnreadConversationCountFromUser do
11 alter table(:users) do
12 remove_if_exists(:unread_conversation_count, :integer)
17 alter table(:users) do
18 add_if_not_exists(:unread_conversation_count, :integer, default: 0)
22 recalc_unread_conversation_count()
25 defp recalc_unread_conversation_count do
26 participations_subquery =
28 p in "conversation_participations",
29 where: p.read == false,
31 select: %{user_id: p.user_id, unread_conversation_count: count(p.id)}
36 join: p in subquery(participations_subquery),
37 on: p.user_id == u.id,
38 update: [set: [unread_conversation_count: p.unread_conversation_count]]
40 |> Repo.update_all([])