The Locksmith's Duplicate Ward: On Understanding What Your Database Schema Hides
We spend a lot of time caring for our databases. We backup the bits, monitor the replication lag, and tune the queries. But there’s a quieter, more insidious form of rot that can set in, one that your backup system will faithfully preserve and your logs will never scream about: the silent assumption. It’s a flaw not in the code, but in the collective understanding of the very shape of your data. It lives in the gap between what the schema allows and what the application truly requires. And the best way to find it is to try to break it on purpose.
I call this technique "schema fuzzing," though it requires no fancy tools. It's a manual, almost meditative process of questioning every constraint your schema doesn't enforce. Start with your core tables. You have a `users` table with an `email` column, marked as unique. Good. But is it nullable? If it is, your application might assume, in a hundred different places, that a user always has an email. What happens when one doesn't? The schema permits it. The code might not.
The next step is to look for the absence of a unique constraint on columns that should have one. This is the "duplicate ward" of the title. Consider a simple `api_keys` table. It has a `key` column (a hash) and a `user_id`. The application logic likely assumes that one active key exists per user. But does your schema have a unique constraint on `user_id`? Or can your application, through a bug or a race condition, create two active keys for the same user? Which key is used then? The behavior becomes unpredictable. The lack of that constraint is a hidden door, a vulnerability not to attackers first, but to chaos.
This exercise is about strengthening the contract between your application and its storage. For every table, ask: What are the unspoken rules? That a ‘status’ column can only be ‘active’ or ‘inactive’? Enumerate it. That a ‘parent_id’ must always point to a valid record? That’s a foreign key, add it. That a ‘deleted_at’ timestamp being null means the record is active? Consider a partial unique index to ensure only one active record can exist where needed.
This isn't about building an impenetrable fortress of constraints that makes future changes impossible. It's about making the implicit, explicit. It’s the difference between a door with a flimsy latch that looks closed and one with a deadbolt that audibly clicks shut. By probing for these weaknesses yourself, you aren't just preventing future data corruption; you are deepening your understanding of the system you steward. You stop being a mere caretaker of the data and become its locksmith, carefully crafting the wards and tumblers that ensure it remains coherent, reliable, and truthful. The next time you review a migration, before you run it, ask not just what it does, but what it fails to forbid.
Notes & further reading
A few pages I came back to while writing this:
- Surprise, AZ
- The Ceramicist's Perfect Cracks: On the System That Learns by Breaking
- Elk Grove, CA
- The Miller's Constant Sigh: On the Millstone That Grinds Empty
- Pasadena, CA
- The Porter's Dented Tin Cup: On the Bucket That Carried the Drip
- New Haven, CT
- Stamford, CT
- Washington, DC
- one area's overview
- a practical rundown
- Little Rock, AR
- Gilbert, AZ