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