From e64f031167a855b36298dbda988d6bde210299a4 Mon Sep 17 00:00:00 2001 From: Oneric Date: Mon, 23 Oct 2023 01:27:56 +0200 Subject: [PATCH] Log number of deleted rows in prune_orphaned_activities This gives feedback when to stop rerunning limited batches. Most of the diff is just adjusting indentation; best reviewed with whitespace-only changes hidden, e.g. `git diff -w`. --- .../docs/administration/CLI_tasks/database.md | 5 ++ lib/mix/tasks/pleroma/database.ex | 72 ++++++++++--------- 2 files changed, 44 insertions(+), 33 deletions(-) diff --git a/docs/docs/administration/CLI_tasks/database.md b/docs/docs/administration/CLI_tasks/database.md index eba56da10..c57817bf4 100644 --- a/docs/docs/administration/CLI_tasks/database.md +++ b/docs/docs/administration/CLI_tasks/database.md @@ -59,6 +59,11 @@ This will prune activities which are no longer referenced by anything. Such activities might be the result of running `prune_objects` without `--prune-orphaned-activities`. The same notes and warnings apply as for `prune_objects`. +The task will print out how many rows were freed in total in its last +line of output in the form `Deleted 345 rows`. +When running the job in limited batches this can be used to determine +when all orphaned activities have been deleted. + === "OTP" ```sh diff --git a/lib/mix/tasks/pleroma/database.ex b/lib/mix/tasks/pleroma/database.ex index 8ded7bbec..083f73fe2 100644 --- a/lib/mix/tasks/pleroma/database.ex +++ b/lib/mix/tasks/pleroma/database.ex @@ -29,40 +29,44 @@ defmodule Mix.Tasks.Pleroma.Database do end # Prune activities who link to a single object - """ - delete from public.activities - where id in ( - select a.id from public.activities a - left join public.objects o on a.data ->> 'object' = o.data ->> 'id' - left join public.activities a2 on a.data ->> 'object' = a2.data ->> 'id' - left join public.users u on a.data ->> 'object' = u.ap_id - where not a.local - and jsonb_typeof(a."data" -> 'object') = 'string' - and o.id is null - and a2.id is null - and u.id is null - #{limit_arg} - ) - """ - |> Repo.query([], timeout: :infinity) + {:ok, %{:num_rows => del_single}} = + """ + delete from public.activities + where id in ( + select a.id from public.activities a + left join public.objects o on a.data ->> 'object' = o.data ->> 'id' + left join public.activities a2 on a.data ->> 'object' = a2.data ->> 'id' + left join public.users u on a.data ->> 'object' = u.ap_id + where not a.local + and jsonb_typeof(a."data" -> 'object') = 'string' + and o.id is null + and a2.id is null + and u.id is null + #{limit_arg} + ) + """ + |> Repo.query([], timeout: :infinity) # Prune activities who link to an array of objects - """ - delete from public.activities - where id in ( - select a.id from public.activities a - join json_array_elements_text((a."data" -> 'object')::json) as j on jsonb_typeof(a."data" -> 'object') = 'array' - left join public.objects o on j.value = o.data ->> 'id' - left join public.activities a2 on j.value = a2.data ->> 'id' - left join public.users u on j.value = u.ap_id - group by a.id - having max(o.data ->> 'id') is null - and max(a2.data ->> 'id') is null - and max(u.ap_id) is null - #{limit_arg} - ) - """ - |> Repo.query([], timeout: :infinity) + {:ok, %{:num_rows => del_array}} = + """ + delete from public.activities + where id in ( + select a.id from public.activities a + join json_array_elements_text((a."data" -> 'object')::json) as j on jsonb_typeof(a."data" -> 'object') = 'array' + left join public.objects o on j.value = o.data ->> 'id' + left join public.activities a2 on j.value = a2.data ->> 'id' + left join public.users u on j.value = u.ap_id + group by a.id + having max(o.data ->> 'id') is null + and max(a2.data ->> 'id') is null + and max(u.ap_id) is null + #{limit_arg} + ) + """ + |> Repo.query([], timeout: :infinity) + + del_single + del_array end def run(["remove_embedded_objects" | args]) do @@ -131,7 +135,9 @@ defmodule Mix.Tasks.Pleroma.Database do Logger.info(log_message) - prune_orphaned_activities(limit) + deleted = prune_orphaned_activities(limit) + + Logger.info("Deleted #{deleted} rows") end def run(["prune_objects" | args]) do