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
17 changes: 9 additions & 8 deletions lib/ecto/migration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1096,14 +1096,15 @@ defmodule Ecto.Migration do
struct(%Constraint{table: table, name: name}, opts)
end

@doc """
Executes queue migration commands.

Reverses the order in which commands are executed when doing a rollback
on a `change/0` function and resets the commands queue.
"""
def flush do
Runner.flush
@doc "Executes queue migration commands."
defmacro flush do
quote do
if direction() == :down and not function_exported?(__MODULE__, :down, 0) do
raise "calling flush() inside change when doing rollback is not supported."
else
Runner.flush()
end
end
end

# Validation helpers
Expand Down
25 changes: 25 additions & 0 deletions test/ecto/migrator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,19 @@ defmodule Ecto.MigratorTest do
use Ecto.Repo, otp_app: :ecto_sql, adapter: EctoSQL.TestAdapter
end

defmodule EmptyUpDownMigration do
use Ecto.Migration

def up, do: flush()
def down, do: flush()
end

defmodule EmptyChangeMigration do
use Ecto.Migration

def change, do: flush()
end

Application.put_env(:ecto_sql, MigrationSourceRepo, [migration_source: "my_schema_migrations"])

setup do
Expand Down Expand Up @@ -156,6 +169,18 @@ defmodule Ecto.MigratorTest do
"""
end

@tag :current
test "flush" do
num = System.unique_integer([:positive])
assert :ok == up(TestRepo, num, EmptyUpDownMigration, log: false)
assert :ok == down(TestRepo, num, EmptyUpDownMigration, log: false)
assert :ok == up(TestRepo, num, EmptyChangeMigration, log: false)
message = "calling flush() inside change when doing rollback is not supported."
assert_raise(RuntimeError, message, fn ->
down(TestRepo, num, EmptyChangeMigration, log: false)
end)
end

test "custom schema migrations table is right" do
assert SchemaMigration.get_source(TestRepo) == "schema_migrations"
assert SchemaMigration.get_source(MigrationSourceRepo) == "my_schema_migrations"
Expand Down