From AI Prototype to Production Schema: Data Modeling and Migration Strategy
A production data model is not a SQL version of the current JSON. Define the domain truth first, then migrate existing data toward it without losing compatibility.
Prototype schemas optimize for learning. Production schemas must protect ownership, relations, invariants and recovery. The migration between those worlds should be treated as an operational protocol rather than a one-off SQL script.
1. Model the domain, not the current tables
I identify entities, owners, required relationships, uniqueness rules and deletion semantics before redesigning tables. Existing schema is evidence, not the specification.
type DomainInvariant = { entity: string; owner: "user" | "workspace" | "system"; uniqueBy: string[]; requiredRelations: string[];};2. Move assumptions into database guarantees
Critical truths belong in NOT NULL, UNIQUE, FOREIGN KEY, CHECK, RLS or server-side policy. A UI that happens not to create invalid data is not a data-integrity strategy.
3. Use expand → migrate → contract
Add the new structure first, make the application compatible with both versions, backfill and validate, switch reads and writes, then remove the old structure after the compatibility window closes.
EXPAND -> add new schemaMIGRATE -> backfill and validateCUTOVER -> move reads/writesCONTRACT -> remove legacy fields4. Treat backfills as operations
Large backfills need batching, idempotency, retry rules and observability. They should be safe to resume and make progress measurable.
- Measure nulls and distributions before migration.
- Batch large updates.
- Make every batch idempotent.
- Delay strict constraints until backfill is complete.
- Compare old and new read paths before cutover.
- Plan application and schema rollback together.
5. Design indexes from real queries
Normalization does not guarantee performance. Indexes should follow measured filters, sort patterns and tenancy boundaries rather than intuition alone.
6. Rollback is a compatibility problem
Once a new application version writes data the old version cannot understand, deployment rollback may become unsafe. Some migrations require forward-fix rather than down migration; that choice should be explicit before release.
From Prototype to Production
Turning a working MVP into a dependable product: production checks, modular refactoring, data migration, audits, rewrite decisions and release gates.