Refactor the query to detect unattached media
The previous one was going 💥 in production
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
parent
71f50b35b6
commit
db1649ded8
@ -33,6 +33,22 @@ defmodule Mobilizon.Service.CleanOrphanMedia do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Ecto doesn't currently allow us to use exists with a subquery,
|
||||||
|
# so we can't create the union through Ecto
|
||||||
|
# https://github.com/elixir-ecto/ecto/issues/3619
|
||||||
|
@union_query [
|
||||||
|
[from: "events", param: "picture_id"],
|
||||||
|
[from: "events_medias", param: "media_id"],
|
||||||
|
[from: "posts", param: "picture_id"],
|
||||||
|
[from: "posts_medias", param: "media_id"],
|
||||||
|
[from: "comments_medias", param: "media_id"]
|
||||||
|
]
|
||||||
|
|> Enum.map(fn [from: from, param: param] ->
|
||||||
|
"SELECT 1 FROM #{from} WHERE #{from}.#{param} = m0.id"
|
||||||
|
end)
|
||||||
|
|> Enum.join(" UNION ")
|
||||||
|
|> (&"NOT EXISTS(#{&1})").()
|
||||||
|
|
||||||
@spec find_media(Keyword.t()) :: list(Media.t())
|
@spec find_media(Keyword.t()) :: list(Media.t())
|
||||||
defp find_media(opts) do
|
defp find_media(opts) do
|
||||||
default_grace_period =
|
default_grace_period =
|
||||||
@ -41,21 +57,17 @@ defmodule Mobilizon.Service.CleanOrphanMedia do
|
|||||||
grace_period = Keyword.get(opts, :grace_period, default_grace_period)
|
grace_period = Keyword.get(opts, :grace_period, default_grace_period)
|
||||||
expiration_date = DateTime.add(DateTime.utc_now(), grace_period * -3600)
|
expiration_date = DateTime.add(DateTime.utc_now(), grace_period * -3600)
|
||||||
|
|
||||||
Media
|
query =
|
||||||
|> where([m], m.inserted_at < ^expiration_date)
|
from(m in Media,
|
||||||
|> join(:inner, [m], a in Actor)
|
as: :media,
|
||||||
|> where([_m, a], is_nil(a.domain))
|
distinct: true,
|
||||||
|> join(:left, [m], e in assoc(m, :events))
|
join: a in Actor,
|
||||||
|> join(:left, [m], ep in assoc(m, :event_picture))
|
on: a.id == m.actor_id,
|
||||||
|> join(:left, [m], p in assoc(m, :posts))
|
where: is_nil(a.domain),
|
||||||
|> join(:left, [m], pp in assoc(m, :posts_picture))
|
where: m.inserted_at < ^expiration_date,
|
||||||
|> join(:left, [m], c in assoc(m, :comments))
|
where: fragment(@union_query)
|
||||||
|> where([_m, _a, e], is_nil(e.id))
|
)
|
||||||
|> where([_m, _a, _e, ep], is_nil(ep.id))
|
|
||||||
|> where([_m, _a, _e, _ep, p], is_nil(p.id))
|
Repo.all(query)
|
||||||
|> where([_m, _a, _e, _ep, _p, pp], is_nil(pp.id))
|
|
||||||
|> where([_m, _a, _e, _ep, _p, _pp, c], is_nil(c.id))
|
|
||||||
|> distinct(true)
|
|
||||||
|> Repo.all()
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user