move to 2.5.5
[anni] / priv / repo / migrations / 20200825061316_move_activity_expirations_to_oban.exs
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
5 defmodule Pleroma.Repo.Migrations.MoveActivityExpirationsToOban do
6   use Ecto.Migration
7
8   import Ecto.Query, only: [from: 2]
9
10   def change do
11     Pleroma.Config.Oban.warn()
12
13     Application.ensure_all_started(:oban)
14
15     Supervisor.start_link([{Oban, Pleroma.Config.get(Oban)}],
16       strategy: :one_for_one,
17       name: Pleroma.Supervisor
18     )
19
20     from(e in "activity_expirations",
21       select: %{id: e.id, activity_id: e.activity_id, scheduled_at: e.scheduled_at}
22     )
23     |> Pleroma.Repo.stream()
24     |> Stream.each(fn expiration ->
25       with {:ok, expires_at} <- DateTime.from_naive(expiration.scheduled_at, "Etc/UTC") do
26         Pleroma.Workers.PurgeExpiredActivity.enqueue(%{
27           activity_id: FlakeId.to_string(expiration.activity_id),
28           expires_at: expires_at
29         })
30       end
31     end)
32     |> Stream.run()
33   end
34 end