Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
944B

  1. defmodule Pleroma.Repo.Migrations.RemoveUnreadConversationCountFromUser do
  2. use Ecto.Migration
  3. import Ecto.Query
  4. alias Pleroma.Repo
  5. def up do
  6. alter table(:users) do
  7. remove_if_exists(:unread_conversation_count, :integer)
  8. end
  9. end
  10. def down do
  11. alter table(:users) do
  12. add_if_not_exists(:unread_conversation_count, :integer, default: 0)
  13. end
  14. flush()
  15. recalc_unread_conversation_count()
  16. end
  17. defp recalc_unread_conversation_count do
  18. participations_subquery =
  19. from(
  20. p in "conversation_participations",
  21. where: p.read == false,
  22. group_by: p.user_id,
  23. select: %{user_id: p.user_id, unread_conversation_count: count(p.id)}
  24. )
  25. from(
  26. u in "users",
  27. join: p in subquery(participations_subquery),
  28. on: p.user_id == u.id,
  29. update: [set: [unread_conversation_count: p.unread_conversation_count]]
  30. )
  31. |> Repo.update_all([])
  32. end
  33. end