Modifying the database schema used to be one of the most complicated tasks a software developer had to do back in the day. Thankfully ActiveRecord migrations simplified this a lot. However, even with this great tool, you need to learn how to use it properly. If you do not, there are still some scenarios in which things could go sideways. Do you remember my post about adding indexes with no downtime? That is an example of a migration that could go wrong if you do not plan it accordingly. If you do not want this to happen to you, stay with me, because in this post you will learn how you can add a “supervisor” to your project that will tell you whether your migrations are safe to go.

Strong migrations

This supervisor is no other than the strong_migrations gem. Just let me tell you that if you are not already using it in your project you should add it right now. Strong migrations detects potentially dangerous migrations in your application and provides alternatives to make them safer. Oh! And it supports PostgreSQL, MySQL and MariaDB.

But what is a dangerous migration?

Strong migrations will consider a migration dangerous if:

Let’s see one example of each in action!

Migration that blocks reads or writes ⏳🕸

Believe it or not, but adding a column to a table with a default value used to block reads and writes to that table in some versions of PostgreSQL, MySQL and MariaDB04. blocks reads and writes

because it needed to be rewritten.

If you are adding a column with a default value like this:

Strong migrations will complain about it and you will have to rewrite it as it follows if you are using a version earlier than Postgres 11, MySQL 8.0.12 or MariaDB 10.3.2

Migration that is very likely to cause errors 💥

The other kind of migration that strong_migrations will flag as dangerous is where errors are very likely to happen. For example, when you remove a column. If you had this code:

How likely would it be that you forget to remove occurrences of Post#content from your application and alarms start going off once you already have removed the column from your database? 😱

To avoid that strong_migrations suggest you to do the following:

  1. Tell Active Record to ignore the column:
  1. Deploy your code (If errors happen now you would be able to revert easily since no data has been removed yet).
  2. Once you are sure everything works, write your migration wrapped in a safety_assured block.
  1. Deploy and run your migration.
  2. Remove the line added in step 1.

And that’s it! I recommend you of course to take a look at the strong_migrations gem. In their GitHub repository you will find more use cases and instructions on how to solve them.

I hope you found this post valuable! If you did, please consider subscribing so that you do not miss any future updates. If you want me to write about something in particular, please let me know in the comment section below.

See you in my next post! Adiós!