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