Merge branch 'join-in-preloads' into 'develop'

Join on preloads to avoid N+1 queries

See merge request pleroma/pleroma!714
This commit is contained in:
kaniini 2019-01-26 15:57:02 +00:00
commit 3e66723f7e
2 changed files with 10 additions and 3 deletions

View File

@ -35,7 +35,8 @@ defmodule Pleroma.Notification do
n in Notification,
where: n.user_id == ^user.id,
order_by: [desc: n.id],
preload: [:activity],
join: activity in assoc(n, :activity),
preload: [activity: activity],
limit: 20
)
@ -66,7 +67,8 @@ defmodule Pleroma.Notification do
from(
n in Notification,
where: n.id == ^id,
preload: [:activity]
join: activity in assoc(n, :activity),
preload: [activity: activity]
)
notification = Repo.one(query)

View File

@ -33,7 +33,12 @@ defmodule Pleroma.Plugs.OAuthPlug do
#
@spec fetch_user_and_token(String.t()) :: {:ok, User.t(), Token.t()} | nil
defp fetch_user_and_token(token) do
query = from(q in Token, where: q.token == ^token, preload: [:user])
query =
from(t in Token,
where: t.token == ^token,
join: user in assoc(t, :user),
preload: [user: user]
)
with %Token{user: %{info: %{deactivated: false} = _} = user} = token_record <- Repo.one(query) do
{:ok, user, token_record}