Fix AssignAppUser migration OOM

This commit is contained in:
tusooa 2025-02-23 21:12:08 -05:00
commit 7b69e52564
No known key found for this signature in database
GPG key ID: 42AEC43D48433C51
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 defmodule Pleroma.Repo.Migrations.AssignAppUser do
use Ecto.Migration use Ecto.Migration
import Ecto.Query
alias Pleroma.Repo alias Pleroma.Repo
alias Pleroma.Web.OAuth.App alias Pleroma.Web.OAuth.App
alias Pleroma.Web.OAuth.Token alias Pleroma.Web.OAuth.Token
def up do def up do
Repo.all(Token) Token
|> Enum.group_by(fn x -> Map.get(x, :app_id) end) |> where([t], not is_nil(t.user_id))
|> Enum.each(fn {_app_id, tokens} -> |> group_by([t], t.app_id)
token = |> select([t], %{app_id: t.app_id, id: min(t.id)})
Enum.filter(tokens, fn x -> not is_nil(x.user_id) end) |> order_by(asc: :app_id)
|> List.first() |> Repo.stream()
|> Stream.each(fn %{id: id} ->
token = Token.Query.get_by_id(id) |> Repo.one()
App.maybe_update_owner(token) App.maybe_update_owner(token)
end) end)
|> Stream.run()
end end
def down, do: :ok def down, do: :ok