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
17 @shortdoc "A collection of database related tasks"
18 @moduledoc File.read!("docs/administration/CLI_tasks/database.md")
20 def run(["remove_embedded_objects" | args]) do
30 Logger.info("Removing embedded objects")
33 "update activities set data = safe_jsonb_set(data, '{object}'::text[], data->'object'->'id') where data->'object'->>'id' is not null;",
38 if Keyword.get(options, :vacuum) do
39 Maintenance.vacuum("full")
43 def run(["bump_all_conversations"]) do
45 Conversation.bump_for_all_activities()
48 def run(["update_users_following_followers_counts"]) do
53 |> Enum.each(&User.update_follower_count/1)
56 def run(["prune_objects" | args]) do
67 deadline = Pleroma.Config.get([:instance, :remote_post_retention_days])
69 Logger.info("Pruning objects older than #{deadline} days")
72 NaiveDateTime.utc_now()
73 |> NaiveDateTime.add(-(deadline * 86_400))
78 "?->'to' \\? ? OR ?->'cc' \\? ?",
80 ^Pleroma.Constants.as_public(),
82 ^Pleroma.Constants.as_public()
84 where: o.inserted_at < ^time_deadline,
86 fragment("split_part(?->>'actor', '/', 3) != ?", o.data, ^Pleroma.Web.Endpoint.host())
88 |> Repo.delete_all(timeout: :infinity)
90 if Keyword.get(options, :vacuum) do
91 Maintenance.vacuum("full")
95 def run(["fix_likes_collections"]) do
98 from(object in Object,
99 where: fragment("(?)->>'likes' is not null", object.data),
100 select: %{id: object.id, likes: fragment("(?)->>'likes'", object.data)}
102 |> Pleroma.Repo.chunk_stream(100, :batches)
103 |> Stream.each(fn objects ->
106 |> Enum.filter(fn object -> object.likes |> Jason.decode!() |> is_map() end)
110 |> where([object], object.id in ^ids)
115 "safe_jsonb_set(?, '{likes}', '[]'::jsonb, true)",
120 |> Repo.update_all([], timeout: :infinity)
125 def run(["vacuum", args]) do
128 Maintenance.vacuum(args)
131 def run(["ensure_expiration"]) do
133 days = Pleroma.Config.get([:mrf_activity_expiration, :days], 365)
136 |> join(:inner, [a], o in Object,
139 "(?->>'id') = COALESCE((?)->'object'->> 'id', (?)->>'object')",
145 |> where(local: true)
146 |> where([a], fragment("(? ->> 'type'::text) = 'Create'", a.data))
147 |> where([_a, o], fragment("?->>'type' = 'Note'", o.data))
148 |> Pleroma.Repo.chunk_stream(100, :batches)
149 |> Stream.each(fn activities ->
150 Enum.each(activities, fn activity ->
153 |> DateTime.from_naive!("Etc/UTC")
154 |> Timex.shift(days: days)
156 Pleroma.Workers.PurgeExpiredActivity.enqueue(%{
157 activity_id: activity.id,
158 expires_at: expires_at