3bba70b7b42d31783f78083cf9ff1f7ab33705c1
[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 defp check_migrations_applied!(:ok) do
28 unless Pleroma.Config.get(
29 [:i_am_aware_this_may_cause_data_loss, :disable_migration_check],
30 false
31 ) do
32 {_, res, _} =
33 Ecto.Migrator.with_repo(Pleroma.Repo, fn repo ->
34 down_migrations =
35 Ecto.Migrator.migrations(repo)
36 |> Enum.reject(fn
37 {:up, _, _} -> true
38 {:down, _, _} -> false
39 end)
40
41 if length(down_migrations) > 0 do
42 down_migrations_text =
43 Enum.map(down_migrations, fn {:down, id, name} -> "- #{name} (#{id})\n" end)
44
45 Logger.error(
46 "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"
47 )
48
49 {:error, "Unapplied Migrations detected"}
50 else
51 :ok
52 end
53 end)
54
55 res
56 else
57 :ok
58 end
59 end
60
61 defp check_migrations_applied!(result), do: result
62
63 defp check_rum!(:ok) do
64 {_, res, _} =
65 Ecto.Migrator.with_repo(Pleroma.Repo, fn repo ->
66 migrate =
67 from(o in "columns",
68 where: o.table_name == "objects",
69 where: o.column_name == "fts_content"
70 )
71 |> repo.exists?(prefix: "information_schema")
72
73 setting = Pleroma.Config.get([:database, :rum_enabled], false)
74
75 do_check_rum!(setting, migrate)
76 end)
77
78 res
79 end
80
81 defp check_rum!(result), do: result
82
83 defp do_check_rum!(setting, migrate) do
84 case {setting, migrate} do
85 {true, false} ->
86 Logger.error(
87 "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/`"
88 )
89
90 {:error, "Unapplied RUM Migrations detected"}
91
92 {false, true} ->
93 Logger.error(
94 "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/`"
95 )
96
97 {:error, "RUM Migrations detected"}
98
99 _ ->
100 :ok
101 end
102 end
103 end