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
8 alias Pleroma.Maintenance
14 require Pleroma.Constants
21 @shortdoc "A collection of database related tasks"
22 @moduledoc File.read!("docs/administration/CLI_tasks/database.md")
24 def run(["remove_embedded_objects" | args]) do
34 Logger.info("Removing embedded objects")
37 "update activities set data = safe_jsonb_set(data, '{object}'::text[], data->'object'->'id') where data->'object'->>'id' is not null;",
42 if Keyword.get(options, :vacuum) do
43 Maintenance.vacuum("full")
47 def run(["bump_all_conversations"]) do
49 Conversation.bump_for_all_activities()
52 def run(["update_users_following_followers_counts"]) do
57 from(u in User, select: u)
59 |> Stream.each(&User.update_follower_count/1)
66 def run(["prune_objects" | args]) do
77 deadline = Pleroma.Config.get([:instance, :remote_post_retention_days])
79 Logger.info("Pruning objects older than #{deadline} days")
82 NaiveDateTime.utc_now()
83 |> NaiveDateTime.add(-(deadline * 86_400))
88 "?->'to' \\? ? OR ?->'cc' \\? ?",
90 ^Pleroma.Constants.as_public(),
92 ^Pleroma.Constants.as_public()
94 where: o.inserted_at < ^time_deadline,
96 fragment("split_part(?->>'actor', '/', 3) != ?", o.data, ^Pleroma.Web.Endpoint.host())
98 |> Repo.delete_all(timeout: :infinity)
100 if Keyword.get(options, :vacuum) do
101 Maintenance.vacuum("full")
105 def run(["fix_likes_collections"]) do
108 from(object in Object,
109 where: fragment("(?)->>'likes' is not null", object.data),
110 select: %{id: object.id, likes: fragment("(?)->>'likes'", object.data)}
112 |> Pleroma.Repo.chunk_stream(100, :batches)
113 |> Stream.each(fn objects ->
116 |> Enum.filter(fn object -> object.likes |> Jason.decode!() |> is_map() end)
120 |> where([object], object.id in ^ids)
125 "safe_jsonb_set(?, '{likes}', '[]'::jsonb, true)",
130 |> Repo.update_all([], timeout: :infinity)
135 def run(["vacuum", args]) do
138 Maintenance.vacuum(args)
141 def run(["ensure_expiration"]) do
143 days = Pleroma.Config.get([:mrf_activity_expiration, :days], 365)
146 |> join(:inner, [a], o in Object,
149 "(?->>'id') = COALESCE((?)->'object'->> 'id', (?)->>'object')",
155 |> where(local: true)
156 |> where([a], fragment("(? ->> 'type'::text) = 'Create'", a.data))
157 |> where([_a, o], fragment("?->>'type' = 'Note'", o.data))
158 |> Pleroma.Repo.chunk_stream(100, :batches)
159 |> Stream.each(fn activities ->
160 Enum.each(activities, fn activity ->
163 |> DateTime.from_naive!("Etc/UTC")
164 |> Timex.shift(days: days)
166 Pleroma.Workers.PurgeExpiredActivity.enqueue(%{
167 activity_id: activity.id,
168 expires_at: expires_at
175 def run(["transfer_hashtags"]) do
180 Logger.info("Starting transferring object embedded hashtags to `hashtags` table...")
182 # Note: most objects have Mention-type AS2 tags and no hashtags (but we can't filter them out)
185 left_join: hashtag in assoc(object, :hashtags),
186 where: is_nil(hashtag.id),
188 fragment("(?)->'tag' IS NOT NULL AND (?)->'tag' != '[]'::jsonb", object.data, object.data),
191 tag: fragment("(?)->'tag'", object.data)
194 |> Repo.chunk_stream(100, :one, timeout: :infinity)
195 |> Stream.each(&transfer_object_hashtags(&1))
198 Logger.info("Done transferring hashtags. Please check logs to ensure no errors.")
201 defp transfer_object_hashtags(object) do
202 hashtags = Object.object_data_hashtags(%{"tag" => object.tag})
204 Repo.transaction(fn ->
205 with {:ok, hashtag_records} <- Hashtag.get_or_create_by_names(hashtags) do
206 for hashtag_record <- hashtag_records do
209 "insert into hashtags_objects(hashtag_id, object_id) values ($1, $2);",
210 [hashtag_record.id, object.id]
216 "ERROR: could not link object #{object.id} and hashtag " <>
217 "#{hashtag_record.id}: #{inspect(e)}"
220 Repo.rollback(object.id)
227 error = "ERROR: could not create hashtags for object #{object.id}: #{inspect(e)}"
229 Repo.rollback(object.id)