0f428ca0340e6881639bc5d8d5ac88dbfae23945
[akkoma] / lib / mix / tasks / pleroma / database.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Mix.Tasks.Pleroma.Database do
6 alias Pleroma.Conversation
7 alias Pleroma.Maintenance
8 alias Pleroma.Object
9 alias Pleroma.Repo
10 alias Pleroma.User
11
12 require Logger
13 require Pleroma.Constants
14
15 import Ecto.Query
16 import Mix.Pleroma
17
18 use Mix.Task
19
20 @shortdoc "A collection of database related tasks"
21 @moduledoc File.read!("docs/docs/administration/CLI_tasks/database.md")
22
23 def run(["remove_embedded_objects" | args]) do
24 {options, [], []} =
25 OptionParser.parse(
26 args,
27 strict: [
28 vacuum: :boolean
29 ]
30 )
31
32 start_pleroma()
33 Logger.info("Removing embedded objects")
34
35 Repo.query!(
36 "update activities set data = safe_jsonb_set(data, '{object}'::text[], data->'object'->'id') where data->'object'->>'id' is not null;",
37 [],
38 timeout: :infinity
39 )
40
41 if Keyword.get(options, :vacuum) do
42 Maintenance.vacuum("full")
43 end
44 end
45
46 def run(["bump_all_conversations"]) do
47 start_pleroma()
48 Conversation.bump_for_all_activities()
49 end
50
51 def run(["update_users_following_followers_counts"]) do
52 start_pleroma()
53
54 Repo.transaction(
55 fn ->
56 from(u in User, select: u)
57 |> Repo.stream()
58 |> Stream.each(&User.update_follower_count/1)
59 |> Stream.run()
60 end,
61 timeout: :infinity
62 )
63 end
64
65 def run(["prune_objects" | args]) do
66 {options, [], []} =
67 OptionParser.parse(
68 args,
69 strict: [
70 vacuum: :boolean,
71 keep_threads: :boolean,
72 keep_non_public: :boolean,
73 prune_orphaned_activities: :boolean
74 ]
75 )
76
77 start_pleroma()
78
79 deadline = Pleroma.Config.get([:instance, :remote_post_retention_days])
80 time_deadline = NaiveDateTime.utc_now() |> NaiveDateTime.add(-(deadline * 86_400))
81
82 log_message = "Pruning objects older than #{deadline} days"
83
84 log_message =
85 if Keyword.get(options, :keep_non_public) do
86 log_message <> ", keeping non public posts"
87 else
88 log_message
89 end
90
91 log_message =
92 if Keyword.get(options, :keep_threads) do
93 log_message <> ", keeping threads intact"
94 else
95 log_message
96 end
97
98 log_message =
99 if Keyword.get(options, :prune_orphaned_activities) do
100 log_message <> ", pruning orphaned activities"
101 else
102 log_message
103 end
104
105 log_message =
106 if Keyword.get(options, :vacuum) do
107 log_message <>
108 ", doing a full vacuum (you shouldn't do this as a recurring maintanance task)"
109 else
110 log_message
111 end
112
113 Logger.info(log_message)
114
115 if Keyword.get(options, :keep_threads) do
116 # We want to delete objects from threads where
117 # 1. the newest post is still old
118 # 2. none of the activities is local
119 # 3. none of the activities is bookmarked
120 # 4. optionally none of the posts is non-public
121 deletable_context =
122 if Keyword.get(options, :keep_non_public) do
123 Pleroma.Activity
124 |> join(:left, [a], b in Pleroma.Bookmark, on: a.id == b.activity_id)
125 |> group_by([a], fragment("? ->> 'context'::text", a.data))
126 |> having(
127 [a],
128 not fragment(
129 # Posts (checked on Create Activity) is non-public
130 "bool_or((not(?->'to' \\? ? OR ?->'cc' \\? ?)) and ? ->> 'type' = 'Create')",
131 a.data,
132 ^Pleroma.Constants.as_public(),
133 a.data,
134 ^Pleroma.Constants.as_public(),
135 a.data
136 )
137 )
138 else
139 Pleroma.Activity
140 |> join(:left, [a], b in Pleroma.Bookmark, on: a.id == b.activity_id)
141 |> group_by([a], fragment("? ->> 'context'::text", a.data))
142 end
143 |> having([a], max(a.updated_at) < ^time_deadline)
144 |> having([a], not fragment("bool_or(?)", a.local))
145 |> having([_, b], fragment("max(?::text) is null", b.id))
146 |> select([a], fragment("? ->> 'context'::text", a.data))
147
148 Pleroma.Object
149 |> where([o], fragment("? ->> 'context'::text", o.data) in subquery(deletable_context))
150 else
151 if Keyword.get(options, :keep_non_public) do
152 Pleroma.Object
153 |> where(
154 [o],
155 fragment(
156 "?->'to' \\? ? OR ?->'cc' \\? ?",
157 o.data,
158 ^Pleroma.Constants.as_public(),
159 o.data,
160 ^Pleroma.Constants.as_public()
161 )
162 )
163 else
164 Pleroma.Object
165 end
166 |> where([o], o.updated_at < ^time_deadline)
167 |> where(
168 [o],
169 fragment("split_part(?->>'actor', '/', 3) != ?", o.data, ^Pleroma.Web.Endpoint.host())
170 )
171 end
172 |> Repo.delete_all(timeout: :infinity)
173
174 if Keyword.get(options, :prune_orphaned_activities) do
175 """
176 delete from public.activities
177 where id in (
178 select a.id from public.activities a
179 left join public.objects o on a.data ->> 'object' = o.data ->> 'id'
180 left join public.activities a2 on a.data ->> 'object' = a2.data ->> 'id'
181 left join public.users u on a.data ->> 'object' = u.ap_id
182 -- Only clean up remote activities
183 where not a.local
184 -- For now we only focus on activities with direct links to objects
185 -- e.g. not json objects (in case of embedded objects) or json arrays (in case of multiple objects)
186 and jsonb_typeof(a."data" -> 'object') = 'string'
187 -- Find Activities that don't have existing objects
188 and o.id is null
189 and a2.id is null
190 and u.id is null
191 )
192 """
193 |> Repo.query()
194 end
195
196 prune_hashtags_query = """
197 DELETE FROM hashtags AS ht
198 WHERE NOT EXISTS (
199 SELECT 1 FROM hashtags_objects hto
200 WHERE ht.id = hto.hashtag_id)
201 """
202
203 Repo.query(prune_hashtags_query)
204
205 if Keyword.get(options, :vacuum) do
206 Maintenance.vacuum("full")
207 end
208 end
209
210 def run(["prune_task"]) do
211 start_pleroma()
212
213 nil
214 |> Pleroma.Workers.Cron.PruneDatabaseWorker.perform()
215 end
216
217 def run(["fix_likes_collections"]) do
218 start_pleroma()
219
220 from(object in Object,
221 where: fragment("(?)->>'likes' is not null", object.data),
222 select: %{id: object.id, likes: fragment("(?)->>'likes'", object.data)}
223 )
224 |> Pleroma.Repo.chunk_stream(100, :batches)
225 |> Stream.each(fn objects ->
226 ids =
227 objects
228 |> Enum.filter(fn object -> object.likes |> Jason.decode!() |> is_map() end)
229 |> Enum.map(& &1.id)
230
231 Object
232 |> where([object], object.id in ^ids)
233 |> update([object],
234 set: [
235 data:
236 fragment(
237 "safe_jsonb_set(?, '{likes}', '[]'::jsonb, true)",
238 object.data
239 )
240 ]
241 )
242 |> Repo.update_all([], timeout: :infinity)
243 end)
244 |> Stream.run()
245 end
246
247 def run(["vacuum", args]) do
248 start_pleroma()
249
250 Maintenance.vacuum(args)
251 end
252
253 def run(["ensure_expiration"]) do
254 start_pleroma()
255 days = Pleroma.Config.get([:mrf_activity_expiration, :days], 365)
256
257 Pleroma.Activity
258 |> join(:inner, [a], o in Object,
259 on:
260 fragment(
261 "(?->>'id') = COALESCE((?)->'object'->> 'id', (?)->>'object')",
262 o.data,
263 a.data,
264 a.data
265 )
266 )
267 |> where(local: true)
268 |> where([a], fragment("(? ->> 'type'::text) = 'Create'", a.data))
269 |> where([_a, o], fragment("?->>'type' = 'Note'", o.data))
270 |> Pleroma.Repo.chunk_stream(100, :batches)
271 |> Stream.each(fn activities ->
272 Enum.each(activities, fn activity ->
273 expires_at =
274 activity.inserted_at
275 |> DateTime.from_naive!("Etc/UTC")
276 |> Timex.shift(days: days)
277
278 Pleroma.Workers.PurgeExpiredActivity.enqueue(%{
279 activity_id: activity.id,
280 expires_at: expires_at
281 })
282 end)
283 end)
284 |> Stream.run()
285 end
286
287 def run(["set_text_search_config", tsconfig]) do
288 start_pleroma()
289 %{rows: [[tsc]]} = Ecto.Adapters.SQL.query!(Pleroma.Repo, "SHOW default_text_search_config;")
290 shell_info("Current default_text_search_config: #{tsc}")
291
292 %{rows: [[db]]} = Ecto.Adapters.SQL.query!(Pleroma.Repo, "SELECT current_database();")
293 shell_info("Update default_text_search_config: #{tsconfig}")
294
295 %{messages: msg} =
296 Ecto.Adapters.SQL.query!(
297 Pleroma.Repo,
298 "ALTER DATABASE #{db} SET default_text_search_config = '#{tsconfig}';"
299 )
300
301 # non-exist config will not raise excpetion but only give >0 messages
302 if length(msg) > 0 do
303 shell_info("Error: #{inspect(msg, pretty: true)}")
304 else
305 rum_enabled = Pleroma.Config.get([:database, :rum_enabled])
306 shell_info("Recreate index, RUM: #{rum_enabled}")
307
308 # Note SQL below needs to be kept up-to-date with latest GIN or RUM index definition in future
309 if rum_enabled do
310 Ecto.Adapters.SQL.query!(
311 Pleroma.Repo,
312 "CREATE OR REPLACE FUNCTION objects_fts_update() RETURNS trigger AS $$ BEGIN
313 new.fts_content := to_tsvector(new.data->>'content');
314 RETURN new;
315 END
316 $$ LANGUAGE plpgsql",
317 [],
318 timeout: :infinity
319 )
320
321 shell_info("Refresh RUM index")
322 Ecto.Adapters.SQL.query!(Pleroma.Repo, "UPDATE objects SET updated_at = NOW();")
323 else
324 Ecto.Adapters.SQL.query!(Pleroma.Repo, "DROP INDEX IF EXISTS objects_fts;")
325
326 Ecto.Adapters.SQL.query!(
327 Pleroma.Repo,
328 "CREATE INDEX CONCURRENTLY objects_fts ON objects USING gin(to_tsvector('#{tsconfig}', data->>'content')); ",
329 [],
330 timeout: :infinity
331 )
332 end
333
334 shell_info('Done.')
335 end
336 end
337
338 # Rolls back a specific migration (leaving subsequent migrations applied).
339 # WARNING: imposes a risk of unrecoverable data loss — proceed at your own responsibility.
340 # Based on https://stackoverflow.com/a/53825840
341 def run(["rollback", version]) do
342 prompt = "SEVERE WARNING: this operation may result in unrecoverable data loss. Continue?"
343
344 if shell_prompt(prompt, "n") in ~w(Yn Y y) do
345 {_, result, _} =
346 Ecto.Migrator.with_repo(Pleroma.Repo, fn repo ->
347 version = String.to_integer(version)
348 re = ~r/^#{version}_.*\.exs/
349 path = Ecto.Migrator.migrations_path(repo)
350
351 with {_, "" <> file} <- {:find, Enum.find(File.ls!(path), &String.match?(&1, re))},
352 {_, [{mod, _} | _]} <- {:compile, Code.compile_file(Path.join(path, file))},
353 {_, :ok} <- {:rollback, Ecto.Migrator.down(repo, version, mod)} do
354 {:ok, "Reversed migration: #{file}"}
355 else
356 {:find, _} -> {:error, "No migration found with version prefix: #{version}"}
357 {:compile, e} -> {:error, "Problem compiling migration module: #{inspect(e)}"}
358 {:rollback, e} -> {:error, "Problem reversing migration: #{inspect(e)}"}
359 end
360 end)
361
362 shell_info(inspect(result))
363 end
364 end
365 end