Skip to content

Fix D1 dashboard recent items query#910

Closed
masonjames wants to merge 2 commits intoemdash-cms:mainfrom
masonjames:fix/dashboard-d1-recent-items-union
Closed

Fix D1 dashboard recent items query#910
masonjames wants to merge 2 commits intoemdash-cms:mainfrom
masonjames:fix/dashboard-d1-recent-items-union

Conversation

@masonjames
Copy link
Copy Markdown
Contributor

@masonjames masonjames commented May 4, 2026

What does this PR do?

Fixes the D1 dashboard 500 from recent-items loading by replacing the single cross-collection UNION ALL query with per-collection capped queries that are merged and limited in JS. This preserves the dashboard API shape while avoiding D1's low compound SELECT limit on sites with many collections.

Closes #895

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 a changeset (if this PR changes a published package)
  • New features link to an approved Discussion: N/A — bug fix

AI-generated code disclosure

  • This PR includes AI-generated code — model/tool: GPT-5 Codex

Screenshots / test output

Not visual.

Local targeted checks:

pnpm --silent lint:quick
=> { "diagnostics": [] }

cd packages/core && pnpm exec vitest run tests/unit/api/dashboard-handlers.test.ts
=> Test Files 1 passed; Tests 11 passed

pnpm --filter emdash typecheck
=> passed

pnpm format
=> passed

GitHub CI is passing, including Typecheck, Lint, Tests, Integration Tests, Browser Tests, Smoke Tests, E2E shards, Format, Version Check, Validate Plugins, and Validate PR.

Additional review:

Adversarial review cycle 1: found flaky test assertion on tied timestamps.
Fix applied: removed ordering-sensitive assertion from the D1 regression test.
Adversarial review cycle 2: No bugs found.

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 4, 2026

🦋 Changeset detected

Latest commit: 9a2af65

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

This PR includes changesets to release 13 packages
Name Type
emdash Patch
@emdash-cms/cloudflare Patch
@emdash-cms/fixture-perf-site Patch
@emdash-cms/perf-demo-site Patch
@emdash-cms/cache-demo-site Patch
@emdash-cms/admin Patch
@emdash-cms/auth Patch
@emdash-cms/blocks Patch
@emdash-cms/gutenberg-to-portable-text Patch
@emdash-cms/x402 Patch
create-emdash Patch
@emdash-cms/auth-atproto Patch
@emdash-cms/plugin-embeds 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

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 4, 2026

Open in StackBlitz

@emdash-cms/admin

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

@emdash-cms/auth

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

@emdash-cms/blocks

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

@emdash-cms/cloudflare

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

emdash

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

create-emdash

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

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

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

@emdash-cms/x402

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

@emdash-cms/plugin-ai-moderation

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

@emdash-cms/plugin-atproto

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

@emdash-cms/plugin-audit-log

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

@emdash-cms/plugin-color

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

@emdash-cms/plugin-embeds

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

@emdash-cms/plugin-forms

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

@emdash-cms/plugin-webhook-notifier

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

commit: 9a2af65

@masonjames masonjames marked this pull request as ready for review May 4, 2026 02:06
Copilot AI review requested due to automatic review settings May 4, 2026 02:06
@masonjames
Copy link
Copy Markdown
Contributor Author

Closing as a duplicate of #896. I missed that #896 already fixes #895 with the same per-collection recent-items approach, has a changeset, and has green CI. The only notable difference in this PR was a D1-like regression test that rejects compound UNION ALL query construction; maintainers can still lift that from this closed PR if useful, but #896 should be the active fix.

@masonjames masonjames closed this May 4, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes the dashboard’s “recent items” query so it no longer triggers D1’s low compound-SELECT limit. In the core API, it replaces the cross-collection UNION ALL approach with per-collection capped queries that are merged in application code, while keeping the dashboard response shape unchanged.

Changes:

  • Reworked fetchRecentItems() to query each collection separately, merge the results in JS, and return the top 10 most recently updated items.
  • Added a regression-oriented unit test intended to guard against reintroducing a compound recent-items query on D1.
  • Added a patch changeset for the published emdash package.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
packages/core/src/api/handlers/dashboard.ts Replaces the dashboard recent-items SQL strategy with per-collection queries plus JS merge/sort/limit logic.
packages/core/tests/unit/api/dashboard-handlers.test.ts Adds a new regression test for the D1 compound-select failure mode.
.changeset/stale-actors-sneeze.md Records the patch release note for the D1 dashboard fix.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +15 to +17
const rejectCompoundSelectPlugin = {
transformQuery(args) {
if (JSON.stringify(args.node).includes(" UNION ALL ")) {
Comment on lines +136 to +137
const d1LikeDb = db.withPlugin(rejectCompoundSelectPlugin);
const result = await handleDashboardStats(d1LikeDb);
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.

Dashboard 500 on D1: fetchRecentItems UNION ALL exceeds SQLITE_LIMIT_COMPOUND_SELECT

2 participants