Connecting to PlanetScale with Phoenix (elixir)

Dec 04, 2021

I’ve been starting to learn Phoenix recently and wanted to see how hard it was to get PlanetScale connected.

This is mostly a post for myself on the next project.

As an aside, PlanetScale is pretty sweet, working at Shopify for a few years I’ve come to love MySQL for what it is. Working with branches and easy schema changes is really nice.

Configuring Dev

I’ve opted to do something similar in dev like PlanetScale blogged about.

Here’s my workflow with PlanetScale:

  1. Create a new branch & switch to it
# creating a new branch
pscale branch switch branch_name --database db_name --create
  1. Connect via pscale
# I've opted to use port `3305`, since normally it'd just be `3306` locally.
pscale connect db_name --port=3305

Running migrations is a matter of doing:

PSCALE=1 mix ecto.migrate

To get this all working, I’ve had to tweak the normal dev.exs DB config:

I’ve opted to just use a env for almost all configuration, to make it a bit easier to copy/paste on the next app.