2
votes

I'm trying to figure out what kind of situation would require using flush.

As a counter example, this (trivial) situation does not require flush :

defmodule MyRepo.Migrations.TestFlush do
  use Ecto.Migration

  def change do
    create table(:test)
    alter table(:test) do
      add :a, :integer
    end
  end
end

It works both "forwards" and "backwards" using ecto.migrate and ecto.rollback respectively. The second macro "alter table" is dependent on the first macro "create table" completing. flush() is not required to indicate any sort of synchronous dependency here.

I'm trying to define situations where flush() is required to be invoked in order for a particular migration to run successfully.

Here's the docs for flush : https://hexdocs.pm/ecto/Ecto.Migration.html#flush/0

Here's the source code for flush : https://github.com/elixir-ecto/ecto/blob/master/lib/ecto/migration/runner.ex#L96

Note the source code for flush reverses the "commands", but it doesn't define what constitutes a command.

thanks!

1

1 Answers

2
votes

You have to use flush, e.g. when your migration after flush (M2) depends on migrations before flush (M1). To be clear, M1 and M2 are both part of the same migration file and both are defined and executed within change/0.

Let's say M1 creates a table, adds columns to the database and inserts some data that are part of your migration code. M2 then queries the table and columns from M1 and generates a new table with the same data. Then, if you don't run flush/0 between M1 and M2, you cannot perform a query on the column of M1 since the migration is not executed yet.