Commit d773f53
authored
fix(planner): emit executable DDL for NOT NULL columns on non-empty tables (#241)
closes
[TML-2067](https://linear.app/prisma-company/issue/TML-2067/planner-emits-unexecutable-ddl-for-not-null-columns-on-non-empty)
## Intent
Fix Postgres migration planning and execution for `ADD COLUMN ... NOT
NULL` on existing tables during `db update`.
Before this branch, two things were wrong:
- the planner could only safely add a NOT NULL column without an
explicit default by requiring the table to be empty
- the runner could skip `db update` work when the marker already matched
the destination, even if the live schema had drifted
This PR fixes that bug, extracts the first reusable multi-step planner
recipe, and adds an extension-aware seam for temporary-default
resolution. It does **not** try to ship a public TypeScript migration
API yet.
## What Changed
### Planner
- `buildAddColumnOperation(...)` now decides **when** the
temporary-default strategy applies.
- The actual 2-step recipe is extracted into
`buildAddNotNullColumnWithTemporaryDefaultOperation(...)`.
- Temporary-default lookup now flows through
`resolveTemporaryDefaultLiteral(...)`:
1. ask a codec hook
2. fall back to built-in Postgres defaults
3. if neither can provide a safe value, keep the empty-table precheck
- `CodecControlHooks` now exposes `resolveTemporaryDefaultLiteral(...)`
so extensions and parameterized codecs can participate in this decision.
- `pgvector` implements that hook and returns a dimension-aware zero
vector literal.
- The built-in Postgres fallback now covers more safe cases, including
arrays (`'{}'`), `tsvector` (`''::tsvector`), `bytea` (`''::bytea`),
alias forms like `varchar` / `bpchar` / `varbit`, and length-aware zero
literals for `bit(n)`.
### Runner
- `db update` plans (`origin: null`) now always apply operations.
- `migration apply` plans (`origin` set) still skip when the marker
already matches the destination.
That preserves migration replay idempotency while allowing `db update`
to recover from live drift.
### Tests and Evidence
- `planner.behavior.test.ts` now verifies:
- the extracted 2-step recipe for built-in types
- extension-aware temporary defaults via `pgvector`
- fallback-to-empty-table behavior when a codec explicitly declines a
temporary default
- expanded built-in fallback coverage for arrays, `tsvector`, `bytea`,
and `bit(n)`
- `drift-schema.e2e.test.ts` now exercises the built-in recovery path:
- initialize the schema
- insert a row
- manually drop a NOT NULL column
- run `db update`
- verify the row was backfilled and that the temporary default was
removed
- `psl.pgvector-dbinit.test.ts` now exercises the extension path against
a live database:
- initialize a schema with a `vector(3)` NOT NULL column
- insert a row
- manually drop the vector column
- run `db update`
- verify the row was backfilled to `[0,0,0]` and that no default remains
on the column
- `runner.idempotency.integration.test.ts` now explicitly models
`migration apply` semantics by setting `origin`, so it still verifies
skip-on-marker-match in the right path.
### Supporting Changes
- Migration subsystem docs now describe the temporary-default strategy
as:
- codec hook first
- built-in fallback second
- empty-table precheck only when no safe default is known
## Behavior Change
For a NOT NULL column without an explicit contract default:
- if the planner can resolve a safe temporary default, it emits:
1. `ADD COLUMN ... DEFAULT <temporary> NOT NULL`
2. `ALTER COLUMN ... DROP DEFAULT`
- existing rows keep the backfilled value
- future inserts must still provide an explicit value
- if no safe temporary default is known, the operation still requires an
empty table
## Scope and Deferrals
This PR intentionally stops at the internal planner boundary.
Deferred:
- public composable migration utilities for user-authored TypeScript
migrations
- a strategy registry or broader cross-target abstraction layer
- broader work on user-authored TS contracts / TS migrations
Those are follow-up design and implementation steps. This PR is the bug
fix plus the smallest useful extensibility seam.
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **New Features**
* `db update` live reconciliation for recovering schema drift on
non-empty tables.
* Planner now supports a safe temporary-default strategy to add NOT NULL
columns without requiring empty tables.
* Extensions (e.g., vector) can provide temporary-defaults for
reconciliation.
* **Bug Fixes**
* Improved idempotency: migration-apply plans are skipped when already
at destination.
* **Documentation**
* Added architecture docs for the `db update` reconciliation flow and
planner behavior.
* **Tests**
* Expanded tests for NOT NULL migrations, pgvector recovery,
idempotency, and CLI error reporting.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
---------
Co-authored-by: jkomyno <12381818+jkomyno@users.noreply.github.com>1 parent ee1235b commit d773f53
18 files changed
Lines changed: 1436 additions & 466 deletions
File tree
- docs/architecture docs/subsystems
- packages
- 2-sql/3-tooling/family/src
- core/migrations
- exports
- 3-extensions/pgvector/src/exports
- 3-targets/3-targets/postgres
- src
- core/migrations
- exports
- test/migrations
- test/integration/test
- authoring
- cli-journeys
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
298 | 298 | | |
299 | 299 | | |
300 | 300 | | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
301 | 311 | | |
302 | 312 | | |
303 | 313 | | |
| |||
333 | 343 | | |
334 | 344 | | |
335 | 345 | | |
336 | | - | |
337 | | - | |
| |||
Lines changed: 23 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
164 | 164 | | |
165 | 165 | | |
166 | 166 | | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
167 | 180 | | |
168 | 181 | | |
169 | 182 | | |
| |||
194 | 207 | | |
195 | 208 | | |
196 | 209 | | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
197 | 220 | | |
198 | 221 | | |
199 | 222 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
| 50 | + | |
50 | 51 | | |
51 | 52 | | |
52 | 53 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
10 | 20 | | |
11 | 21 | | |
12 | 22 | | |
| |||
15 | 25 | | |
16 | 26 | | |
17 | 27 | | |
| 28 | + | |
18 | 29 | | |
19 | 30 | | |
20 | 31 | | |
| |||
Lines changed: 129 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
Lines changed: 83 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
Lines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
15 | 14 | | |
16 | 15 | | |
17 | 16 | | |
18 | 17 | | |
19 | 18 | | |
20 | | - | |
| 19 | + | |
| 20 | + | |
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| |||
0 commit comments