Skip to content

Commit 611ced7

Browse files
Allow custom directory in Migrator.migrations_path (#246)
Fixes #245
1 parent cd7f585 commit 611ced7

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

lib/ecto/migrator.ex

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,17 @@ defmodule Ecto.Migrator do
161161

162162
@doc """
163163
Gets the migrations path from a repository.
164+
165+
This function accepts an optional second parameter to customize the
166+
migrations directory. This can be used to specify a custom migrations
167+
path.
164168
"""
165-
@spec migrations_path(Ecto.Repo.t) :: String.t
166-
def migrations_path(repo) do
169+
@spec migrations_path(Ecto.Repo.t, String.t) :: String.t
170+
def migrations_path(repo, directory \\ "migrations") do
167171
config = repo.config()
168172
priv = config[:priv] || "priv/#{repo |> Module.split |> List.last |> Macro.underscore}"
169173
app = Keyword.fetch!(config, :otp_app)
170-
Application.app_dir(app, Path.join(priv, "migrations"))
174+
Application.app_dir(app, Path.join(priv, directory))
171175
end
172176

173177
@doc """

test/ecto/migrator_test.exs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,20 @@ defmodule Ecto.MigratorTest do
698698
end
699699
end
700700

701+
describe "migrations_path" do
702+
test "is inferred from the repository name" do
703+
path = migrations_path(TestRepo)
704+
expected = "priv/test_repo/migrations"
705+
assert path == Application.app_dir(TestRepo.config()[:otp_app], expected)
706+
end
707+
708+
test "uses custom directory when provided" do
709+
path = migrations_path(TestRepo, "custom")
710+
expected = "priv/test_repo/custom"
711+
assert path == Application.app_dir(TestRepo.config()[:otp_app], expected)
712+
end
713+
end
714+
701715
describe "with_repo" do
702716
defmodule Repo do
703717
def start_link(opts) do

0 commit comments

Comments
 (0)