Improve error messages
[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_confirmation_accounts!
20 |> check_migrations_applied!()
21 |> check_rum!()
22 |> handle_result()
23 end
24
25 defp handle_result(:ok), do: :ok
26 defp handle_result({:error, message}), do: raise(VerifyError, message: message)
27
28 # Checks account confirmation email
29 #
30 def check_confirmation_accounts!(:ok) do
31 if Pleroma.Config.get([:instance, :account_activation_required]) &&
32 not Pleroma.Config.get([Pleroma.Emails.Mailer, :enabled]) do
33 Logger.error(
34 "Account activation enabled, but no Mailer settings enabled.\nPlease set config :pleroma, :instance, account_activation_required: false\nOtherwise setup and enable Mailer."
35 )
36
37 {:error, "Account activation enabled, but Mailer is disabled. Cannot send confirmation emails."}
38 else
39 :ok
40 end
41 end
42
43 def check_confirmation_accounts!(result), do: result
44
45 # Checks for pending migrations.
46 #
47 def check_migrations_applied!(:ok) do
48 unless Pleroma.Config.get(
49 [:i_am_aware_this_may_cause_data_loss, :disable_migration_check],
50 false
51 ) do
52 {_, res, _} =
53 Ecto.Migrator.with_repo(Pleroma.Repo, fn repo ->
54 down_migrations =
55 Ecto.Migrator.migrations(repo)
56 |> Enum.reject(fn
57 {:up, _, _} -> true
58 {:down, _, _} -> false
59 end)
60
61 if length(down_migrations) > 0 do
62 down_migrations_text =
63 Enum.map(down_migrations, fn {:down, id, name} -> "- #{name} (#{id})\n" end)
64
65 Logger.error(
66 "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"
67 )
68
69 {:error, "Unapplied Migrations detected"}
70 else
71 :ok
72 end
73 end)
74
75 res
76 else
77 :ok
78 end
79 end
80
81 def check_migrations_applied!(result), do: result
82
83 # Checks for settings of RUM indexes.
84 #
85 defp check_rum!(:ok) do
86 {_, res, _} =
87 Ecto.Migrator.with_repo(Pleroma.Repo, fn repo ->
88 migrate =
89 from(o in "columns",
90 where: o.table_name == "objects",
91 where: o.column_name == "fts_content"
92 )
93 |> repo.exists?(prefix: "information_schema")
94
95 setting = Pleroma.Config.get([:database, :rum_enabled], false)
96
97 do_check_rum!(setting, migrate)
98 end)
99
100 res
101 end
102
103 defp check_rum!(result), do: result
104
105 defp do_check_rum!(setting, migrate) do
106 case {setting, migrate} do
107 {true, false} ->
108 Logger.error(
109 "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/`"
110 )
111
112 {:error, "Unapplied RUM Migrations detected"}
113
114 {false, true} ->
115 Logger.error(
116 "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/`"
117 )
118
119 {:error, "RUM Migrations detected"}
120
121 _ ->
122 :ok
123 end
124 end
125 end