Add a migration to remove embeded objects
authorrinpatch <rinpatch@sdf.org>
Thu, 18 Apr 2019 10:10:38 +0000 (13:10 +0300)
committerrinpatch <rinpatch@sdf.org>
Thu, 18 Apr 2019 10:10:38 +0000 (13:10 +0300)
lib/mix/tasks/compact_database.ex [deleted file]
priv/repo/migrations/20190418072951_remove_embeded_objects.exs [new file with mode: 0644]

diff --git a/lib/mix/tasks/compact_database.ex b/lib/mix/tasks/compact_database.ex
deleted file mode 100644 (file)
index 17b9721..0000000
+++ /dev/null
@@ -1,57 +0,0 @@
-defmodule Mix.Tasks.CompactDatabase do
-  @moduledoc """
-  Compact the database by flattening the object graph.
-  """
-
-  require Logger
-
-  use Mix.Task
-  import Ecto.Query
-  alias Pleroma.Activity
-  alias Pleroma.Repo
-
-  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
-
-  def run(_args) do
-    Application.ensure_all_started(:pleroma)
-
-    max = Repo.aggregate(Activity, :max, :id)
-    Logger.info("Considering #{max} activities")
-
-    chunks = 0..round(max / 100)
-
-    Enum.each(chunks, fn i ->
-      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
diff --git a/priv/repo/migrations/20190418072951_remove_embeded_objects.exs b/priv/repo/migrations/20190418072951_remove_embeded_objects.exs
new file mode 100644 (file)
index 0000000..1280942
--- /dev/null
@@ -0,0 +1,10 @@
+defmodule Pleroma.Repo.Migrations.RemoveEmbededObjects do
+  use Ecto.Migration
+
+  # TODO: bench on a real DB and add clippy if it takes too long
+  def change do
+  execute """
+  update activities set data = jsonb_set(data, '{object}'::text[], data->'object'->'id') where data->>'type' = 'Create' and data->'object'->>'id' is not null;
+  """
+  end
+end