Moving a Hanami application from Heroku to Railway
Tags: ruby, rails, railway, postgres
Published: 2022-12-08 15:00:46

---
title: Moving a Hanami application from Heroku to Railway
published: true
description: A few tips on how to migrate your app from Heroku to Railway 
tags: ruby, rails, railway, postgreSQL
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tyl6e1okb74ybpod3j81.png
# Use a ratio of 100:42 for best results.
# published_at: 2022-12-08 13:21 +0000
---

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](https://dev.to/bajena/hanami-thoughts-of-a-rails-developer-ik1)) got deleted and I still wanted to continue learning Italian, so I started looking for an alternative to Heroku.

A colleague recommended [Railway.app](https://railway.app/) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uq6u1y0zc22mzbn225yb.png)

![Configuring a GitHub project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jt8xipiq55kydj8fpte2.png)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0m7f0crqm75dr4rbcpfw.png)
![PostgreSQL database in my Railway project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tyl6e1okb74ybpod3j81.png)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/birymo22z85kl1c1lj4x.png)
![Environment variables copied to Railway](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6r504khuny45k0iopigp.png)

## 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`:
![Hanami::Assets::MissingManifestFileError](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o1kqes4xhez3amn9nynt.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jxaur5egf3npiwno8hy8.png)

Next, I opened the Postgres addon in my Railway project and found the connection URL:
![Finding database connection string in Railway](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q1ksm3g3646z2k1xhygh.png)

Having that URL, I launched [pgAdmin](https://www.pgadmin.org/) on my computed, connected to the database, clicked "Restore" and picked the previously exported Heroku database file:
![Restoring database through pgAdmin](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ar6v7llj0n2wm9x4f67c.png)

## Summary
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!