Commit 80a895b
fix: chunk SEO IN clause to stay within D1 SQL variable limit (#422)
* fix: chunk byline IN clauses to stay within D1 SQL variable limit
Fixes #219. hydrateEntryBylines builds unbounded IN (?, ?, …) clauses
that exceed Cloudflare D1's bound-parameter limit on large collections.
Adds a chunks() utility and applies it defense-in-depth at the
repository level: getContentBylinesMany, findByUserIds, and
getAuthorIds now batch IDs in groups of 50.
* chore: add changeset for byline chunking fix
* fix: deduplicate content IDs before chunking and add integration tests
Deduplicates contentIds in getContentBylinesMany to prevent duplicate
credits when the same ID appears across chunk boundaries. Adds tests
for the duplication edge case and an end-to-end getBylinesForEntries
test spanning both explicit and inferred byline paths.
* fix: chunk SEO IN clause to stay within D1 SQL variable limit
SeoRepository.getMany builds a WHERE content_id IN (?, ?, ...)
clause alongside a collection = ? filter. On Cloudflare D1, which
caps bound parameters at 100 per query, passing 100 content ids
produces 101 parameters and trips the limit:
D1_ERROR: too many SQL variables at offset 369: SQLITE_ERROR
This is the same root cause as the byline hydration fix in the
sibling commit, but on a different repository that wasn't covered
there. SeoRepository.getMany is called from handleContentList
before hydrateBylinesMany, so on any collection with has_seo = 1
and >= 99 items, it's the first function to fail on the admin
content list endpoint.
Apply the same chunking pattern using the shared chunks() helper
and SQL_BATCH_SIZE constant. Deduplicate contentIds before
chunking for consistency with the byline fix. Pre-fill result
with defaults so the two-pass resolve-then-fill-missing logic
collapses to a single pass.
Adds unit tests covering:
- input size larger than SQL_BATCH_SIZE, real ids spread across chunks
- all-missing ids get defaults
- duplicate input ids resolve cleanly without duplicate rows
Repro of the underlying D1 limit for the record:
wrangler d1 execute <db> --remote --command \
"SELECT 1 WHERE 'x' = ? AND 1 IN (?,?,...x100)"
-> too many SQL variables at offset 231: SQLITE_ERROR [code: 7500]
* style: format
---------
Co-authored-by: emdashbot[bot] <emdashbot[bot]@users.noreply.github.com>
Co-authored-by: Matt Kane <mkane@cloudflare.com>1 parent a13c4ec commit 80a895b
3 files changed
Lines changed: 140 additions & 17 deletions
File tree
- .changeset
- packages/core
- src/database/repositories
- tests/unit/database/repositories
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
| |||
61 | 62 | | |
62 | 63 | | |
63 | 64 | | |
64 | | - | |
| 65 | + | |
65 | 66 | | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
66 | 71 | | |
67 | 72 | | |
68 | 73 | | |
69 | 74 | | |
70 | 75 | | |
71 | 76 | | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
| 77 | + | |
83 | 78 | | |
84 | | - | |
85 | | - | |
86 | | - | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
87 | 93 | | |
88 | 94 | | |
89 | 95 | | |
90 | 96 | | |
91 | 97 | | |
92 | 98 | | |
93 | | - | |
94 | | - | |
95 | 99 | | |
96 | 100 | | |
97 | 101 | | |
| |||
Lines changed: 114 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 | + | |
0 commit comments