Real-World Examples

This tutorial demonstrates real-world migration workflows for managing database schema changes in production environments.

What You'll Learn

  • Planning schema changes

  • Creating safe migrations

  • Testing migrations before deployment

  • Rolling back when needed

  • Handling data migrations

Prerequisites

  • Completed a getting started tutorial

  • Understanding of database migrations

Workflow 1: Adding a New Feature

Let's add a tagging system to a blog application.

Step 1: Plan the Schema Changes

Before writing code, plan what tables and columns you need:

Step 2: Create the Migrations

Create migrations in the correct order:

Step 3: Test Locally

Step 4: Create the Models

Workflow 2: Modifying Existing Tables

Adding a new column to an existing table requires careful planning.

Step 1: Plan for Zero Downtime

When adding columns:

  • Make new columns nullable OR provide defaults

  • Add columns first, then update code

  • Never remove columns that existing code depends on

Step 2: Create the Migration

Step 3: Deploy Strategy

  1. Deploy migration (adds column with default)

  2. Deploy new code that uses the column

  3. Backfill data if needed

Workflow 3: Data Migrations

Sometimes you need to transform existing data.

Step 1: Create Migration with Data Transformation

Step 2: For Large Tables, Use Batching

Workflow 4: Rolling Back

Safe Rollback

When Rollback Isn't Possible

Some migrations can't be rolled back:

  • Data loss migrations (dropping columns with data)

  • Data transformations

  • Renaming with data loss

Mark these clearly:

Workflow 5: Production Deployment

Pre-Deployment Checklist

  1. Test migration locally

  2. Test migration on staging

  3. Backup production database

  4. Plan rollback strategy

  5. Schedule maintenance window if needed

Deployment Script

Best Practices Summary

  1. Always write reversible migrations when possible

  2. Test migrations on a copy of production data

  3. Make additive changes first (add columns before code uses them)

  4. Use transactions for related changes

  5. Batch large data migrations

  6. Document irreversible migrations

  7. Have a rollback plan before deploying

Next Steps

Last updated

Was this helpful?