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 from(u in User, select: u)
55 |> Stream.each(&User.update_follower_count/1)
62 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 Maintenance.vacuum("full")
101 def run(["fix_likes_collections"]) do
104 from(object in Object,
105 where: fragment("(?)->>'likes' is not null", object.data),
106 select: %{id: object.id, likes: fragment("(?)->>'likes'", object.data)}
108 |> Pleroma.Repo.chunk_stream(100, :batches)
109 |> Stream.each(fn objects ->
112 |> Enum.filter(fn object -> object.likes |> Jason.decode!() |> is_map() end)
116 |> where([object], object.id in ^ids)
121 "safe_jsonb_set(?, '{likes}', '[]'::jsonb, true)",
126 |> Repo.update_all([], timeout: :infinity)
131 def run(["fill_old_hashtags", month_limit]) do
136 month_limit = String.to_integer(month_limit)
138 if month_limit < 1 do
139 shell_error("Invalid `month_limit` argument, needs to be greater than 1")
141 time_limit = DateTime.utc_now() |> Timex.shift(months: -month_limit)
145 where: fragment("(?)->>'hashtags' is null", o.data),
146 where: fragment("(?)->>'tag' != '[]'", o.data),
147 where: o.inserted_at < ^time_limit,
148 select: %{id: o.id, tag: fragment("(?)->>'tag'", o.data)}
150 |> Pleroma.Repo.chunk_stream(200, :batches)
151 |> Stream.each(fn objects ->
152 Repo.transaction(fn ->
153 objects_first = objects |> List.first()
154 objects_last = objects |> List.last()
157 "fill_old_hashtags: #{objects_first.id} (#{objects_first.inserted_at}) -- #{
159 } (#{objects_last.inserted_at})"
163 |> Enum.map(fn object ->
167 |> Enum.filter(&is_bitstring(&1))
170 |> where([o], o.id == ^object.id)
172 set: [data: fragment("safe_jsonb_set(?, '{hashtags}', ?, true)", o.data, ^tags)]
174 |> Repo.update_all([], timeout: :infinity)
182 def run(["vacuum", args]) do
185 Maintenance.vacuum(args)
188 def run(["ensure_expiration"]) do
190 days = Pleroma.Config.get([:mrf_activity_expiration, :days], 365)
193 |> join(:inner, [a], o in Object,
196 "(?->>'id') = COALESCE((?)->'object'->> 'id', (?)->>'object')",
202 |> where(local: true)
203 |> where([a], fragment("(? ->> 'type'::text) = 'Create'", a.data))
204 |> where([_a, o], fragment("?->>'type' = 'Note'", o.data))
205 |> Pleroma.Repo.chunk_stream(100, :batches)
206 |> Stream.each(fn activities ->
207 Enum.each(activities, fn activity ->
210 |> DateTime.from_naive!("Etc/UTC")
211 |> Timex.shift(days: days)
213 Pleroma.Workers.PurgeExpiredActivity.enqueue(%{
214 activity_id: activity.id,
215 expires_at: expires_at