mirror of
https://git.pleroma.social/pleroma/pleroma.git
synced 2024-12-23 00:26:30 +00:00
added notification constraints
This commit is contained in:
parent
2937e3095a
commit
3e53ab4e98
4 changed files with 67 additions and 10 deletions
|
@ -19,13 +19,13 @@ defmodule Pleroma.MigrationHelper.NotificationBackfill do
|
|||
query
|
||||
|> Repo.chunk_stream(100)
|
||||
|> Enum.each(fn notification ->
|
||||
type =
|
||||
notification.activity
|
||||
|> type_from_activity()
|
||||
if notification.activity do
|
||||
type = type_from_activity(notification.activity)
|
||||
|
||||
notification
|
||||
|> Ecto.Changeset.change(%{type: type})
|
||||
|> Repo.update()
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
|
@ -72,8 +72,7 @@ defmodule Pleroma.MigrationHelper.NotificationBackfill do
|
|||
"pleroma:emoji_reaction"
|
||||
|
||||
"Create" ->
|
||||
activity
|
||||
|> type_from_activity_object()
|
||||
type_from_activity_object(activity)
|
||||
|
||||
t ->
|
||||
raise "No notification type for activity type #{t}"
|
||||
|
|
|
@ -49,7 +49,7 @@ defmodule Pleroma.Repo do
|
|||
end
|
||||
end
|
||||
|
||||
def chunk_stream(query, chunk_size) do
|
||||
def chunk_stream(query, chunk_size, returns_as \\ :one) do
|
||||
# We don't actually need start and end funcitons of resource streaming,
|
||||
# but it seems to be the only way to not fetch records one-by-one and
|
||||
# have individual records be the elements of the stream, instead of
|
||||
|
@ -69,7 +69,12 @@ defmodule Pleroma.Repo do
|
|||
|
||||
records ->
|
||||
last_id = List.last(records).id
|
||||
|
||||
if returns_as == :one do
|
||||
{records, last_id}
|
||||
else
|
||||
{[records], last_id}
|
||||
end
|
||||
end
|
||||
end,
|
||||
fn _ -> :ok 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, :bacthes)
|
||||
|> 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 a new issue