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"
17 def run(["remove_embedded_objects" | args]) do
27 Logger.info("Removing embedded objects")
30 "update activities set data = jsonb_set(data, '{object}'::text[], data->'object'->'id') where data->'object'->>'id' is not null;",
35 if Keyword.get(options, :vacuum) do
36 Logger.info("Runnning VACUUM FULL")
46 def run(["bump_all_conversations"]) do
48 Conversation.bump_for_all_activities()
51 def run(["update_users_following_followers_counts"]) do
54 users = Repo.all(User)
55 Enum.each(users, &User.remove_duplicated_following/1)
56 Enum.each(users, &User.update_follower_count/1)
59 def run(["prune_objects" | args]) do
72 deadline = Pleroma.Config.get([:instance, :remote_post_retention_days])
74 Logger.info("Pruning objects older than #{deadline} days")
77 NaiveDateTime.utc_now()
78 |> NaiveDateTime.add(-(deadline * 86_400))
83 "?->'to' \\? ? OR ?->'cc' \\? ?",
85 ^Pleroma.Constants.as_public(),
87 ^Pleroma.Constants.as_public()
89 where: o.inserted_at < ^time_deadline,
91 fragment("split_part(?->>'actor', '/', 3) != ?", o.data, ^Pleroma.Web.Endpoint.host())
93 |> Repo.delete_all(timeout: :infinity)
95 if Keyword.get(options, :vacuum) do
96 Logger.info("Runnning VACUUM FULL")
106 def run(["fix_likes_collections"]) do
111 from(object in Object,
112 where: fragment("(?)->>'likes' is not null", object.data),
113 select: %{id: object.id, likes: fragment("(?)->>'likes'", object.data)}
115 |> Pleroma.RepoStreamer.chunk_stream(100)
116 |> Stream.each(fn objects ->
119 |> Enum.filter(fn object -> object.likes |> Jason.decode!() |> is_map() end)
123 |> where([object], object.id in ^ids)
128 "jsonb_set(?, '{likes}', '[]'::jsonb, true)",
133 |> Repo.update_all([], timeout: :infinity)