2018-11-29 07:53:17 +01:00
|
|
|
defmodule Mix.Tasks.CompactDatabase do
|
|
|
|
@moduledoc """
|
|
|
|
Compact the database by flattening the object graph.
|
|
|
|
"""
|
|
|
|
|
|
|
|
require Logger
|
|
|
|
|
|
|
|
use Mix.Task
|
|
|
|
import Ecto.Query
|
2019-04-17 11:22:32 +02:00
|
|
|
alias Pleroma.Activity
|
|
|
|
alias Pleroma.Repo
|
2018-11-29 07:53:17 +01:00
|
|
|
|
|
|
|
defp maybe_compact(%Activity{data: %{"object" => %{"id" => object_id}}} = activity) do
|
|
|
|
data =
|
|
|
|
activity.data
|
|
|
|
|> Map.put("object", object_id)
|
|
|
|
|
|
|
|
{:ok, activity} =
|
|
|
|
Activity.change(activity, %{data: data})
|
|
|
|
|> Repo.update()
|
|
|
|
|
|
|
|
{:ok, activity}
|
|
|
|
end
|
|
|
|
|
|
|
|
defp maybe_compact(%Activity{} = activity), do: {:ok, activity}
|
|
|
|
|
|
|
|
defp activity_query(min_id, max_id) do
|
|
|
|
from(
|
|
|
|
a in Activity,
|
|
|
|
where: fragment("?->>'type' = 'Create'", a.data),
|
|
|
|
where: a.id >= ^min_id,
|
|
|
|
where: a.id < ^max_id
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-04-17 11:22:32 +02:00
|
|
|
def run(_args) do
|
2018-11-29 07:53:17 +01:00
|
|
|
Application.ensure_all_started(:pleroma)
|
|
|
|
|
|
|
|
max = Repo.aggregate(Activity, :max, :id)
|
|
|
|
Logger.info("Considering #{max} activities")
|
|
|
|
|
2018-12-01 23:17:28 +01:00
|
|
|
chunks = 0..round(max / 100)
|
2018-11-29 07:53:17 +01:00
|
|
|
|
2018-12-01 23:17:28 +01:00
|
|
|
Enum.each(chunks, fn i ->
|
2018-11-29 07:53:17 +01:00
|
|
|
min = i * 100
|
|
|
|
max = min + 100
|
|
|
|
|
|
|
|
activity_query(min, max)
|
|
|
|
|> Repo.all()
|
|
|
|
|> Enum.each(&maybe_compact/1)
|
|
|
|
|
|
|
|
IO.write(".")
|
|
|
|
end)
|
|
|
|
|
|
|
|
Logger.info("Finished.")
|
|
|
|
end
|
|
|
|
end
|