Repository navigation
Commit 88f22e0
fix(driver-sql): upsert with nothing left to merge is a plain INSERT, not merge-ALL (#26015)
<!-- claude-projects-attribution: {"github_login":"os-project-manager"}
-->
_Requested by **Leehom** · [project
thread](https://claude.ai/code/project/chan_01TenzsRopZj4jMbvjX8uz1X?thread=cmsg_01TenzsRopZj4jMbvjX8uz1XENoZnG15Ne6zvnM6q3Fq5d)_
Fixes #8740
Clause-②: no (narrowing)
Metadata-protocol: none
## What changed
`SqlDriver.upsert` merges every payload column that is not insert-only,
or, when there is none, the conflict-target columns the payload carries
(a no-op). When the payload carried neither, the statement fell back to
knex's bare `merge()`, which compiles to merge-ALL on every dialect (`do
update set` / `on duplicate key update` over every inserted column,
insert-only ones included).
That fallback is removed. With nothing to merge, the statement is a
plain `INSERT` with no conflict clause, so a collision surfaces as the
driver's own unique-violation error and the stored row is left as it
was. It is not `insert ignore` / `do nothing`, which would hide the
collision. The reachable no-op merge on the conflict-target columns is
unchanged.
- `packages/drivers/driver-sql/src/upsert-conflict-clause.ts` (new, not
exported from the package): `withUpsertConflictClause(insertion,
mergeKeys, columnsToMerge)` returns the plain insertion when
`columnsToMerge` is empty, else
`onConflict(mergeKeys).merge(columnsToMerge)`. Kept free of the driver
so its SQL is pinned per dialect with no server.
- `sql-driver.ts`: the upsert statement goes through it; the
in-statement merge predicate is applied only when the statement merges.
- `.changeset/8740-upsert-nothing-to-merge.md`: `patch`, BREAKING banner
(a served-set narrowing), ADR-0087 `not-required
(no-migration-prescription)`.
## Tests
New `src/sql-driver-upsert-empty-merge-set.test.ts`:
- compiled SQL with nothing to merge, via `toSQL()` with no server,
exact strings for `better-sqlite3`, `pg` and `mysql2`: no `on conflict`,
`do update set`, `on duplicate key update` or `insert ignore`; plus the
contrast (something to merge keeps the clause and merges only the named
column);
- per dialect cell (SQLite always; PostgreSQL and MySQL where
`OS_TEST_POSTGRES_URL` / `OS_TEST_MYSQL_URL` are provisioned): a
collision on a conflict-target column the payload omits (non-null
default) is refused with a unique violation and rewrites nothing; a
primary-key collision whose payload carries only insert-only columns is
refused and rewrites nothing; controls: a payload that collides with
nothing still inserts and answers the row, and a payload carrying its
conflict target still upserts onto the stored row. The refusal is
asserted by `isUniqueViolationError` AND the dialect's native code
(`SQLITE_CONSTRAINT_*`, `23505`, `ER_DUP_ENTRY`).
Red first, on the unfixed driver (commit 19a401b): 4 failed / 12
passed, the non-null-default collision red on all three cells, the
primary-key collision red on the MySQL-dialect cell. Green after the fix
(commit fc2ee27), with the four neighbouring upsert suites: 5 files,
119 tests passed, all three cells live.
Local cells: SQLite in-process; PostgreSQL 16.15 local; the MySQL cell
against MariaDB 10.11.14 (no MySQL 8 server in the container). CI's
`Temporal Conformance (live PG + MySQL)` job runs this package's whole
suite, so the new file's MySQL cell runs there on MySQL 8.
## Gates
- `pnpm --filter @objectstack/driver-sql test`: 236 files passed, 12
skipped; 3795 tests passed (SQLite cells; live cells via CI).
`typecheck`: exit 0.
- With local live URLs the package run shows 49 failures in 24 files,
all live PG/MariaDB cells and none in an upsert file; the same 24 files
on the base commit `05f7758f05` show the same 49 failures (local MariaDB
/ PG environment, not this diff).
- `eslint --no-inline-config` on the three touched source files: exit 0.
- `dispatch-gates --commands`: 71 derived, 70 run, every exit 0; 1 NOT
MEASURED: `check:dual-build-cjs-loads` (exit 3, needs the whole tree
built; CI runs it). `--ran` reconciliation: 71 accounted, 0 unrun.
## Acceptance notes
- An unbacked conflict target is still refused VALIDATION_ERROR / 400 on
the nothing-to-merge path on every dialect, checked against the table
keys before the plain INSERT (pinned per dialect cell).
- **`isUniqueViolationError` reads a non-unique error as a unique
violation when the error message echoes an `on duplicate key update`
statement** (driver-level repro: a MySQL `ER_TRUNCATED_WRONG_VALUE`
whose message carries the statement answers `true`; the same code
without the statement text answers `false`). Not fixed here; the new
tests pin the native code alongside it for that reason. No public door
measured.
- `driver-turso`'s remote transport is a separate statement builder (`DO
NOTHING` on an empty merge set, SQLite dialect only) and is untouched;
its local face inherits this fix through `super.upsert`.
- Full dogfood battery (owed by the `(narrowing)` arm) is run on the
final head and reported on #8740.
---
_Generated by [Claude
Code](https://claude.ai/code/session_01T6USnvRkjp4BPytcNttAuF)_
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent aa2f8e3 commit 88f22e0
4 files changed
Lines changed: 389 additions & 26 deletions
File tree
- .changeset
- packages/drivers/driver-sql/src
| 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 | + | |
| 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 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
0 commit comments