Repository navigation
fix(cloudflare): report changed rows, not rowsWritten, from Durable Object SQL writes - #3633
Conversation
…bject SQL writes rowsWritten counts index entries as well as table rows, so a one-row UPDATE on an indexed table reports 3. Kysely numAffectedRows and the collection-deletion fence (capture_state is indexed) compared that value against 1 and treated a successful write as stale. Use SQLite changes(). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
🦋 Changeset detectedLatest commit: 16f6cf1 The changes in this PR will be included in the next version bump. This PR includes changesets to release 12 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
This is the right fix: cursor.rowsWritten on the Durable Object SQLite surface counts index/trigger writes, so it cannot be used as an affected-row count. Switching EmDashDB.query() and executeCollectionDeletionGuard to SQLite changes() aligns DOQueryResult.changes with the Kysely numAffectedRows contract and fixes the false stale outcome when collection-deletion fencing updates an indexed media-usage status row. The implementation is small, localized, and consistent with the existing #-prefixed private-method style.
What I checked:
- The diff in
packages/cloudflare/src/db/do-sql-class.tsand the new regression tests. - The dialect mapping in
packages/cloudflare/src/db/do-sql-dialect.ts(numAffectedRows = BigInt(result.changes)), confirming the value flows into Kysely. - The
DOQueryResulttype docs and the siblingEmDashPreviewDBimplementation. - The changeset against
.changeset/README.md.
Headline: the code is correct and the regression cases are covered. I only have polish-level feedback: the public type doc for DOQueryResult.changes is now misleading, and the changeset could name the user-facing surface more concretely. I also noted that EmDashPreviewDB.query() still returns rowsWritten, but preview mode is read-only today so this is not a blocker.
Findings
-
[suggestion]
packages/cloudflare/src/db/do-sql-types.ts:66The
changesfield now reports SQLitechanges()(actual affected table rows), but its JSDoc still says "Rows written." Update the docstring so callers and dialect implementors know the semantics changed./** Rows changed by the last statement (SQLite `changes()`). `undefined` for read-only statements. */ -
[suggestion]
.changeset/do-sql-affected-rows.md:5The changeset is accurate, but it opens with the internal backend name. Leading with the public Kysely surface and the concrete user-visible symptom (
numAffectedRowsand collection-deletion fencing) makes it easier for upgraders to decide if the release affects them.Fixes Kysely `numAffectedRows` on the Durable Object SQL database backend: writes now report the number of changed table rows (SQLite `changes()`) instead of `rowsWritten`, which also counts index entries. This resolves false `stale` outcomes when fencing a collection for deletion with media-usage tracking enabled, and fixes any other `numAffectedRows` / `numUpdatedRows` comparisons against indexed tables.
|
All contributors have signed the CLA ✍️ ✅ |
|
I have read the CLA Document and I hereby sign the CLA |
…ent the field Apply review feedback: DOQueryResult.changes now documents SQLite changes() semantics, the changeset names the user-visible symptom, and EmDashPreviewDB.query() reports changes() instead of rowsWritten too.
@emdash-cms/admin
@emdash-cms/auth
@emdash-cms/auth-atproto
@emdash-cms/blocks
@emdash-cms/cloudflare
@emdash-cms/contentful-to-portable-text
emdash
create-emdash
@emdash-cms/gutenberg-to-portable-text
@emdash-cms/plugin-cli
@emdash-cms/plugin-test
@emdash-cms/plugin-types
@emdash-cms/registry-client
@emdash-cms/registry-lexicons
@emdash-cms/registry-loader
@emdash-cms/registry-moderation
@emdash-cms/registry-verification
@emdash-cms/sandbox-workerd
@emdash-cms/x402
@emdash-cms/plugin-ai-moderation
@emdash-cms/plugin-atproto
@emdash-cms/plugin-audit-log
@emdash-cms/plugin-color
@emdash-cms/plugin-embeds
@emdash-cms/plugin-field-kit
@emdash-cms/plugin-forms
@emdash-cms/plugin-webhook-notifier
commit: |
|
Thanks for contributing to EmDash! 🎉 Join the EmDash Discord, then run |
What does this PR do?
EmDashDB.query()(Durable Object SQL backend) returnedcursor.rowsWrittenas the write'schanges, which the dialects map to Kysely'snumAffectedRows.rowsWrittenalso counts index entries, so it is not an affected-row count. Measured in workerd (miniflare) on a table with two secondary indexes:rowsWrittenchanges()INSERTone rowUPDATEone row (both indexed columns)Any code that checks
numUpdatedRows === 1as a compare-and-set reads a successful single-row write as a lost race on this backend. The collection-deletion fence inexecuteCollectionDeletionGuardhas the same problem directly: it checksupdated.rowsWritten === 1on_emdash_media_usage_index_status, whosecapture_statecolumn is covered byidx__emdash_media_usage_status_reconciliation, so the fence returnsstaleafter it has succeeded.Both paths now read SQLite
changes()after the statement.rowsWritten > 0is still used only as the "did this write" signal for bookmarks.Following review,
EmDashPreviewDB.query()also reportschanges()instead ofrowsWritten, and theDOQueryResult.changesdoc comment describes the new semantics.No linked issue.
Type of change
Checklist
pnpm typecheckpassespnpm lintpassespnpm testpasses (or targeted tests for my change)pnpm formathas been runmessages.pochanges except in translation PRs — a workflow extracts catalogs on merge tomain.AI-generated code disclosure
Screenshots / test output
Not applicable (no UI). Two new tests in
packages/cloudflare/tests/db/do-sql-collection-deletion.test.tsfail onmain(expected 3 to be 1;expected { outcome: 'stale' } to deeply equal { outcome: 'fenced' }) and pass with this change.@emdash-cms/cloudflaresuite: 503 passed, 2 skipped.