diff --git a/lib/ecto/migration.ex b/lib/ecto/migration.ex index 2d1d8228..d791cdc7 100644 --- a/lib/ecto/migration.ex +++ b/lib/ecto/migration.ex @@ -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 diff --git a/test/ecto/migrator_test.exs b/test/ecto/migrator_test.exs index de5cbc8a..82c3b889 100644 --- a/test/ecto/migrator_test.exs +++ b/test/ecto/migrator_test.exs @@ -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 @@ -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"