Recommendations from Shopify's Data Migrations team for adding NOT NULL columns safely to your own database tables, based on a recent investigation into schema change safety using using the popular Large Hadron Migrator (LHM) gem.
Overview
This article discusses the safe addition of NOT NULL columns to database tables, focusing on the use of the Large Hadron Migrator (LHM) gem for schema changes in MySQL databases. It outlines the safety definitions, potential pitfalls, and recommendations for implementing such changes without data loss.
What You'll Learn
How to safely add NOT NULL columns to MySQL tables using LHM
Why defining a DEFAULT value is crucial when adding NOT NULL columns
When to use UNIQUE INDEX in schema changes and its implications
Prerequisites & Requirements
- Understanding of MySQL schema changes and database migrations
- Familiarity with the Large Hadron Migrator (LHM) gem(optional)
Key Questions Answered
What is the procedure LHM uses for schema changes?
What factors determine the safety of adding a NOT NULL column?
How does MySQL mode affect the addition of NOT NULL columns?
What are the consequences of adding a UNIQUE INDEX during schema changes?
Technologies & Tools
Some links below are affiliate links. We may earn a commission if you make a purchase.
Key Actionable Insights
1Always define a DEFAULT value when adding NOT NULL columns to avoid unexpected behaviors during migrations.This practice ensures that existing records in the original table can be populated correctly in the shadow table, preventing potential application compatibility issues.
2Check for duplicate values before adding UNIQUE INDEX to avoid data loss.This precaution helps maintain data integrity and prevents migration failures that could arise from existing duplicates in the database.
3Utilize the shadow-table mechanism for schema changes to minimize downtime.This approach allows for safe migrations without impacting the availability of the original table, ensuring that ongoing operations can continue seamlessly.