Skip to content
Learn Netverks

Lesson

Step 31/36 86% through track

migrations-basics

Migrations basics

Last reviewed May 28, 2026 Content v20260528
Track mode
sql_sandbox
Means
SQL sandbox
Reading
~2 min
Level
intermediate

This lesson

This lesson teaches Migrations basics: the SQL patterns, schema habits, and query reasoning you need before advancing in SQL.

Teams query Migrations basics on every SQL codebase—skipping it leaves gaps in debugging and data reviews.

You will apply Migrations basics in contexts like: Application releases, zero-downtime deploys, and schema reviews in Django, Laravel, or Rails.

Copy SQL from each lesson into SQLite (sqlite3), DB Fiddle, or local Postgres—read result grids and row counts. The in-browser SQL lab (sql_sandbox) will run queries when the runner ships; until then, local clients are the practice path.

Toward the end of the track—consolidate before dialect tracks, interview prep, and production checklist lessons.

Migrations are versioned schema change scripts applied in order across dev, staging, and production. Frameworks like Django migrations and Laravel/PHP tools generate SQL from model changes.

Why migrations

  • Reproducible schema across teammates and CI
  • Reviewable DDL in pull requests
  • Rollback or forward-fix strategies documented

Up and down

-- up.sql
ALTER TABLE products ADD COLUMN archived INTEGER DEFAULT 0;

-- down.sql (if supported)
-- ALTER TABLE products DROP COLUMN archived;

Not every change reverses cleanly—backup before destructive migrations in production.

Safe migration habits

  1. Add nullable columns before backfill
  2. Backfill data in batches
  3. Add NOT NULL and constraints after data is valid
  4. Create indexes concurrently on large Postgres tables (dialect-specific)

Important interview questions and answers

  1. Q: Migration vs raw SQL file?
    A: Migrations track applied versions in a metadata table; ad-hoc scripts lack ordering guarantees.
  2. Q: Zero-downtime deploy?
    A: Expand-contract pattern: add column, dual-write, backfill, switch reads, remove old.

Self-check

  1. Why not ALTER production by hand?
  2. What is a common order for adding a NOT NULL column to a populated table?

Tip: Add nullable columns first, backfill, then add NOT NULL in a follow-up migration.

Interview prep

Why migrations?

Ordered, versioned, reviewable schema changes across environments.

Add NOT NULL safely?

Add nullable, backfill, then enforce NOT NULL.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Backward compatible?
  • Lock during ALTER?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump