Unit 3 of 8 · about 4 hours · explain, implement

Schema versions, compatibility and migrations

Schemas change while old readers are still running. An additive field with a default keeps both directions working, while a rename without a transition breaks one side silently. Version policy plus a tested downgrade makes evolution boring.

You'll be able to

  • Derive the valid states of a versioned record across validation, quarantine, normalization and persistence.
  • Design a backward-compatible schema change with explicit mixed-version behavior and a tested rollback.
  • Classify DDL changes as compatible or breaking, and run a paired upgrade and downgrade on a scratch database.

Covers

Schema versions, compatibility and explicit migrationsBackward-compatible migration, mixed versions and rollback
1

Read

In this order, only the parts listed

0 of 4 done

  1. Pydantic Extra data as a version policyOfficial docs typed-service build path

    Extra data (ignore, forbid, allow): old readers drop, keep or reject fields added by newer writers, set through model_config

  2. Pydantic Default valuesOfficial docs typed-service build path

    Default values and Validate default values: a new field with a default lets old payloads validate against the new model

  3. Tutorial: migration scripts, running migrations, downgradingAlembic tutorial - migration scripts and downgrading

    Create a Migration Script (revision, down_revision, upgrade, downgrade); Running our First Migration and Running our Second Migration (upgrade head); Getting Information (history --verbose); Downgrading (downgrade base)

  4. Creating and Modifying TablesAlgoMaster SQL - selected route

    Constraints in DDL (primary and foreign keys, UNIQUE, CHECK, DEFAULT); ALTER TABLE (adding and dropping columns, changing types, adding constraints); DROP TABLE, TRUNCATE and DELETE

2

Focus on

  • Additive changes (a new optional field, a new table) keep old readers working. Renames, type narrows and new required fields break one direction until every reader upgrades.
  • Every migration ships upgrade and downgrade together, and downgrade is executed, not just written. An empty downgrade makes rollback untested.
  • Alembic orders by down_revision chains, not file names. history --verbose shows the chain and current shows where the database stands.
  • Old payloads must validate against new models with defaults filling new fields, and new payloads must not crash old readers; the extra policy decides whether unknowns drop, stay or raise.
Common mistakes · 3
  • extra forbid on a versioned reader. It rejects payloads from newer writers on unknown fields; versioned boundaries usually ignore or allow and stage the unknowns.
  • A new required field with existing rows. Old writers cannot supply it, and bulk backfill needs a default or a two-step migration.
  • Renaming a column while mixed versions read it. Old readers query the old name, so expand (add new, dual-write, migrate readers) before contracting (drop old).
3

Try it yourself

Closed-book, before checking anything

Closed-book, evolve a v1 record model to v2 with one optional field: validate a v1 payload against v2 and a v2 payload against v1, stating what fills and what drops. Then write one Alembic revision with upgrade and downgrade and run upgrade head, downgrade base, upgrade head on a scratch database.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.A v2 model adds nickname with a default. Does a v1 payload validate, and does a v1 model accept a v2 payload under default settings?Answer

    Yes to both: the default fills nickname, and default extra ignore drops it on the old reader. Verified on Pydantic 2.14.

  2. 2.What orders two Alembic revisions, and how do you inspect the chain?Answer

    down_revision links order them, not filenames. history --verbose shows parents and current shows the database position.

  3. 3.Name one DDL change that is safe under mixed versions and one that must wait.Answer

    Adding a nullable column is safe. Renaming or dropping a column in use must wait until all readers upgrade.

  4. 4.Why run downgrade base and then upgrade head when testing?Answer

    It executes the rollback path and proves re-upgrade reaches the same schema. An unwritten or unrun downgrade is an untested promise.

5

Practice

You're done when

Both validation directions pass with stated fill and drop behavior, three DDL changes are classified with reasons, and head to base to head succeeds on a scratch database.

Only when you can do this without notes.

Leads to

Notes

Resources · 3 for this unit, 8 more in the section
  • Official docs typed-service build pathPython typing, Pydantic, FastAPI and PostgreSQL · primary

    Build one record service in this order: structural protocols and generics; Pydantic models, validators, strict mode, serialization and settings; FastAPI request bodies, dependencies, errors and tests; then PostgreSQL transactions and EXPLAIN. Replay valid, malformed, duplicate and previous-schema requests through CLI and HTTP.

  • AlgoMaster SQL - selected routeAlgoMaster · required

    Study SQL Foundations, Querying Data, Joins, Subqueries and CTEs, Window Functions, DDL, DML and Views, Performance Optimization, Transactions and Concurrency, and Data Modeling. Stop when the service queries and transaction tests pass.

  • Alembic tutorial - migration scripts and downgradingSQLAlchemy · support

    Use Create a Migration Script, Running our First and Second Migration, Getting Information and Downgrading to run a paired upgrade and downgrade round trip on a scratch database.

Also in this section

  • PostgreSQL transactions and EXPLAINPostgreSQL · required

    Use the transaction tutorial and EXPLAIN guide to demonstrate commit, rollback, one isolation anomaly or prevented anomaly, and a measured plan improvement on the database actually used.

  • FastAPI tutorialFastAPI · support

    Use Request Body, Dependencies, Handling Errors and Testing to expose the same service layer through HTTP. Keep framework models outside the domain core and preserve the same error semantics as the CLI.

  • AlgoMaster Low-Level Design - one rehearsalAlgoMaster · support

    Use one fitting design prompt to rehearse interface contracts, dependency direction and failure ownership. Treat the review as feedback, then verify behavior with your own tests.

  • Stas' Python Cookbook - data and interfacesStas Bekman · support

    Use Serialization and Deserialization plus Environment and Arguments for focused implementation help. Compare JSON, JSONL, CSV and compressed data, and never use pickle for untrusted interchange.

  • AlgoMaster Python - interfaces, generics and data filesAlgoMaster · support

    Use Interfaces and Protocols plus Generic Types for structural typing, and Working with JSON plus Working with CSV for JSONL, error handling, newline and DictReader behavior. Lesson quizzes check recall; implementation is verified with your own scripts.

  • SQLAlchemy 2.1 documentation - select, Session and eager loadingSQLAlchemy · reference

    Use the select() tutorial for 2.x query style, Session basics for the transaction boundary and identity map, and relationship loading for lazy versus selectinload versus joinedload plus raiseload. Use select() style and Session.execute or Session.scalars only, never the legacy Query API.

  • PyArrow Parquet read and write docsApache Arrow · support

    Use Reading and Writing Single Files plus Table from_pydict and from_pylist to convert normalized records to Parquet and test types, nulls, column projection and schema.

  • Stripe idempotent requestsStripe · support

    Use the Idempotency-Key header contract, stored-response replay, parameter-mismatch errors and POST-only scope to implement keyed replay for one endpoint.