When Migrations Go Bad
Explore common issues in Rails migrations when DDL operations fail and cause inconsistent database states. Learn strategies for troubleshooting migration failures, including safely dropping and recreating databases, and applying updates while minimizing risk in production environments. Understand how to back up your database and perform migrations carefully to maintain schema integrity.
We'll cover the following...
Overview
Migrations suffer from one serious problem. The underlying DDL statements that update the database schema are not transactional. This isn’t exclusively a failing in Rails, though, as most databases don’t support the rolling back of create table, alter table, and other DDL statements.
Let’s look at a migration that tries to add two tables to a database:
In the normal course of events, the up() method adds tables one and two, and the down() method removes them.
What happens if ...