diff --git a/lib/ecto/migrator.ex b/lib/ecto/migrator.ex index 90c215a5..38b7b461 100644 --- a/lib/ecto/migrator.ex +++ b/lib/ecto/migrator.ex @@ -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 """ diff --git a/test/ecto/migrator_test.exs b/test/ecto/migrator_test.exs index 704ac90a..d4cb98d8 100644 --- a/test/ecto/migrator_test.exs +++ b/test/ecto/migrator_test.exs @@ -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