Constraints, migrations and schema evolution
Build a Node HTTP API from scratch, model data in SQL and MongoDB, and reason about transactions, indexes, caching and authentication so the endpoint you ship stays correct under load.
What you will be able to do
#- Enforce data rules with NOT NULL, UNIQUE, CHECK and foreign keys.
- Apply the expand and contract pattern for backward-compatible changes.
- Keep migrations ordered, reversible and reviewed.
Overview
#A migration is a versioned, ordered change to the schema. Treat migrations as code: review them, keep them deterministic, and never edit one that has already run in an environment.
Constraints as guardrails
ALTER TABLE articles
ADD CONSTRAINT title_len CHECK (char_length(title) >= 3),
ALTER COLUMN status SET NOT NULL;
Constraints catch bugs at the last possible moment and document intent. A unique constraint on (author_id, slug) prevents duplicate slugs per author.
Expand and contract
To rename a column safely without breaking running code:
- Expand: add the new column and backfill it.
- Dual write: write to both old and new columns.
- Switch reads to the new column.
- Contract: drop the old column in a later release.
This keeps old and new application versions working during a rolling deploy.
Common pitfalls
- Adding a NOT NULL column with no default to a large table, which locks or fails on existing rows.
- Editing a migration that already ran, so environments diverge.
- Dropping a column in the same release that stops using it.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Implement
Write the expand step to add a new non-null column author_slug to articles without breaking existing rows.
starter.sql-- articles has existing rowsReveal the reference answer
reference.sqlALTER TABLE articles ADD COLUMN author_slug TEXT; UPDATE articles SET author_slug = 'unknown' WHERE author_slug IS NULL; ALTER TABLE articles ALTER COLUMN author_slug SET NOT NULL;Authored expected output: Column added nullable, backfilled, then made NOT NULL.
Add nullable, backfill in a separate step, then enforce NOT NULL. This avoids failing on existing rows and keeps the change reversible.
Exercise 2Explain
Why is dropping a column in the same release that stops reading it risky?
starter.textRelease N: code stops reading legacy_col Release N: migration drops legacy_colReveal the reference answer
reference.textDuring a rolling deploy, old application instances still running release N-1 may still read or write the column. Dropping it in the same release breaks those instances. Contract in a later release once no running code references the column.Authored expected output: Explains rolling-deploy compatibility and defers the drop to a later release.
Expand and contract keeps every concurrently running version compatible with the schema.
Common misconceptions
#- Migrations are just for creating tables: they are the only sanctioned way to change schema in any environment.
- A rename is one migration: a hard rename breaks the previous application version during a rolling deploy.
Follow-up practice
#- backend-databases-n-plus-one
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.