Merge branch 'develop' into 'fix/extended-nickname-format-default-config'
[akkoma] / lib / mix / tasks / pleroma / database.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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.Object
8 alias Pleroma.Repo
9 alias Pleroma.User
10 require Logger
11 require Pleroma.Constants
12 import Mix.Pleroma
13 use Mix.Task
14
15 @shortdoc "A collection of database related tasks"
16 @moduledoc File.read!("docs/administration/CLI_tasks/database.md")
17
18 def run(["remove_embedded_objects" | args]) do
19 {options, [], []} =
20 OptionParser.parse(
21 args,
22 strict: [
23 vacuum: :boolean
24 ]
25 )
26
27 start_pleroma()
28 Logger.info("Removing embedded objects")
29
30 Repo.query!(
31 "update activities set data = jsonb_set(data, '{object}'::text[], data->'object'->'id') where data->'object'->>'id' is not null;",
32 [],
33 timeout: :infinity
34 )
35
36 if Keyword.get(options, :vacuum) do
37 Logger.info("Runnning VACUUM FULL")
38
39 Repo.query!(
40 "vacuum full;",
41 [],
42 timeout: :infinity
43 )
44 end
45 end
46
47 def run(["bump_all_conversations"]) do
48 start_pleroma()
49 Conversation.bump_for_all_activities()
50 end
51
52 def run(["update_users_following_followers_counts"]) do
53 start_pleroma()
54
55 users = Repo.all(User)
56 Enum.each(users, &User.remove_duplicated_following/1)
57 Enum.each(users, &User.update_follower_count/1)
58 end
59
60 def run(["prune_objects" | args]) do
61 import Ecto.Query
62
63 {options, [], []} =
64 OptionParser.parse(
65 args,
66 strict: [
67 vacuum: :boolean
68 ]
69 )
70
71 start_pleroma()
72
73 deadline = Pleroma.Config.get([:instance, :remote_post_retention_days])
74
75 Logger.info("Pruning objects older than #{deadline} days")
76
77 time_deadline =
78 NaiveDateTime.utc_now()
79 |> NaiveDateTime.add(-(deadline * 86_400))
80
81 from(o in Object,
82 where:
83 fragment(
84 "?->'to' \\? ? OR ?->'cc' \\? ?",
85 o.data,
86 ^Pleroma.Constants.as_public(),
87 o.data,
88 ^Pleroma.Constants.as_public()
89 ),
90 where: o.inserted_at < ^time_deadline,
91 where:
92 fragment("split_part(?->>'actor', '/', 3) != ?", o.data, ^Pleroma.Web.Endpoint.host())
93 )
94 |> Repo.delete_all(timeout: :infinity)
95
96 if Keyword.get(options, :vacuum) do
97 Logger.info("Runnning VACUUM FULL")
98
99 Repo.query!(
100 "vacuum full;",
101 [],
102 timeout: :infinity
103 )
104 end
105 end
106
107 def run(["fix_likes_collections"]) do
108 import Ecto.Query
109
110 start_pleroma()
111
112 from(object in Object,
113 where: fragment("(?)->>'likes' is not null", object.data),
114 select: %{id: object.id, likes: fragment("(?)->>'likes'", object.data)}
115 )
116 |> Pleroma.RepoStreamer.chunk_stream(100)
117 |> Stream.each(fn objects ->
118 ids =
119 objects
120 |> Enum.filter(fn object -> object.likes |> Jason.decode!() |> is_map() end)
121 |> Enum.map(& &1.id)
122
123 Object
124 |> where([object], object.id in ^ids)
125 |> update([object],
126 set: [
127 data:
128 fragment(
129 "jsonb_set(?, '{likes}', '[]'::jsonb, true)",
130 object.data
131 )
132 ]
133 )
134 |> Repo.update_all([], timeout: :infinity)
135 end)
136 |> Stream.run()
137 end
138 end