[#2089] fix notifications See merge request pleroma/pleroma!3000stable
commit
6b088ed76a
@ -1,34 +0,0 @@
|
||||
# Pleroma: A lightweight social networking server
|
||||
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
defmodule Pleroma.RepoStreamer do
|
||||
alias Pleroma.Repo
|
||||
import Ecto.Query
|
||||
|
||||
def chunk_stream(query, chunk_size) do
|
||||
Stream.unfold(0, fn
|
||||
:halt ->
|
||||
{[], :halt}
|
||||
|
||||
last_id ->
|
||||
query
|
||||
|> order_by(asc: :id)
|
||||
|> where([r], r.id > ^last_id)
|
||||
|> limit(^chunk_size)
|
||||
|> Repo.all()
|
||||
|> case do
|
||||
[] ->
|
||||
{[], :halt}
|
||||
|
||||
records ->
|
||||
last_id = List.last(records).id
|
||||
{records, last_id}
|
||||
end
|
||||
end)
|
||||
|> Stream.take_while(fn
|
||||
[] -> false
|
||||
_ -> true
|
||||
end)
|
||||
end
|
||||
end
|
@ -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
|
@ -0,0 +1,23 @@
|
||||
defmodule Pleroma.Repo.Migrations.AddNotificationConstraints do
|
||||
use Ecto.Migration
|
||||
|
||||
def up do
|
||||
drop(constraint(:notifications, "notifications_activity_id_fkey"))
|
||||
|
||||
alter table(:notifications) do
|
||||
modify(:activity_id, references(:activities, type: :uuid, on_delete: :delete_all),
|
||||
null: false
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
def down do
|
||||
drop(constraint(:notifications, "notifications_activity_id_fkey"))
|
||||
|
||||
alter table(:notifications) do
|
||||
modify(:activity_id, references(:activities, type: :uuid, on_delete: :delete_all),
|
||||
null: true
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in new issue