1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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")
47 def run(["bump_all_conversations"]) do
49 Conversation.bump_for_all_activities()
52 def run(["update_users_following_followers_counts"]) do
57 |> Enum.each(&User.update_follower_count/1)
60 def run(["prune_objects" | args]) do
73 deadline = Pleroma.Config.get([:instance, :remote_post_retention_days])
75 Logger.info("Pruning objects older than #{deadline} days")
78 NaiveDateTime.utc_now()
79 |> NaiveDateTime.add(-(deadline * 86_400))
84 "?->'to' \\? ? OR ?->'cc' \\? ?",
86 ^Pleroma.Constants.as_public(),
88 ^Pleroma.Constants.as_public()
90 where: o.inserted_at < ^time_deadline,
92 fragment("split_part(?->>'actor', '/', 3) != ?", o.data, ^Pleroma.Web.Endpoint.host())
94 |> Repo.delete_all(timeout: :infinity)
96 if Keyword.get(options, :vacuum) do
97 Logger.info("Runnning VACUUM FULL")
107 def run(["fix_likes_collections"]) do
112 from(object in Object,
113 where: fragment("(?)->>'likes' is not null", object.data),
114 select: %{id: object.id, likes: fragment("(?)->>'likes'", object.data)}
116 |> Pleroma.RepoStreamer.chunk_stream(100)
117 |> Stream.each(fn objects ->
120 |> Enum.filter(fn object -> object.likes |> Jason.decode!() |> is_map() end)
124 |> where([object], object.id in ^ids)
129 "safe_jsonb_set(?, '{likes}', '[]'::jsonb, true)",
134 |> Repo.update_all([], timeout: :infinity)