Merge branch 'develop' into tests/mastodon_api_controller.ex
[akkoma] / lib / mix / tasks / pleroma / ecto / ecto.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-onl
4
5 defmodule Mix.Tasks.Pleroma.Ecto do
6 @doc """
7 Ensures the given repository's migrations path exists on the file system.
8 """
9 @spec ensure_migrations_path(Ecto.Repo.t(), Keyword.t()) :: String.t()
10 def ensure_migrations_path(repo, opts) do
11 path = opts[:migrations_path] || Path.join(source_repo_priv(repo), "migrations")
12
13 path =
14 case Path.type(path) do
15 :relative ->
16 Path.join(Application.app_dir(:pleroma), path)
17
18 :absolute ->
19 path
20 end
21
22 if not File.dir?(path) do
23 raise_missing_migrations(Path.relative_to_cwd(path), repo)
24 end
25
26 path
27 end
28
29 @doc """
30 Returns the private repository path relative to the source.
31 """
32 def source_repo_priv(repo) do
33 config = repo.config()
34 priv = config[:priv] || "priv/#{repo |> Module.split() |> List.last() |> Macro.underscore()}"
35 Path.join(Application.app_dir(:pleroma), priv)
36 end
37
38 defp raise_missing_migrations(path, repo) do
39 raise("""
40 Could not find migrations directory #{inspect(path)}
41 for repo #{inspect(repo)}.
42 This may be because you are in a new project and the
43 migration directory has not been created yet. Creating an
44 empty directory at the path above will fix this error.
45 If you expected existing migrations to be found, please
46 make sure your repository has been properly configured
47 and the configured path exists.
48 """)
49 end
50 end