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:
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?
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
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.