Merge branch 'tusooa/assign-app-user-oom' into 'develop'

Fix AssignAppUser migration OOM

Closes #3358

See merge request pleroma/pleroma!4326
This commit is contained in:
tusooa 2025-03-20 09:13:50 +00:00
commit 7943152d90
2 changed files with 12 additions and 7 deletions

View file

@ -0,0 +1 @@
Fix AssignAppUser migration OOM

View file

@ -1,20 +1,24 @@
defmodule Pleroma.Repo.Migrations.AssignAppUser do
use Ecto.Migration
import Ecto.Query
alias Pleroma.Repo
alias Pleroma.Web.OAuth.App
alias Pleroma.Web.OAuth.Token
def up do
Repo.all(Token)
|> Enum.group_by(fn x -> Map.get(x, :app_id) end)
|> Enum.each(fn {_app_id, tokens} ->
token =
Enum.filter(tokens, fn x -> not is_nil(x.user_id) end)
|> List.first()
Token
|> where([t], not is_nil(t.user_id))
|> group_by([t], t.app_id)
|> select([t], %{app_id: t.app_id, id: min(t.id)})
|> order_by(asc: :app_id)
|> Repo.stream()
|> Stream.each(fn %{id: id} ->
token = Token.Query.get_by_id(id) |> Repo.one()
App.maybe_update_owner(token)
end)
|> Stream.run()
end
def down, do: :ok