logo

pleroma

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

20210128092834_remove_duplicates_from_activity_expiration_queue.exs (1011B)


  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.RemoveDuplicatesFromActivityExpirationQueue do
  5. use Ecto.Migration
  6. import Ecto.Query, only: [from: 2]
  7. def up do
  8. duplicate_ids =
  9. from(j in Oban.Job,
  10. where: j.queue == "activity_expiration",
  11. where: j.worker == "Pleroma.Workers.PurgeExpiredActivity",
  12. where: j.state == "scheduled",
  13. select:
  14. {fragment("(?)->>'activity_id'", j.args), fragment("array_agg(?)", j.id), count(j.id)},
  15. group_by: fragment("(?)->>'activity_id'", j.args),
  16. having: count(j.id) > 1
  17. )
  18. |> Pleroma.Repo.all()
  19. |> Enum.map(fn {_, ids, _} ->
  20. max_id = Enum.max(ids)
  21. List.delete(ids, max_id)
  22. end)
  23. |> List.flatten()
  24. from(j in Oban.Job, where: j.id in ^duplicate_ids)
  25. |> Pleroma.Repo.delete_all()
  26. end
  27. def down, do: :noop
  28. end