How to Run Ecto Migrations on Production
Not as obvious as you might think
This post was originally published on the author’s personal blog.
You’d think the answer to this question would be a simple Google search away. Unfortunately, that wasn’t the case for me this afternoon, working on a Phoenix project with a newly-added Ecto backend. In an effort to save others (and let’s be honest, future me) the same frustration, here’s the most straight-forward solutions I found.
What Doesn’t Work
So don’t try using your trusty local
mix ecto.migrate task on production. Not gonna help you here.
What Does Work
Ecto ships with Ecto.Migrator, a first-class module for Ecto’s migration API.
Run it manually by ssh’ing onto your app server, attaching to your Phoenix app, and running the following:
path = Application.app_dir(:my_app, “priv/repo/migrations”)
Ecto.Migrator.run(MyApp.Repo, path, :up, all: true)
Ideally, you’d wrap up the above in its own task that can be called during your build and deployment process. Check out Plataformatec’s blog for a nice example.
Our app uses edeliver for deployments, and it has a super handy command for manually running migrations:
mix edeliver migrate production
To run successfully, you’ll need to add
ECTO_REPOSITORY=”MyApp.Repo” to your
Again, Plataformatec has a nice blog post on deploying your Elixir app with eDeliver.
Hi future me! Hope this post was still helpful the nth time around.
Wanna work on a mission-driven team that loves perfecting Elixir deploy tooling? Get in touch — we’re hiring!