formatting
[akkoma] / lib / mix / tasks / compact_database.ex
1 defmodule Mix.Tasks.CompactDatabase do
2 @moduledoc """
3 Compact the database by flattening the object graph.
4 """
5
6 require Logger
7
8 use Mix.Task
9 import Mix.Ecto
10 import Ecto.Query
11 alias Pleroma.{Repo, Object, Activity}
12
13 defp maybe_compact(%Activity{data: %{"object" => %{"id" => object_id}}} = activity) do
14 data =
15 activity.data
16 |> Map.put("object", object_id)
17
18 {:ok, activity} =
19 Activity.change(activity, %{data: data})
20 |> Repo.update()
21
22 {:ok, activity}
23 end
24
25 defp maybe_compact(%Activity{} = activity), do: {:ok, activity}
26
27 defp activity_query(min_id, max_id) do
28 from(
29 a in Activity,
30 where: fragment("?->>'type' = 'Create'", a.data),
31 where: a.id >= ^min_id,
32 where: a.id < ^max_id
33 )
34 end
35
36 def run(args) do
37 Application.ensure_all_started(:pleroma)
38
39 max = Repo.aggregate(Activity, :max, :id)
40 Logger.info("Considering #{max} activities")
41
42 chunks = 0..round(max / 100)
43
44 Enum.each(chunks, fn i ->
45 min = i * 100
46 max = min + 100
47
48 activity_query(min, max)
49 |> Repo.all()
50 |> Enum.each(&maybe_compact/1)
51
52 IO.write(".")
53 end)
54
55 Logger.info("Finished.")
56 end
57 end