Merge remote-tracking branch 'upstream/develop' into linkify
[akkoma] / lib / pleroma / application_requirements.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.ApplicationRequirements do
6 @moduledoc """
7 The module represents the collection of validations to runs before start server.
8 """
9
10 defmodule VerifyError, do: defexception([:message])
11
12 import Ecto.Query
13
14 require Logger
15
16 @spec verify!() :: :ok | VerifyError.t()
17 def verify! do
18 :ok
19 |> check_migrations_applied!()
20 |> check_rum!()
21 |> handle_result()
22 end
23
24 defp handle_result(:ok), do: :ok
25 defp handle_result({:error, message}), do: raise(VerifyError, message: message)
26
27 # Checks for pending migrations.
28 #
29 def check_migrations_applied!(:ok) do
30 unless Pleroma.Config.get(
31 [:i_am_aware_this_may_cause_data_loss, :disable_migration_check],
32 false
33 ) do
34 {_, res, _} =
35 Ecto.Migrator.with_repo(Pleroma.Repo, fn repo ->
36 down_migrations =
37 Ecto.Migrator.migrations(repo)
38 |> Enum.reject(fn
39 {:up, _, _} -> true
40 {:down, _, _} -> false
41 end)
42
43 if length(down_migrations) > 0 do
44 down_migrations_text =
45 Enum.map(down_migrations, fn {:down, id, name} -> "- #{name} (#{id})\n" end)
46
47 Logger.error(
48 "The following migrations were not applied:\n#{down_migrations_text}If you want to start Pleroma anyway, set\nconfig :pleroma, :i_am_aware_this_may_cause_data_loss, disable_migration_check: true"
49 )
50
51 {:error, "Unapplied Migrations detected"}
52 else
53 :ok
54 end
55 end)
56
57 res
58 else
59 :ok
60 end
61 end
62
63 def check_migrations_applied!(result), do: result
64
65 # Checks for settings of RUM indexes.
66 #
67 defp check_rum!(:ok) do
68 {_, res, _} =
69 Ecto.Migrator.with_repo(Pleroma.Repo, fn repo ->
70 migrate =
71 from(o in "columns",
72 where: o.table_name == "objects",
73 where: o.column_name == "fts_content"
74 )
75 |> repo.exists?(prefix: "information_schema")
76
77 setting = Pleroma.Config.get([:database, :rum_enabled], false)
78
79 do_check_rum!(setting, migrate)
80 end)
81
82 res
83 end
84
85 defp check_rum!(result), do: result
86
87 defp do_check_rum!(setting, migrate) do
88 case {setting, migrate} do
89 {true, false} ->
90 Logger.error(
91 "Use `RUM` index is enabled, but were not applied migrations for it.\nIf you want to start Pleroma anyway, set\nconfig :pleroma, :database, rum_enabled: false\nOtherwise apply the following migrations:\n`mix ecto.migrate --migrations-path priv/repo/optional_migrations/rum_indexing/`"
92 )
93
94 {:error, "Unapplied RUM Migrations detected"}
95
96 {false, true} ->
97 Logger.error(
98 "Detected applied migrations to use `RUM` index, but `RUM` isn't enable in settings.\nIf you want to use `RUM`, set\nconfig :pleroma, :database, rum_enabled: true\nOtherwise roll `RUM` migrations back.\n`mix ecto.rollback --migrations-path priv/repo/optional_migrations/rum_indexing/`"
99 )
100
101 {:error, "RUM Migrations detected"}
102
103 _ ->
104 :ok
105 end
106 end
107 end