Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/ecto/migrator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,17 @@ defmodule Ecto.Migrator do

@doc """
Gets the migrations path from a repository.

This function accepts an optional second parameter to customize the
migrations directory. This can be used to specify a custom migrations
path.
"""
@spec migrations_path(Ecto.Repo.t) :: String.t
def migrations_path(repo) do
@spec migrations_path(Ecto.Repo.t, String.t) :: String.t
def migrations_path(repo, directory \\ "migrations") do
config = repo.config()
priv = config[:priv] || "priv/#{repo |> Module.split |> List.last |> Macro.underscore}"
app = Keyword.fetch!(config, :otp_app)
Application.app_dir(app, Path.join(priv, "migrations"))
Application.app_dir(app, Path.join(priv, directory))
end

@doc """
Expand Down
14 changes: 14 additions & 0 deletions test/ecto/migrator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,20 @@ defmodule Ecto.MigratorTest do
end
end

describe "migrations_path" do
test "is inferred from the repository name" do
path = migrations_path(TestRepo)
expected = "priv/test_repo/migrations"
assert path == Application.app_dir(TestRepo.config()[:otp_app], expected)
end

test "uses custom directory when provided" do
path = migrations_path(TestRepo, "custom")
expected = "priv/test_repo/custom"
assert path == Application.app_dir(TestRepo.config()[:otp_app], expected)
end
end

describe "with_repo" do
defmodule Repo do
def start_link(opts) do
Expand Down