1 defmodule Mix.Tasks.CompactDatabase do
3 Compact the database by flattening the object graph.
10 alias Pleroma.Activity
13 defp maybe_compact(%Activity{data: %{"object" => %{"id" => object_id}}} = activity) do
16 |> Map.put("object", object_id)
19 Activity.change(activity, %{data: data})
25 defp maybe_compact(%Activity{} = activity), do: {:ok, activity}
27 defp activity_query(min_id, max_id) do
30 where: fragment("?->>'type' = 'Create'", a.data),
31 where: a.id >= ^min_id,
37 Application.ensure_all_started(:pleroma)
39 max = Repo.aggregate(Activity, :max, :id)
40 Logger.info("Considering #{max} activities")
42 chunks = 0..round(max / 100)
44 Enum.each(chunks, fn i ->
48 activity_query(min, max)
50 |> Enum.each(&maybe_compact/1)
55 Logger.info("Finished.")