Skip to content

Commit e3014c4

Browse files
authored
Pass up/4 opts to Ecto.Migrator (#499)
1 parent 8d409eb commit e3014c4

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

lib/ecto/migrator.ex

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,19 @@ defmodule Ecto.Migrator do
8585
repos: Application.fetch_env!(:my_app, :ecto_repos),
8686
skip: System.get_env("SKIP_MIGRATIONS") == "true"}
8787
88-
To skip migrations you can also pass `skip: true` or as in the example
88+
To skip migrations you can also pass `skip: true` or as in the example
8989
set the environment variable `SKIP_MIGRATIONS` to a truthy value.
9090
91-
To roll back you'd do it normally:
91+
And all other options described in `up/4` are allowed,
92+
for example if you want to log the SQL commands,
93+
and run migrations in a prefix:
94+
95+
{Ecto.Migrator,
96+
repos: Application.fetch_env!(:my_app, :ecto_repos),
97+
log_migrator_sql: true,
98+
prefix: "my_app"}
99+
100+
To roll back you'd do it normally:
92101
93102
$ mix ecto.rollback
94103
@@ -466,33 +475,37 @@ defmodule Ecto.Migrator do
466475
|> collect_migrations(directories)
467476
|> Enum.sort_by(fn {_, version, _} -> version end)
468477
end
469-
478+
470479
use GenServer
471480

472481
@doc """
473482
Runs migrations as part of your supervision tree.
474483
475484
## Options
476485
477-
* `:repos` - Required option to tell the migrator which Repo's to
486+
* `:repos` - Required option to tell the migrator which Repo's to
478487
migrate. Example: `repos: [MyApp.Repo]`
479-
* `:skip` - Option to skip migrations.
480-
Defaults to `false`.
488+
489+
* `:skip` - Option to skip migrations. Defaults to `false`.
490+
491+
Plus all other options described in `up/4`.
492+
493+
See "Example: Running migrations on application startup" for more info.
481494
"""
482495
def start_link(opts) do
483496
GenServer.start_link(__MODULE__, opts, name: __MODULE__)
484497
end
485498

486499
@impl true
487500
def init(opts) do
488-
repos = Keyword.fetch!(opts, :repos)
489-
490-
skip? = Keyword.get(opts, :skip, false)
491-
migrator = Keyword.get(opts, :migrator, &Ecto.Migrator.run/3)
501+
{repos, opts} = Keyword.pop!(opts, :repos)
502+
{skip?, opts} = Keyword.pop(opts, :skip, false)
503+
{migrator, opts} = Keyword.pop(opts, :migrator, &Ecto.Migrator.run/3)
504+
opts = Keyword.put(opts, :all, true)
492505

493506
unless skip? do
494507
for repo <- repos do
495-
{:ok, _, _} = with_repo(repo, &migrator.(&1, :up, all: true))
508+
{:ok, _, _} = with_repo(repo, &migrator.(&1, :up, opts))
496509
end
497510
end
498511

test/ecto/migrator_test.exs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ defmodule Ecto.MigratorTest do
861861
end
862862
end
863863

864-
describe "gen_server" do
864+
describe "gen_server" do
865865
test "runs the migrator with repos config" do
866866
migrator = fn repo, _, _ ->
867867
assert TestRepo == repo
@@ -871,6 +871,16 @@ defmodule Ecto.MigratorTest do
871871
assert {:ok, :undefined} = start_supervised({Ecto.Migrator, [repos: [TestRepo], migrator: migrator]})
872872
end
873873

874+
test "runs the migrator with extra opts" do
875+
migrator = fn repo, _, opts ->
876+
assert TestRepo == repo
877+
assert opts == [all: true, prefix: "foo"]
878+
[]
879+
end
880+
881+
assert {:ok, :undefined} = start_supervised({Ecto.Migrator, [repos: [TestRepo], migrator: migrator, skip: false, prefix: "foo"]})
882+
end
883+
874884
test "skip is set" do
875885
migrator = fn repo, _, _ ->
876886
refute TestRepo == repo

0 commit comments

Comments
 (0)