We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Migration Review

Let's look at a more realistic migration that reflects a common evolution.

Example

The projects table is being renamed to initiatives to better reflect how teams plan and track long-term work.

We also want to record when each initiative officially launched.

Up migration:

ALTER TABLE projects RENAME TO initiatives;

ALTER TABLE initiatives
ADD COLUMN launched_at TIMESTAMP;

Down migration:

ALTER TABLE initiatives DROP COLUMN launched_at;

ALTER TABLE initiatives RENAME TO projects;

This pair of migrations is reversible and safe. If something breaks, we can undo it.

Real World Migration Tools

In real-world projects, we don't run raw SQL migrations. We use tools that help:

  • Track which migrations have been applied.
  • Organize migrations in files.
  • Apply and roll back safely.

Popular Tools

Tool Language Notes
Goose Go Native Go tool
Flyway Java, etc. Simple file-based
Liquibase Java More config-heavy
Alembic Python For SQLAlchemy
Prisma Migrate TypeScript Works with Prisma ORM
Drizzle Kit TypeScript Works with Drizzle ORM

Example Workflow With a Tool

This will vary according to the tool you use.

  1. Write migration files.
    • 001_add_columns_to_transactions.up.sql
    • 001_add_columns_to_transactions.down.sql
  2. Apply them using a CLI:
    migrate up
    
  3. Your tool logs which migrations ran, and prevents duplicate migrations.

Version Control for Your Schema

Migration files are committed like code. They travel with your project, so your teammates and CI systems always apply the same schema changes in the right order.