Skip to content

Commit

Permalink
Make mix migration tasks responsible for furnishing defaults to Migra…
Browse files Browse the repository at this point in the history
…tor.
  • Loading branch information
Chris Keele committed Dec 15, 2013
1 parent fd8aba7 commit 10c61ce
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 55 deletions.
18 changes: 9 additions & 9 deletions integration_test/pg/migrations_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ defmodule Ecto.Integration.MigrationsTest do
in_tmp fn path ->
create_migration(42, @good_migration)
create_migration(43, @good_migration)
assert [42, 43] = run(TestRepo, path, :up)
assert [42, 43] = run(TestRepo, path, :up, { :all, true })

create_migration(44, @good_migration)
assert [44] = run(TestRepo, path, :up)
assert [44] = run(TestRepo, path, :up, { :all, true })

assert [] = run(TestRepo, path, :up)
assert [] = run(TestRepo, path, :up, { :all, true })

assert Postgrex.Result[num_rows: 3] =
Postgres.query(TestRepo, "SELECT * FROM migrations_test")
Expand Down Expand Up @@ -125,10 +125,10 @@ defmodule Ecto.Integration.MigrationsTest do
create_migration(49, @good_migration),
create_migration(50, @good_migration),
]
assert [49, 50] = run(TestRepo, path, :up)
assert [49, 50] = run(TestRepo, path, :up, { :all, true })
purge migrations

assert [50] = run(TestRepo, path, :down)
assert [50] = run(TestRepo, path, :down, { :step, 1 })
purge migrations

assert Postgrex.Result[num_rows: 1] =
Expand All @@ -145,7 +145,7 @@ defmodule Ecto.Integration.MigrationsTest do
create_migration(52, @good_migration),
]

assert [51, 52] = run(TestRepo, path, :up)
assert [51, 52] = run(TestRepo, path, :up, { :all, true })
purge migrations

assert [52] = run(TestRepo, path, :down, { :to, 52 })
Expand All @@ -164,7 +164,7 @@ defmodule Ecto.Integration.MigrationsTest do
create_migration(53, @good_migration),
create_migration(54, @good_migration),
]
assert [53, 54] = run(TestRepo, path, :up)
assert [53, 54] = run(TestRepo, path, :up, { :all, true })
purge migrations

assert [54, 53] = run(TestRepo, path, :down, { :all, true })
Expand All @@ -173,15 +173,15 @@ defmodule Ecto.Integration.MigrationsTest do
assert Postgrex.Result[num_rows: 0] =
Postgres.query(TestRepo, "SELECT * FROM migrations_test")

assert [53, 54] = run(TestRepo, path, :up)
assert [53, 54] = run(TestRepo, path, :up, { :all, true })
end
end

test "bad migration raises" do
in_tmp fn path ->
create_migration(55, @bad_migration)
assert_raise Postgrex.Error, fn ->
run(TestRepo, path, :up)
run(TestRepo, path, :up, { :all, true })
end
end
end
Expand Down
22 changes: 1 addition & 21 deletions lib/ecto/migrator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,9 @@ defmodule Ecto.Migrator do
repo.adapter.migrate_down(repo, version, commands)
end

@spec strategy_types :: list(atom)
def strategy_types, do: [:to, :step, :all]


@doc """
Apply migrations in a directory to a repository.
Uses the default strategy for each direction:
* ':up' uses `{ :all, true }`
* ':down' uses `{ :step, 1 }`
Consult Migrator.run/4 for information on strategies.
"""
@spec run(Ecto.Repo.t, binary, atom) :: [integer]
def run(repo, directory, direction)

def run(repo, directory, :up) do
run repo, directory, :up, { :all, true }
end
def run(repo, directory, :down) do
run repo, directory, :down, { :step, 1 }
end

@doc """
Apply migrations in a directory to a repository with given strategy.
Expand Down
2 changes: 1 addition & 1 deletion lib/mix/tasks/ecto.migrate.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ defmodule Mix.Tasks.Ecto.Migrate do
ensure_repo(repo)
ensure_started(repo)
{ strategy, _ } = parse_strategy(opts) # We don't use other opts atm
migrator.(repo, migrations_path(repo), :up, strategy)
migrator.(repo, migrations_path(repo), :up, strategy || { :all, true })
end
end
2 changes: 1 addition & 1 deletion lib/mix/tasks/ecto.rollback.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ defmodule Mix.Tasks.Ecto.Rollback do
ensure_repo(repo)
ensure_started(repo)
{ strategy, _ } = parse_strategy(opts) # We don't use other opts atm
migrator.(repo, migrations_path(repo), :down, strategy)
migrator.(repo, migrations_path(repo), :down, strategy || { :step, 1 })
end
end
25 changes: 4 additions & 21 deletions test/ecto/migrator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ defmodule Ecto.MigratorTest do
test "expects files starting with an integer" do
in_tmp fn path ->
create_migration "a_sample.exs"
assert run(ProcessRepo, path, :up) == []
assert run(ProcessRepo, path, :up, { :all, true }) == []
end
end

test "fails if there is no migration in file" do
in_tmp fn path ->
File.write! "13_sample.exs", ":ok"
assert_raise Ecto.MigrationError, "file 13_sample.exs does not contain any Ecto.Migration", fn ->
run(ProcessRepo, path, :up)
run(ProcessRepo, path, :up, { :all, true })
end
end
end
Expand All @@ -73,32 +73,15 @@ defmodule Ecto.MigratorTest do
create_migration "13_hello.exs"
create_migration "13_other.exs"
assert_raise Ecto.MigrationError, "migrations can't be executed, version 13 is duplicated", fn ->
run(ProcessRepo, path, :up)
run(ProcessRepo, path, :up, { :all, true })
end
end
end

test "upwards migrations without strategies runs all" do
in_tmp fn path ->
create_migration "13_up_without_strategies.exs"
create_migration "14_up_without_strategies.exs"
assert run(ProcessRepo, path, :up) == [13, 14]
end
end

test "downwards migrations without strategies revert one" do
in_tmp fn path ->
create_migration "1_down_without_strategies.exs"
create_migration "2_down_without_strategies.exs"
create_migration "3_down_without_strategies.exs"
assert run(ProcessRepo, path, :down) == [3]
end
end

test "upwards migrations skips migrations that are already up" do
in_tmp fn path ->
create_migration "1_sample.exs"
assert run(ProcessRepo, path, :up) == []
assert run(ProcessRepo, path, :up, { :all, true }) == []
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/mix/tasks/ecto.migrate_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ defmodule Mix.Tasks.Ecto.MigrateTest do
assert repo == Repo
assert path == "hello/migrations"
assert direction == :up
assert strategy == nil
assert strategy == { :all, true }
end
end
end
2 changes: 1 addition & 1 deletion test/mix/tasks/ecto.rollback_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ defmodule Mix.Tasks.Ecto.RollbackTest do
assert repo == Repo
assert path == "hello/migrations"
assert direction == :down
assert strategy == nil
assert strategy == { :step, 1 }
end
end
end

0 comments on commit 10c61ce

Please sign in to comment.