2019-06-19 10:33:33 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-02 20:38:50 +00:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2022-02-26 06:11:42 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
2019-07-09 16:54:13 +00:00
|
|
|
|
2019-06-19 10:33:33 +00:00
|
|
|
defmodule Mix.Tasks.Pleroma.Ecto do
|
|
|
|
@doc """
|
|
|
|
Ensures the given repository's migrations path exists on the file system.
|
|
|
|
"""
|
|
|
|
@spec ensure_migrations_path(Ecto.Repo.t(), Keyword.t()) :: String.t()
|
|
|
|
def ensure_migrations_path(repo, opts) do
|
|
|
|
path = opts[:migrations_path] || Path.join(source_repo_priv(repo), "migrations")
|
|
|
|
|
2019-06-22 01:17:04 +00:00
|
|
|
path =
|
|
|
|
case Path.type(path) do
|
|
|
|
:relative ->
|
|
|
|
Path.join(Application.app_dir(:pleroma), path)
|
|
|
|
|
|
|
|
:absolute ->
|
|
|
|
path
|
|
|
|
end
|
|
|
|
|
2019-06-19 10:33:33 +00:00
|
|
|
if not File.dir?(path) do
|
|
|
|
raise_missing_migrations(Path.relative_to_cwd(path), repo)
|
|
|
|
end
|
|
|
|
|
|
|
|
path
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns the private repository path relative to the source.
|
|
|
|
"""
|
|
|
|
def source_repo_priv(repo) do
|
|
|
|
config = repo.config()
|
|
|
|
priv = config[:priv] || "priv/#{repo |> Module.split() |> List.last() |> Macro.underscore()}"
|
2019-06-22 01:17:04 +00:00
|
|
|
Path.join(Application.app_dir(:pleroma), priv)
|
2019-06-19 10:33:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
defp raise_missing_migrations(path, repo) do
|
|
|
|
raise("""
|
|
|
|
Could not find migrations directory #{inspect(path)}
|
|
|
|
for repo #{inspect(repo)}.
|
|
|
|
This may be because you are in a new project and the
|
|
|
|
migration directory has not been created yet. Creating an
|
|
|
|
empty directory at the path above will fix this error.
|
|
|
|
If you expected existing migrations to be found, please
|
|
|
|
make sure your repository has been properly configured
|
|
|
|
and the configured path exists.
|
|
|
|
""")
|
|
|
|
end
|
|
|
|
end
|