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
13 require Pleroma.Constants
20 @shortdoc "A collection of database related tasks"
21 @moduledoc File.read!("docs/administration/CLI_tasks/database.md")
23 # Rolls back a specific migration (leaving subsequent migrations applied)
24 # Based on https://stackoverflow.com/a/53825840
25 def run(["rollback", version]) do
28 version = String.to_integer(version)
29 re = ~r/^#{version}_.*\.exs/
30 path = Application.app_dir(:pleroma, Path.join(["priv", "repo", "migrations"]))
33 with {:find, "" <> file} <- {:find, Enum.find(File.ls!(path), &String.match?(&1, re))},
34 {:compile, [{mod, _} | _]} <- {:compile, Code.compile_file(Path.join(path, file))},
35 {:rollback, :ok} <- {:rollback, Ecto.Migrator.down(Repo, version, mod)} do
36 {:ok, "Reversed migration: #{file}"}
38 {:find, _} -> {:error, "No migration found with version prefix: #{version}"}
39 {:compile, e} -> {:error, "Problem compiling migration module: #{inspect(e)}"}
40 {:rollback, e} -> {:error, "Problem reversing migration: #{inspect(e)}"}
41 e -> {:error, "Something unexpected happened: #{inspect(e)}"}
47 def run(["remove_embedded_objects" | args]) do
57 Logger.info("Removing embedded objects")
60 "update activities set data = safe_jsonb_set(data, '{object}'::text[], data->'object'->'id') where data->'object'->>'id' is not null;",
65 if Keyword.get(options, :vacuum) do
66 Maintenance.vacuum("full")
70 def run(["bump_all_conversations"]) do
72 Conversation.bump_for_all_activities()
75 def run(["update_users_following_followers_counts"]) do
80 from(u in User, select: u)
82 |> Stream.each(&User.update_follower_count/1)
89 def run(["prune_objects" | args]) do
100 deadline = Pleroma.Config.get([:instance, :remote_post_retention_days])
102 Logger.info("Pruning objects older than #{deadline} days")
105 NaiveDateTime.utc_now()
106 |> NaiveDateTime.add(-(deadline * 86_400))
111 "?->'to' \\? ? OR ?->'cc' \\? ?",
113 ^Pleroma.Constants.as_public(),
115 ^Pleroma.Constants.as_public()
117 where: o.inserted_at < ^time_deadline,
119 fragment("split_part(?->>'actor', '/', 3) != ?", o.data, ^Pleroma.Web.Endpoint.host())
121 |> Repo.delete_all(timeout: :infinity)
123 if Keyword.get(options, :vacuum) do
124 Maintenance.vacuum("full")
128 def run(["fix_likes_collections"]) do
131 from(object in Object,
132 where: fragment("(?)->>'likes' is not null", object.data),
133 select: %{id: object.id, likes: fragment("(?)->>'likes'", object.data)}
135 |> Pleroma.Repo.chunk_stream(100, :batches)
136 |> Stream.each(fn objects ->
139 |> Enum.filter(fn object -> object.likes |> Jason.decode!() |> is_map() end)
143 |> where([object], object.id in ^ids)
148 "safe_jsonb_set(?, '{likes}', '[]'::jsonb, true)",
153 |> Repo.update_all([], timeout: :infinity)
158 def run(["vacuum", args]) do
161 Maintenance.vacuum(args)
164 def run(["ensure_expiration"]) do
166 days = Pleroma.Config.get([:mrf_activity_expiration, :days], 365)
169 |> join(:inner, [a], o in Object,
172 "(?->>'id') = COALESCE((?)->'object'->> 'id', (?)->>'object')",
178 |> where(local: true)
179 |> where([a], fragment("(? ->> 'type'::text) = 'Create'", a.data))
180 |> where([_a, o], fragment("?->>'type' = 'Note'", o.data))
181 |> Pleroma.Repo.chunk_stream(100, :batches)
182 |> Stream.each(fn activities ->
183 Enum.each(activities, fn activity ->
186 |> DateTime.from_naive!("Etc/UTC")
187 |> Timex.shift(days: days)
189 Pleroma.Workers.PurgeExpiredActivity.enqueue(%{
190 activity_id: activity.id,
191 expires_at: expires_at