Merge pull request 'Manually define PATH for Arch Linux users in systemd unit' (...
[akkoma] / priv / repo / migrations / 20210128092834_remove_duplicates_from_activity_expiration_queue.exs
1 defmodule Pleroma.Repo.Migrations.RemoveDuplicatesFromActivityExpirationQueue do
2 use Ecto.Migration
3
4 import Ecto.Query, only: [from: 2]
5
6 def up do
7 duplicate_ids =
8 from(j in Oban.Job,
9 where: j.queue == "activity_expiration",
10 where: j.worker == "Pleroma.Workers.PurgeExpiredActivity",
11 where: j.state == "scheduled",
12 select:
13 {fragment("(?)->>'activity_id'", j.args), fragment("array_agg(?)", j.id), count(j.id)},
14 group_by: fragment("(?)->>'activity_id'", j.args),
15 having: count(j.id) > 1
16 )
17 |> Pleroma.Repo.all()
18 |> Enum.map(fn {_, ids, _} ->
19 max_id = Enum.max(ids)
20 List.delete(ids, max_id)
21 end)
22 |> List.flatten()
23
24 from(j in Oban.Job, where: j.id in ^duplicate_ids)
25 |> Pleroma.Repo.delete_all()
26 end
27
28 def down, do: :noop
29 end