Migrating A Rails App To Digital Ocean The Easy Way

Written by abdelp | Published 2021/05/29
Tech Story Tags: digital-ocean | app-platform | heroku | postgresql | rails | ruby-on-rails | migration | paas

TLDR Heroku is one of the top PaaS in the market thanks to many of its features that focus on development instead of configuration and maintenance. With the rise of the competitors like Digital Ocean with their own services, it is a good time to start considering migration your apps if you want to save some money. DigitalOcean App Platform only uses Postgres 12 as its database and deployments are made directly through push actions to the repositories of GitHub, GitLab, docker hub, or in one of its container registries.via the TL;DR App

Heroku is one of the top PaaS in the market thanks to many of its features that focus on development instead of configuration and maintenance. This has been an excellent option if you haven’t had the necessary budget to hire DevOps specialists, but with the rise of the competitors like Digital Ocean with their own services, it is a good time to start considering migrate your apps if you want to save some money.
In this tutorial, we are going to learn how to migrate a Rails app with its PostgreSQL Database from Heroku to an App Platform of Digital Ocean. Some caveats to having into consideration before starting are that DigitalOcean App Platform only uses PostgreSQL 12 as its database and that, unlike Heroku, its deployments are made directly through push actions to the repositories of GitHub, GitLab, docker hub, or in one of its container registries.
The prerequisites for this tutorial are:
  • Having installed the Heroku CLI.
  • Create a DigitalOcean account. Important note: as of today, DigitalOcean offers $100 of credit for 60 days with the creation of your account, you can also gain more credits by referring to other accounts or being referred, that way you can start with more than $100.
That being said, let’s start with the creation of the App Platform, after signing in, on the home page, click on the option Apps, as indicated in the image:
Click on the Create App button:
Now you need to choose where you are going to retrieve your application from:
For this tutorial, we are going to use a GitHub repo. After clicking on the GitHub option, you will be requested to select your specific repo:
Choose your repo, and the branch, there’s also the option to auto-deploy whenever the code changes in that branch.
After clicking next, it should be automatically recognized as a Rails project, and you can add some extra configurations:
For this specific example, you can just change the port to 3000, but sometimes you will need to put a custom Build Command to install your gem dependencies or any other configuration that you need to build your project.
After that, click on Add a Database:
You can leave the default options, and upgrade them according to your specific necessities later. After clicking Add Database and clicking next, you will be asked to name your web service and the region of your domain:
Before launching your App Platform, it’ll be displayed the plans available:
We can select the Basic plan and launch it:
the deployment of our platform will start, if you want to see it in more detail, you can tap on the Logs tab and see them.
If everything went well, a Deployed successfully message will be displayed:
Now we can start with the migration of our database, we can export the database in many ways and formats, for this case, we are going to use the basic way as shown in the Heroku documentation to export/import the database, first we capture a backup:
$ heroku pg:backups:capture
Starting backup of postgresql-rugged-66622... done

Use Ctrl-C at any time to stop monitoring progress; the backup will continue running.
Use heroku pg:backups:info to check progress.
Stop a running backup with heroku pg:backups:cancel.
Backing up DATABASE to b002... done
then download the backup:
$ heroku pg:backups:download
Getting backup from ⬢ whispering-retreat-92662... done, #2
Downloading latest.dump... ████████████████████████▏  100% 00:00 23.94KB
This is going to download the backup at a latest.dump file in your current directory, to import that file into our App Platform, we need to click on the settings tab, and copy the connection string of the database:
Make sure that the Connection Details is in Connection String, and copy the connection string below, you can also connect to your database through connection parameters though.
And finally, you can use your connection string or parameters to restore your database with the pg_restore command:
pg_restore --verbose --clean --no-acl --no-owner -d <YOUR_CONNECTION_STRING> latest.dump
And that’s all, check if there are no errors in the logs, sometimes some errors are due to some of the drops of nonexistent objects just to clean the database.
Hope you have found this article useful and discovered the wide range of options that these two tools offer to facilitate their migrations.

Written by abdelp | Software Engineer with over 10 years of professional experience, working mostly with web technologies
Published by HackerNoon on 2021/05/29