Skip to content

fix(cloudflare): report changed rows, not rowsWritten, from Durable Object SQL writes - #3633

Merged
ascorbic merged 2 commits into
emdash-cms:mainfrom
koikar:fix/do-sql-affected-rows
Sep 30, 2026
Merged

ascorbic merged 2 commits into
emdash-cms:mainfrom
koikar:fix/do-sql-affected-rows

Conversation

@koikar

@koikar koikar commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

EmDashDB.query() (Durable Object SQL backend) returned cursor.rowsWritten as the write's changes, which the dialects map to Kysely's numAffectedRows. rowsWritten also counts index entries, so it is not an affected-row count. Measured in workerd (miniflare) on a table with two secondary indexes:

Statement rowsWritten changes()
INSERT one row 4 1
UPDATE one row (both indexed columns) 3 1

Any code that checks numUpdatedRows === 1 as a compare-and-set reads a successful single-row write as a lost race on this backend. The collection-deletion fence in executeCollectionDeletionGuard has the same problem directly: it checks updated.rowsWritten === 1 on _emdash_media_usage_index_status, whose capture_state column is covered by idx__emdash_media_usage_status_reconciliation, so the fence returns stale after it has succeeded.

Both paths now read SQLite changes() after the statement. rowsWritten > 0 is still used only as the "did this write" signal for bookmarks.

Following review, EmDashPreviewDB.query() also reports changes() instead of rowsWritten, and the DOQueryResult.changes doc comment describes the new semantics.

No linked issue.

Type of change

  • Bug fix
  • Feature (requires maintainer-approved Discussion)
  • Refactor (no behavior change)
  • Translation
  • Documentation
  • Performance improvement
  • Tests
  • Chore (dependencies, CI, tooling)

Checklist

  • I have read CONTRIBUTING.md
  • pnpm typecheck passes
  • pnpm lint passes
  • pnpm test passes (or targeted tests for my change)
  • pnpm format has been run
  • I have added/updated tests for my changes (if applicable)
  • User-visible strings in the admin UI are wrapped for translation (if applicable). Do not include messages.po changes except in translation PRs — a workflow extracts catalogs on merge to main.
  • I have added and reviewed the user-facing changeset (if this PR changes a published package)
  • New features link to an approved Discussion: https://github.com/emdash-cms/emdash/discussions/...
  • I have included screenshots below if this PR changes the UI

AI-generated code disclosure

  • This PR includes AI-generated code — model/tool: Claude Opus 5.5 (Claude Code)

Screenshots / test output

Not applicable (no UI). Two new tests in packages/cloudflare/tests/db/do-sql-collection-deletion.test.ts fail on main (expected 3 to be 1; expected { outcome: 'stale' } to deeply equal { outcome: 'fenced' }) and pass with this change. @emdash-cms/cloudflare suite: 503 passed, 2 skipped.

…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>
Copilot AI balanced review requested due to automatic review settings September 29, 2026 14:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@changeset-bot

changeset-bot Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 16f6cf1

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 12 packages
Name Type
@emdash-cms/cloudflare Patch
@emdash-cms/plugin-test Patch
@emdash-cms/admin Patch
@emdash-cms/auth Patch
@emdash-cms/blocks Patch
emdash Patch
create-emdash Patch
@emdash-cms/gutenberg-to-portable-text Patch
@emdash-cms/x402 Patch
@emdash-cms/auth-atproto Patch
@emdash-cms/plugin-embeds Patch
@emdash-cms/sandbox-workerd Patch

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

@emdashbot emdashbot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.ts and 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 DOQueryResult type docs and the sibling EmDashPreviewDB implementation.
  • 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:66

    The changes field now reports SQLite changes() (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:5

    The changeset is accurate, but it opens with the internal backend name. Leading with the public Kysely surface and the concrete user-visible symptom (numAffectedRows and 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.
    

@emdashbot emdashbot Bot added the review/awaiting-author Reviewed; waiting on the author to respond label Sep 29, 2026
@github-actions

github-actions Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@koikar

koikar commented Sep 29, 2026

Copy link
Copy Markdown
Contributor Author

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.
@github-actions github-actions Bot added review/needs-rereview New commits since the last review and removed review/awaiting-author Reviewed; waiting on the author to respond labels Sep 29, 2026
github-actions Bot added a commit that referenced this pull request Sep 29, 2026
@pkg-pr-new

pkg-pr-new Bot commented Sep 30, 2026

Copy link
Copy Markdown

Open in StackBlitz

@emdash-cms/admin

npm i https://pkg.pr.new/@emdash-cms/admin@3633

@emdash-cms/auth

npm i https://pkg.pr.new/@emdash-cms/auth@3633

@emdash-cms/auth-atproto

npm i https://pkg.pr.new/@emdash-cms/auth-atproto@3633

@emdash-cms/blocks

npm i https://pkg.pr.new/@emdash-cms/blocks@3633

@emdash-cms/cloudflare

npm i https://pkg.pr.new/@emdash-cms/cloudflare@3633

@emdash-cms/contentful-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/contentful-to-portable-text@3633

emdash

npm i https://pkg.pr.new/emdash@3633

create-emdash

npm i https://pkg.pr.new/create-emdash@3633

@emdash-cms/gutenberg-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/gutenberg-to-portable-text@3633

@emdash-cms/plugin-cli

npm i https://pkg.pr.new/@emdash-cms/plugin-cli@3633

@emdash-cms/plugin-test

npm i https://pkg.pr.new/@emdash-cms/plugin-test@3633

@emdash-cms/plugin-types

npm i https://pkg.pr.new/@emdash-cms/plugin-types@3633

@emdash-cms/registry-client

npm i https://pkg.pr.new/@emdash-cms/registry-client@3633

@emdash-cms/registry-lexicons

npm i https://pkg.pr.new/@emdash-cms/registry-lexicons@3633

@emdash-cms/registry-loader

npm i https://pkg.pr.new/@emdash-cms/registry-loader@3633

@emdash-cms/registry-moderation

npm i https://pkg.pr.new/@emdash-cms/registry-moderation@3633

@emdash-cms/registry-verification

npm i https://pkg.pr.new/@emdash-cms/registry-verification@3633

@emdash-cms/sandbox-workerd

npm i https://pkg.pr.new/@emdash-cms/sandbox-workerd@3633

@emdash-cms/x402

npm i https://pkg.pr.new/@emdash-cms/x402@3633

@emdash-cms/plugin-ai-moderation

npm i https://pkg.pr.new/@emdash-cms/plugin-ai-moderation@3633

@emdash-cms/plugin-atproto

npm i https://pkg.pr.new/@emdash-cms/plugin-atproto@3633

@emdash-cms/plugin-audit-log

npm i https://pkg.pr.new/@emdash-cms/plugin-audit-log@3633

@emdash-cms/plugin-color

npm i https://pkg.pr.new/@emdash-cms/plugin-color@3633

@emdash-cms/plugin-embeds

npm i https://pkg.pr.new/@emdash-cms/plugin-embeds@3633

@emdash-cms/plugin-field-kit

npm i https://pkg.pr.new/@emdash-cms/plugin-field-kit@3633

@emdash-cms/plugin-forms

npm i https://pkg.pr.new/@emdash-cms/plugin-forms@3633

@emdash-cms/plugin-webhook-notifier

npm i https://pkg.pr.new/@emdash-cms/plugin-webhook-notifier@3633

commit: 16f6cf1

@ascorbic ascorbic left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@emdashbot emdashbot Bot added review/approved Approved; no new commits since and removed review/needs-rereview New commits since the last review labels Sep 30, 2026
@ascorbic
ascorbic merged commit 33a00e4 into emdash-cms:main Sep 30, 2026
44 checks passed
@emdashbot

emdashbot Bot commented Sep 30, 2026

Copy link
Copy Markdown
Contributor

Thanks for contributing to EmDash! 🎉

Join the EmDash Discord, then run /link to connect your GitHub account and claim the Contributor role.

@emdashbot emdashbot Bot mentioned this pull request Sep 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants