Chapter 14: Data and failure

A table can do more than hold data. It can refuse it.

Schema

Also called Constraints

The declared shape of your data: which tables exist, which columns they have, what type each holds, and the rules each must obey, such as not empty, or never below zero. The database enforces those rules on every write, whoever is writing.

A rule in your code protects the one path that has it. An agent adding a new route, a script or a backfill won't know the rule is there. A constraint in the schema holds against all of them, including the agent itself. It is backpressure that lives in the data.

Say it to your agent

“Add these to the schema, not just the code: total_cents integer NOT NULL CHECK (total_cents >= 0), status text NOT NULL CHECK (status IN ('pending', 'paid', 'refunded')). Then write a test that inserts a bad row with raw SQL and expects an error.”

The workshop

Name a word, or describe what you mean in everyday language.