summaryrefslogtreecommitdiff
path: root/priv/repo/migrations/20200914105638_delete_notification_without_activity.exs
diff options
context:
space:
mode:
Diffstat (limited to 'priv/repo/migrations/20200914105638_delete_notification_without_activity.exs')
-rw-r--r--priv/repo/migrations/20200914105638_delete_notification_without_activity.exs30
1 files changed, 30 insertions, 0 deletions
diff --git a/priv/repo/migrations/20200914105638_delete_notification_without_activity.exs b/priv/repo/migrations/20200914105638_delete_notification_without_activity.exs
new file mode 100644
index 000000000..9333fc5a1
--- /dev/null
+++ b/priv/repo/migrations/20200914105638_delete_notification_without_activity.exs
@@ -0,0 +1,30 @@
+defmodule Pleroma.Repo.Migrations.DeleteNotificationWithoutActivity do
+ use Ecto.Migration
+
+ import Ecto.Query
+ alias Pleroma.Repo
+
+ def up do
+ from(
+ q in Pleroma.Notification,
+ left_join: c in assoc(q, :activity),
+ select: %{id: type(q.id, :integer)},
+ where: is_nil(c.id)
+ )
+ |> Repo.chunk_stream(1_000, :batches)
+ |> Stream.each(fn records ->
+ notification_ids = Enum.map(records, fn %{id: id} -> id end)
+
+ Repo.delete_all(
+ from(n in "notifications",
+ where: n.id in ^notification_ids
+ )
+ )
+ end)
+ |> Stream.run()
+ end
+
+ def down do
+ :ok
+ end
+end