When I'm developing Ecto applications I generally have two databases. Something like app_dev and app_test. It would be nice if there was a task that would prepare both of these databases for me. Right now, I'm doing something like:
defmodule App.Repo do
use Ecto.Repo, adapter: Ecto.Adapters.Postgres
def url do
"ecto://user@localhost/app_#{Mix.env}"
end
end
I run the following to prepare my databases.
mix ecto.migrate
MIX_ENV=test mix ecto.migrate
I don't have a specific solution but I think it is worth starting a discussion.
Thanks @drewolson! If you have any ideas or feedback on how it is solved elsewhere, it would be of great help. Maybe a running migration automatically before each test? Maybe just having a convenience to migrate both dev and test?
Personally, I think just a convenience method to migrate both dev and test would be a fine start. It's also not making any assumptions about workflow.
I actually think it's fine as is. You will always have to run mix ecto.migrate to migrate your dev database because this should be done explicitly, we don't want the database to change unexpectedly under our feet without knowing that it happened or why it happened.
About MIX_ENV=test mix ecto.migrate, I think this should be in test_helper.exs along with everything else to prepare the database for tests. We should probably put together a document about testing with ecto because I see new questions from time to time.
If it's any help, I have the following in my mix.exs, which sets up a fresh test database each time:
defp aliases do
[
test: ["ecto.drop", "ecto.create", "ecto.migrate", "test"]
]
end
In a long-running project, there may be hundreds of migrations, so it doesn't seem like relying on the migrations to recreate the current DB state is ideal in the long run. Rails gets around that with a schema file that holds the current state. This has its own associated issues, but it does solve some problems. I don't see an easy way to replicate that without re-thinking many aspects of the way things work.
Ecto has included ecto.dump and ecto.load for a while. So you can update your mix ecto.setup alias in your mix.exs or in your test_helper.exs to use those instead.
That works - if there's a standard place to keep it, and to use it in tests. It's the difference between there being a way to do it, vs. there being a well-known "normal" way to do it.