
Summary
Key points of this document
- Where are the design challenges?
- How to define boundaries?
- Details that matter in implementation
Overview
Using a type-safe ORM makes schema changes easier to code review. However, for applications with production data, passing type checks and safely migrating are separate issues. The real challenge of schema changes lies not in writing type-safe changes, but in designing the sequence of gradual migrations without corrupting live data.
Where are the design challenges?
Using Drizzle clarifies implementation by deriving types from table definitions. However, changing column names, changing types, adding NOT NULL constraints, and adding foreign key relationships cannot be simply applied in environments with existing data.
In production, old and new versions of the application may temporarily coexist. We consider the possibility of old code running immediately after migration, failure and rollback during the process, and heavy ALTER operations due to large data volumes.
PostgreSQL schemas outlast application implementations. Even seemingly small changes like adding columns, changing types, making columns NOT NULL, or adding indexes can impact existing data, lock times, and deployment order.
How to define boundaries?
Safe changes proceed in the order of adding a column, making the application compatible with both old and new, backfilling data, adding constraints, and finally deleting the old column. Drizzle's types consider not only the final state but also intermediate migration states, maintaining compatibility across releases.
In Drizzle, schema definitions and query types are closely related, making it easier to code review the intent of changes. However, migration is not just a type issue but also a time issue. We assume a period where old applications and new databases, and new applications and old data, coexist simultaneously.
Details that matter in implementation
In migration files, updates to existing data, index creation, and constraint additions are written separately. Large backfills are not included in synchronous migrations but are executed as separate jobs. On the application side, the read/write period for old and new columns is shortened, and completion conditions are verified via logs.
For example, when adding a required column, first add it as NULLable, start new writes in the application, perform a backfill, and finally add the NOT NULL constraint. While showing Drizzle schema changes and SQL migrations in the same PR, the execution order should be clearly specified in the deployment procedure.
- Consider CONCURRENTLY when adding indexes to large tables, and pay attention to transaction settings.
- Backfills should be divisible by primary key ranges or limits, not as a single bulk update.
- The application should be able to read NULLs or old values during the migration period before strengthening DB constraints.
Observe how it breaks
During validation, measure migration time with data volumes close to production. Classify changes beforehand as rollbackable or non-rollbackable, and for non-rollbackable changes, prepare backups and maintenance procedures.
During validation, apply migrations not only to empty databases but also to databases with data volumes close to production. Check execution time, locks, rollback feasibility, and compatibility with old applications to mitigate migration risks not visible through type checks alone.
Discarded options and trade-offs
Gradual migration increases the number of steps. For small applications, there's a temptation to change everything at once, but for changes that risk data loss or involve long-duration locks, the cost of splitting into stages is lower.
Gradual migration increases the number of PRs and deployments. While a single change might be faster, recovery from failure becomes more difficult. For tables where business interruption must be avoided, prioritizing compatibility periods over code cleanliness is a necessary judgment.
Decision criteria to leave for the team
Drizzle's type safety is a tool that improves the clarity of schema changes. However, safe migration requires operational design that includes release order, existing data, and rollback procedures.
The value of Drizzle lies in making schema changes visible through types. However, safe changes require a perspective that simultaneously considers types, data volume, locks, and deployment order.


