I’m looking to migrate all of my apps from Heroku, and I am having a lot of issues with Node+Postgres on Fly. The docs and support articles that exist don’t seem to cover any of the issues I’m having… and there’s a lot.
Let’s take a relatively simple app of mine that runs a NodeJS express server and connects to a Postgres database using TypeORM.
Run fly launch
Give my app a name
Choose a region
It generates a fly.toml with heroku/buildpacks:20 amongst other things
I choose not to deploy at this stage
I use fly secrets set
to set up a few environment variables
I run fly deploy
, and my app is built and then fails to connect to the database to run migrations (which are part of my build script). My build script runs a couple of package.json scripts with NPM, but during the migration step I get a connection refused error.
I try to fly ssh console
to debug the script, but I cannot connect to my app because “Error host unavailable: host was not found in DNS”. My app isn’t running?
I remove my migration script from my build and fly deploy
my app (which is now in a broken state).
I successfully fly ssh console
and try to run my migrations manually with npm run migrate
to see if I can connect to the database… “npm: not found”.
I try a which npm
with no results.
I try a which node
with no results.
At this point I have no idea how my app is even running or what the purpose of fly ssh console
is if I can’t access the core programs that are required to run my app.
After extensive Googling I find out about the deploy release_command
and so I put my migrations in here. I fly deploy
again and everything succeeds and appears to run my migrations.
Now I refer back to an article about migrating from Heroku to transfer the content of my database: Migrate from Heroku · Fly Docs
I run the pg_dump
command, and realize immediately that my console doesn’t have access to the database URLs that I’d just configured. I guess this is supposed to be run with fly
?
I fly ssh console
and run the command, which outputs a bunch of alter tables and such, followed by several ERROR: role "abc123etcetc" does not exist
.
Within the fly console I try psql
, but “error: connection to server on socket “/var/run/postgresql/.s.PGSQL.5432” failed: No such file or directory”.
I remember about reading another command to connect to Postgres so I run fly postgress connect -a postgres-app-name
(correctly replacing the app name).
From here I run \td
which results in “Did not find any relations.”. And a simple count(*)
of my user table errors with “ERROR: relation “user” does not exist”.
I figure I’ve messed up at some point and so I decide to detach the database and try again from scratch.
Run fly postgres detach -a app-name postgres-app-name
and get “Error error running user-delete: 500: ERROR: role “app_name” cannot be dropped because some objects depend on it (SQLSTATE 2BP01)”.
Now I have a completely broken app with a seemingly empty database, and no idea what to do next.
I delete my Postgres app from the Fly web UI, and attempt to detach it from the CLI again, and attach a new database, but everything I run just seems to result in errors because my app is no longer running, and any attempt to remove the database means the app fails to start, so it just rolls back to the previous broken variant.
- What is the correct approach for running migrations during a deployment?
- How do I manually run migrations (or other commands with access to Node/NPM)?
- How do I actually transfer my database content from Heroku to Fly?
- How do I run commands to setup my app before it is running?
- How do I now get a database connected to my horribly broken app?
- How to I reset the database if I need to try this all again?