logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

20200831114918_remove_unread_conversation_count_from_user.exs (1103B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Repo.Migrations.RemoveUnreadConversationCountFromUser do
  5. use Ecto.Migration
  6. import Ecto.Query
  7. alias Pleroma.Repo
  8. def up do
  9. alter table(:users) do
  10. remove_if_exists(:unread_conversation_count, :integer)
  11. end
  12. end
  13. def down do
  14. alter table(:users) do
  15. add_if_not_exists(:unread_conversation_count, :integer, default: 0)
  16. end
  17. flush()
  18. recalc_unread_conversation_count()
  19. end
  20. defp recalc_unread_conversation_count do
  21. participations_subquery =
  22. from(
  23. p in "conversation_participations",
  24. where: p.read == false,
  25. group_by: p.user_id,
  26. select: %{user_id: p.user_id, unread_conversation_count: count(p.id)}
  27. )
  28. from(
  29. u in "users",
  30. join: p in subquery(participations_subquery),
  31. on: p.user_id == u.id,
  32. update: [set: [unread_conversation_count: p.unread_conversation_count]]
  33. )
  34. |> Repo.update_all([])
  35. end
  36. end