How to migrate a Ruby on Rails application

In this guide, you will learn how to migrate a Ruby on Rails app to Coherence. We assume your application is containerized and ready for the cloud – meaning you have a Dockerfile and your application is configured to listen to environment variables in places like config/database.yml and elsewhere. If you need help with containerizing your application here are some links that will help:

coherence.yml

Once your app is cloud-ready, you will need a coherence.yml. This is a special configuration file that Coherence uses to create the infrastructure and services your application needs.

In the root of your repo, create the file coherence.yml. The first thing we need to add to this file is the service type.

backend:
 type: backend

Next, we will add the script we want to run to migrate our database.

backend:
 type: backend
 migration: ["./bin/setup"]

Notice that in this example we are calling the setup script in the /bin directory. You could also call rails db:migrate like so:

backend:
 type: backend
 migration: ["rails", "db:migrate"]

Next, we will add the commands to run for both development and production environments. In this example we are using rails commands, but you could also call custom scripts like we did for migration.

backend:
 type: backend
 migration: ["rails", "db:migrate"]
 dev: ["rails", "s", "-p", "$PORT"]
 prod: ["rails", "s", "-e",  "production", "-p", "$PORT"]

Finally, we will need to specify the resources our application needs like the database and cache. For this guide, we will use PostgreSQL for our database and Redis for the cache.

backend:
 type: backend
 migration: ["rails", "db:migrate"]
 dev: ["rails", "s", "-p", "$PORT"]
 prod: ["rails", "s", "-e",  "production", "-p", "$PORT"]

 resources:
 - name: db1
   engine: postgres
   version: 13
   type: database
 - name: redis
   engine: redis
   version: 7
   type: cache

That’s it!

Coherence will take this configuration file and spin up all of the infrastructure our application needs including our database and cache automatically for us.

Environment variables

There are some important env variables you need to be aware of to use throughout various config files in your rails app.

  • PORT: This is the port the rails server needs to run on. On GCP it defaults to 8080.

It is important to also note that Coherence has some ports reserved which it uses internally.

  • DB_NAME: The name of your database.
  • DB_USER: The username of the database user.
  • DB_PASSWORD: The password for the DB_USER.
  • DB1_SOCKET: This is socket where your database is located. The name of this variable will differ depending upon the name you gave to your database in the coherence.yml.
  • DB1_PORT: The port of your database. The name of this variable will differ depending upon the name you gave to your database in the coherence.yml.
  • REDIS_URL: The URL of your Redis instance.
  • REDIS_PORT: The port of your Redis instance.

database.yml

Here is an example of config/database.yml which uses some of the important environment variables noted above.

default: &default
 adapter: postgresql
 encoding: utf8
 sslmode: "disable"
 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
 username: <%= ENV.fetch("DB_USER") %>
 password: <%= ENV.fetch("DB_PASSWORD") %>
 host: <%= ENV.fetch("DB1_SOCKET") %>
 port: <%= ENV.fetch("DB1_PORT") %>

development:
 <<: *default
 database: <%= ENV.fetch("DB_NAME") %>

test:
 <<: *default
 database: <%= ENV.fetch("DB_NAME") %>_test

production:
 <<: *default
 database: <%= ENV.fetch("DB_NAME") %>