Skip to content
Tech Atlas, home
Search
Esc
  • All roadmapsBrowse every learning path
  • ExploreFind a technology area
  • ProjectsGuided projects and builds
  • My AtlasYour learning dashboard
Lesson 1 of 6 · Concept · 15 min · revision 3

Why Schemas Need Version Control

See schema drift as the failure mode it is, learn what a migration tool actually records, and why most teams treat migration history as append-only and forward-only.

Contentsand lesson path

What you will learn

  • Generate, review and correct Alembic migrations from model changes, and apply or roll them back across environments.

Prerequisites: Data Access with SQLAlchemy

01. Why it matters

The database outlives every release of your code. A new version of the reading-list service can be deployed, rolled back and redeployed within minutes, but the books table you created in the first SQL lesson is the same physical table that will still be there next year, holding rows users care about. Every product change that needs a column, a constraint or a renamed table therefore has to land on that long-lived object, usually while the service is running.

Without a system, this goes wrong in a predictable way. A developer adds a column on their machine with ALTER TABLE to make a feature work. Another developer does the same, slightly differently. Staging gets a hand-run script at midnight; production gets whatever the team can reconstruct the next morning. Within a month the environments disagree, and nobody can say what the production schema even is. This is schema drift: the difference between the schema the code expects and the schema that actually exists.

A migration tool applies the same fix used for source code: version the changes, review them, and apply them in a defined order everywhere. In this skill that tool is Alembic, the migration companion to the SQLAlchemy models you already wrote. This lesson covers what such a tool actually stores, how it decides what to run, and the append-only discipline that keeps it trustworthy.

02. The ledger: revisions and the version table

A migration is a small script with two functions: upgrade() describes a change to apply, downgrade() describes how to undo it. Each script gets a unique revision id (Alembic generates a short hex string) and names its parent in down_revision, so the scripts form a single linked chain from the empty database to the current schema. The newest revision with no children is the head.

The second half of the system lives in the database itself: a tiny table, by default alembic_version, holding exactly one row per branch — the id of the last applied revision. This is the ledger. When you run alembic upgrade head, Alembic reads that row, walks the chain from the recorded revision to the target, and applies each missing upgrade() in order. Nothing else tracks state: no files on disk, no environment variables. If the table says abf2a0c59bd5, that is the whole truth about what has been applied.

From the lab database at the end of this skill, after its six migrations (revision ids are random, so yours will differ):

psql · select version_num from alembic_version
 version_num 
--------------
 abf2a0c59bd5
(1 row)
Terminal · alembic history (the same chain, parents on the left)
6229ecdb7537 -> abf2a0c59bd5 (head), contract drop title
02e95510843f -> 6229ecdb7537, tighten validate check
0e090eac744d -> 02e95510843f, tighten add not-null check
6818d43b0679 -> 0e090eac744d, migrate backfill
045714963dd9 -> 6818d43b0679, expand add display_title
<base> -> 045714963dd9, reading list baseline

This design is what makes many environments possible. Development, staging and production each keep their own alembic_version row. Staging can sit two revisions behind production; running alembic upgrade head there applies exactly the two missing scripts, in order, and nothing else. A fresh database starts with no row at all, so the whole chain runs from <base> — which is also how CI proves the history is complete: build an empty database, upgrade head, and the schema must match the models.

Because each script names its parent, two people can fork the chain by adding revisions on the same parent. Alembic detects this and reports multiple heads; the fix is a merge revision or re-pointing one script, covered with the rest of the review loop in the next lesson.

03. Append-only and forward-only

Every migration file has a downgrade(), and alembic downgrade genuinely walks the chain backwards. So why do most experienced teams treat production as forward-only — never downgrading, never editing an applied file?

Editing is the easier trap. Suppose a migration that staging has already applied is edited to add one more column. Staging never sees the change: its alembic_version row already names that revision, so Alembic will not run it again. Production, when it later upgrades, gets the edited version. The two databases now differ forever, silently. The rule follows: once a revision has left your machine, it is immutable. Mistakes are fixed by writing a new migration that corrects the previous one — the same discipline as never rewriting published git history.

Downgrades fail for a different reason: they are lossy. op.drop_column in upgrade() can be undone by re-adding a column, but the data that was in it is gone; a migration that renamed rows in place cannot un-rename the rows written since. A downgrade() that cannot restore the old state is not a rollback, it is a second data-destroying migration. Teams still write honest downgrades where they are cheap (drop the column that was added), and use them in development, but the production recovery plan is: restore a backup, then move forward with new migrations from there.

The site you are learning from works this way, and you can read it in the repo: plain SQL files, numbered, applied in order by a small runner that only knows how to move forward.

Terminal · ls packages/db/migrations (the Tech Atlas repo; excerpt, more files are added over time)
0000_initial_schema.sql
0001_assessments_and_evidence.sql
0002_analytics_events.sql
0003_align_event_names.sql
0004_projects_and_reviews.sql
...
meta

File 0003 is worth a look precisely because it is not schema at all: it rewrites existing rows to align event names with a new taxonomy. Schema migrations and data migrations live in the same ledger, applied in the same order — a pattern this skill returns to in the backfill lesson. Numbered SQL applied forward is the whole mechanism; Alembic adds generation, a version table and a command line on top of the same idea.

04. Check your understanding

Practice check · 3 questions

The ledger and its rules

Three questions about how Alembic tracks schema history.

Q1. Where does Alembic record that revision 045714963dd9 has been applied?
Q2. Your team adopts Alembic a month after the first release; production already has all the tables, and the first upgrade fails. What is the accepted fix?
Q3. A teammate edits a migration that staging has already applied, adding one more column to upgrade(). What is the danger?
Practice only: not graded, not saved, not evidence.

05. Apply it

Take any database you own, even a toy one:

  1. List the tables with \dt in psql, and try to write down, from memory, the order in which they were created and changed. Where you cannot, that gap is exactly what a migration history would have recorded.
  2. If the project uses Alembic, run alembic current and alembic history and check that the version table row and the file chain agree. If the project has no migrations yet, write the baseline by hand: one file that creates the current schema, then stamp the existing databases with it.
  3. Write down one rule for your team about editing applied migrations, and where a backup is taken before destructive changes.

06. Review prompt

Without looking back: what two things does a revision file link together, and what does the alembic_version table store? Explain to a teammate why an applied migration must never be edited, why production recovery is backup-plus-forward rather than downgrade, and what schema drift is. Name the one command that proves a migration history is complete on a fresh database.

References

Your progress

Reading state

Sign in to save that you finished reading this lesson.

Sign in to mark reading complete

Prove this skill

8 questions · about 6 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.
Take the skill check