1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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(["vacuum", args]) do
134 Maintenance.vacuum(args)
137 def run(["ensure_expiration"]) do
139 days = Pleroma.Config.get([:mrf_activity_expiration, :days], 365)
142 |> join(:inner, [a], o in Object,
145 "(?->>'id') = COALESCE((?)->'object'->> 'id', (?)->>'object')",
151 |> where(local: true)
152 |> where([a], fragment("(? ->> 'type'::text) = 'Create'", a.data))
153 |> where([_a, o], fragment("?->>'type' = 'Note'", o.data))
154 |> Pleroma.Repo.chunk_stream(100, :batches)
155 |> Stream.each(fn activities ->
156 Enum.each(activities, fn activity ->
159 |> DateTime.from_naive!("Etc/UTC")
160 |> Timex.shift(days: days)
162 Pleroma.Workers.PurgeExpiredActivity.enqueue(%{
163 activity_id: activity.id,
164 expires_at: expires_at
171 def run(["set_text_search_config", tsconfig]) do
173 %{rows: [[tsc]]} = Ecto.Adapters.SQL.query!(Pleroma.Repo, "SHOW default_text_search_config;")
174 shell_info("Current default_text_search_config: #{tsc}")
176 %{rows: [[db]]} = Ecto.Adapters.SQL.query!(Pleroma.Repo, "SELECT current_database();")
177 shell_info("Update default_text_search_config: #{tsconfig}")
180 Ecto.Adapters.SQL.query!(
182 "ALTER DATABASE #{db} SET default_text_search_config = '#{tsconfig}';"
185 # non-exist config will not raise excpetion but only give >0 messages
186 if length(msg) > 0 do
187 shell_info("Error: #{inspect(msg, pretty: true)}")
189 rum_enabled = Pleroma.Config.get([:database, :rum_enabled])
190 shell_info("Recreate index, RUM: #{rum_enabled}")
192 # Note SQL below needs to be kept up-to-date with latest GIN or RUM index definition in future
194 Ecto.Adapters.SQL.query!(
196 "CREATE OR REPLACE FUNCTION objects_fts_update() RETURNS trigger AS $$ BEGIN
197 new.fts_content := to_tsvector(new.data->>'content');
203 shell_info("Refresh RUM index")
204 Ecto.Adapters.SQL.query!(Pleroma.Repo, "UPDATE objects SET updated_at = NOW();")
206 Ecto.Adapters.SQL.query!(Pleroma.Repo, "DROP INDEX IF EXISTS objects_fts;")
208 Ecto.Adapters.SQL.query!(
210 "CREATE INDEX objects_fts ON objects USING gin(to_tsvector('#{tsconfig}', data->>'content')); "