Strong migrations in Ruby on Rails
In this article, we looked at the Strong Migrations gem, and here we looked at gh-ost
.
Let us revisit the Problem Statement.
Problem Statement
When the database size is small, running migrations is not an issue as they run without delay.
However, as the database size grows, seemingly straight-forward tasks such as adding a column with a default value can take an enormous amount of time. This causes a problem as the affected table is locked for the duration of the migration. Hence, problems like these require us to come up with innovative ways to solve them.
The Aim
The aim is simply that we want to run the migrations in as non-blocking manner as possible.
Tools in Ruby on Rails
Let us take a look at some gems in Ruby on Rails that allow us to solve this problem.
Strong Migrations
We have already looked at this gem in this article. Let us check out the features that it provides.
It catches potentially dangerous operations.
Prevents them from running by default. However, we can explicitly mark the disputed operations as safe and allow them to run.
It also provides instructions on safer ways to do what we want.
It supports MariaDB, PostgreSQL and MySQL.
Online Migrations
This gem is a superset of the Strong Migrations gem, feature-wise. This means that it has all the features that are available in Strong Migrations. It provides a couple extra features aside from those. However, it is only compatible with PostgreSQL.
Some of this additional features include the following -
It has a set of code helpers. It recommends their usage when it detects a potentially dangerous operation.
It provides an internal framework for running data migrations and schema migrations in the background. These can be paused, retried, throttled or cancelled. We can even subscribe to
ActiveSupport::Notifications
to get the updates.Background data migrations - This feature allows adding data migrations that can be run in the background.
Background schema migrations - This feature allows adding schema migrations that can be run in the background.
It has additional checks for unsafe migrations.
It has higher configurability. For instance, we can -
Specify the migration after which the checks should be run.
Specify the target database version.
Specify whether to perform checks when migrating down.
Run
ANALYZE
on the table after the index was added.Specify the list of tables with permanently small number of records. It is generally considered safe to directly run the unsafe operations on such tables.
Specify the tables that are in the process of being renamed.
Specify a lock timeout beyond which the whole transaction is retried.
Shopify’s Large Hadron Migrator
It is a tool built to enable performing migrations online while the system is live, without locking the table. It can be directly used in an ActiveRecord::Migration
to perform different DDL operations. It also supports throttling.
It, currently, only works with MySQL. It can also be used in conjunction with ProxySQL.
PaHaMigrations
It is a Rails gem that allows us to run migrations safely. It provides safe alternatives, where available, for unsafe operations.
It only works with PostgreSQL.
This gem defines helper methods that can be used in migrations. We can use these methods to perform tasks like adding an index or adding a column with a default value. Each such task may have a safe or unsafe variant can be used to perform the migration as needed.
e.g. we can use safe_change_column_default
to safely change the default value of a column. It will use an internally implemented algorithm to perform the operation.
The algorithms for performing different DDL operations safely is provided in this blog.
Subscribe to my newsletter
Read articles from Shreyansh Gupta directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by