9ff4fcff634d0d9210f069cc9b5b4009bc37a97d
[anni] / priv / repo / migrations / 20200914105638_delete_notification_without_activity.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.DeleteNotificationWithoutActivity do
6   use Ecto.Migration
7
8   import Ecto.Query
9   alias Pleroma.Repo
10
11   def up do
12     from(
13       q in Pleroma.Notification,
14       left_join: c in assoc(q, :activity),
15       select: %{id: type(q.id, :integer)},
16       where: is_nil(c.id)
17     )
18     |> Repo.chunk_stream(1_000, :batches)
19     |> Stream.each(fn records ->
20       notification_ids = Enum.map(records, fn %{id: id} -> id end)
21
22       Repo.delete_all(
23         from(n in "notifications",
24           where: n.id in ^notification_ids
25         )
26       )
27     end)
28     |> Stream.run()
29   end
30
31   def down do
32     :ok
33   end
34 end