DEV Community

Cover image for Moving a Hanami application from Heroku to Railway
Jan Bajena
Jan Bajena

Posted on

Moving a Hanami application from Heroku to Railway

Recently Heroku dropped their free tier which was a go-to platform for many pet projects. My simple Hanami flashcard application (I wrote about it in my previous article) got deleted and I still wanted to continue learning Italian, so I started looking for an alternative to Heroku.

A colleague recommended to me, so I decided to give it a shot.
Eventually I managed to migrate the application, but there were a few problems that I experienced, so I thought to share with you how I managed to solve them.

1. Creating and deploying the application

This part was super straightforward in my case. The only thing I needed to do is to pick "Deploy from GitHub repo" and that was that! Railway picked up the code and started a deploy automatically.
New project type selection

Configuring a GitHub project

2. Adding a Postgres database to the project

The first deploy immediately failed. When I checked the deploy logs I've noticed that the application failed to connect to the Postgres database and well... that's understandable, because I didn't add the database to my project.

What I had to do was to click "+New" in my project and select a Postgres database.

Adding a Postgres to my project automatically injected necessary environment variable: DATABASE_URL.

Application fails to connect to the database
PostgreSQL database in my Railway project

3. Migrating environment variables

The next problem I faced was that the application wouldn't start due to missing environment variables.

So, what I did was that I went to my Heroku project settings and copy pasted the variables:

Environment variables in Heroku
Environment variables copied to Railway

4. Solving assets precompilation

Unfortunately at this point my app still didn't want to start.
Once I checked my project's Sentry I saw Hanami::Assets::MissingManifestFileError:

It turned out that Railway's deployment would only spin up the server without precompiling the assets which was different from Heroku and gave me a lot of headache before I managed to fix that.

What worked for me in the end was to:

  • Set SERVE_STATIC_ASSETS enviromnent variable as true
  • Add a file called Procfile to the root directory of GitHub repository. The file has the following content web: bundle exec hanami assets precompile && bundle exec hanami server -p $PORT

Note the -p $PORT param. $PORT variable is provided by default by Railway and it's super important to set it when starting the server, because otherwise Railway won't know where your application lives.

5. Exporting and restoring PostgreSQL backup

My app's database contained over a thousand of language flashcards and I didn't want to loose them.
So, what I've done is I went to Heroku's PostgreSQL "Durability" tab and downloaded an export file of my database:
Exporting database from Heroku settings

Next, I opened the Postgres addon in my Railway project and found the connection URL:
Finding database connection string in Railway

Having that URL, I launched pgAdmin on my computed, connected to the database, clicked "Restore" and picked the previously exported Heroku database file:
Restoring database through pgAdmin


The migration wasn't as seamless as expected, but eventually everthing seems to be working fine. Note that my application is a Hanami 1.3. application, so things may have changed in recently released 2.0. version.

Happy hacking!

Top comments (0)