Merge branch 'develop' into admin-be
[akkoma] / lib / pleroma / repo.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Repo do
6 use Ecto.Repo,
7 otp_app: :pleroma,
8 adapter: Ecto.Adapters.Postgres,
9 migration_timestamps: [type: :naive_datetime_usec]
10
11 require Logger
12
13 defmodule Instrumenter do
14 use Prometheus.EctoInstrumenter
15 end
16
17 @doc """
18 Dynamically loads the repository url from the
19 DATABASE_URL environment variable.
20 """
21 def init(_, opts) do
22 {:ok, Keyword.put(opts, :url, System.get_env("DATABASE_URL"))}
23 end
24
25 @doc "find resource based on prepared query"
26 @spec find_resource(Ecto.Query.t()) :: {:ok, struct()} | {:error, :not_found}
27 def find_resource(%Ecto.Query{} = query) do
28 case __MODULE__.one(query) do
29 nil -> {:error, :not_found}
30 resource -> {:ok, resource}
31 end
32 end
33
34 def find_resource(_query), do: {:error, :not_found}
35
36 @doc """
37 Gets association from cache or loads if need
38
39 ## Examples
40
41 iex> Repo.get_assoc(token, :user)
42 %User{}
43
44 """
45 @spec get_assoc(struct(), atom()) :: {:ok, struct()} | {:error, :not_found}
46 def get_assoc(resource, association) do
47 case __MODULE__.preload(resource, association) do
48 %{^association => assoc} when not is_nil(assoc) -> {:ok, assoc}
49 _ -> {:error, :not_found}
50 end
51 end
52
53 def check_migrations_applied!() do
54 unless Pleroma.Config.get(
55 [:i_am_aware_this_may_cause_data_loss, :disable_migration_check],
56 false
57 ) do
58 Ecto.Migrator.with_repo(__MODULE__, fn repo ->
59 down_migrations =
60 Ecto.Migrator.migrations(repo)
61 |> Enum.reject(fn
62 {:up, _, _} -> true
63 {:down, _, _} -> false
64 end)
65
66 if length(down_migrations) > 0 do
67 down_migrations_text =
68 Enum.map(down_migrations, fn {:down, id, name} -> "- #{name} (#{id})\n" end)
69
70 Logger.error(
71 "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"
72 )
73
74 raise Pleroma.Repo.UnappliedMigrationsError
75 end
76 end)
77 else
78 :ok
79 end
80 end
81 end
82
83 defmodule Pleroma.Repo.UnappliedMigrationsError do
84 defexception message: "Unapplied Migrations detected"
85 end