Search code examples
elixirphoenix-frameworkectoex-unit

How to test Ecto migrations


I have recently written some Ecto migrations in a Phoenix application.
However, I always tested the migrations manually.

As our migrations grow more complex and need to alter data on our live system, I'd really like to write automated tests before deploying them.

Automatic tests would require:

  1. Reset the database to a point right before the migration
  2. Run migration older than the migration to be tested
  3. Prepare data and insert it to the database
  4. Run migration
  5. Verify results match expectations
  6. Cleanup database to ensure upcoming tests don't clash with the data

We're using ExUnit and ESpec, but I haven't found a way to only apply some migrations prior specific tests.

Is there a way to write automatic tests for Ecto migrations?


Solution

  • This is possible with a bit of manual work. All these tests should have a tag (e. g. @tag :migr) and should be run as a separate test via

    mix test --only migr
    

    Details.

    The standard tests should be run via

    mix test --exclude migr
    

    The latter might be set as a default config, for the former I would create mix alias.


    Then you should create your own task, similar to ecto.migrate. It would roll up all the migrations till the specified one (this might be passed as a parameter,) and perform tests you want.


    Sidenote: the necessity to test migrations is a very bad sign of the code design in general.