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
7 alias Pleroma.Maintenance
12 require Pleroma.Constants
16 @shortdoc "A collection of database related tasks"
17 @moduledoc File.read!("docs/administration/CLI_tasks/database.md")
19 def run(["remove_embedded_objects" | args]) do
29 Logger.info("Removing embedded objects")
32 "update activities set data = safe_jsonb_set(data, '{object}'::text[], data->'object'->'id') where data->'object'->>'id' is not null;",
37 if Keyword.get(options, :vacuum) do
38 Maintenance.vacuum("full")
42 def run(["bump_all_conversations"]) do
44 Conversation.bump_for_all_activities()
47 def run(["update_users_following_followers_counts"]) do
52 |> Enum.each(&User.update_follower_count/1)
55 def run(["prune_objects" | args]) do
68 deadline = Pleroma.Config.get([:instance, :remote_post_retention_days])
70 Logger.info("Pruning objects older than #{deadline} days")
73 NaiveDateTime.utc_now()
74 |> NaiveDateTime.add(-(deadline * 86_400))
79 "?->'to' \\? ? OR ?->'cc' \\? ?",
81 ^Pleroma.Constants.as_public(),
83 ^Pleroma.Constants.as_public()
85 where: o.inserted_at < ^time_deadline,
87 fragment("split_part(?->>'actor', '/', 3) != ?", o.data, ^Pleroma.Web.Endpoint.host())
89 |> Repo.delete_all(timeout: :infinity)
91 if Keyword.get(options, :vacuum) do
92 Maintenance.vacuum("full")
96 def run(["fix_likes_collections"]) do
101 from(object in Object,
102 where: fragment("(?)->>'likes' is not null", object.data),
103 select: %{id: object.id, likes: fragment("(?)->>'likes'", object.data)}
105 |> Pleroma.RepoStreamer.chunk_stream(100)
106 |> Stream.each(fn objects ->
109 |> Enum.filter(fn object -> object.likes |> Jason.decode!() |> is_map() end)
113 |> where([object], object.id in ^ids)
118 "safe_jsonb_set(?, '{likes}', '[]'::jsonb, true)",
123 |> Repo.update_all([], timeout: :infinity)
128 def run(["vacuum", args]) do
131 Maintenance.vacuum(args)