1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Mix.Tasks.Pleroma.Database do
6 alias Pleroma.Conversation
11 require Pleroma.Constants
15 @shortdoc "A collection of database related tasks"
16 @moduledoc File.read!("docs/administration/CLI_tasks/database.md")
18 def run(["remove_embedded_objects" | args]) do
28 Logger.info("Removing embedded objects")
31 "update activities set data = safe_jsonb_set(data, '{object}'::text[], data->'object'->'id') where data->'object'->>'id' is not null;",
36 if Keyword.get(options, :vacuum) do
37 Logger.info("Runnning VACUUM FULL.")
40 "Re-packing your entire database may take a while and will consume extra disk space during the process."
51 def run(["bump_all_conversations"]) do
53 Conversation.bump_for_all_activities()
56 def run(["update_users_following_followers_counts"]) do
61 |> Enum.each(&User.update_follower_count/1)
64 def run(["prune_objects" | args]) do
77 deadline = Pleroma.Config.get([:instance, :remote_post_retention_days])
79 Logger.info("Pruning objects older than #{deadline} days")
82 NaiveDateTime.utc_now()
83 |> NaiveDateTime.add(-(deadline * 86_400))
88 "?->'to' \\? ? OR ?->'cc' \\? ?",
90 ^Pleroma.Constants.as_public(),
92 ^Pleroma.Constants.as_public()
94 where: o.inserted_at < ^time_deadline,
96 fragment("split_part(?->>'actor', '/', 3) != ?", o.data, ^Pleroma.Web.Endpoint.host())
98 |> Repo.delete_all(timeout: :infinity)
100 if Keyword.get(options, :vacuum) do
101 Logger.info("Runnning VACUUM FULL.")
104 "Re-packing your entire database may take a while and will consume extra disk space during the process."
115 def run(["fix_likes_collections"]) do
120 from(object in Object,
121 where: fragment("(?)->>'likes' is not null", object.data),
122 select: %{id: object.id, likes: fragment("(?)->>'likes'", object.data)}
124 |> Pleroma.RepoStreamer.chunk_stream(100)
125 |> Stream.each(fn objects ->
128 |> Enum.filter(fn object -> object.likes |> Jason.decode!() |> is_map() end)
132 |> where([object], object.id in ^ids)
137 "safe_jsonb_set(?, '{likes}', '[]'::jsonb, true)",
142 |> Repo.update_all([], timeout: :infinity)
147 def run(["vacuum", args]) do
152 Logger.info("Runnning VACUUM ANALYZE.")
161 Logger.info("Runnning VACUUM FULL.")
164 "Re-packing your entire database may take a while and will consume extra disk space during the process."
174 Logger.error("Error: invalid vacuum argument.")