Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions server/utils/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const D1_TERMINAL_METHODS = new Set<PropertyKey>(['first', 'run', 'all', 'raw'])
const RETRYABLE_D1_READ_MESSAGES = [
'currently processing a long-running import',
'currently processing a long-running export',
'currently processing an import job',
'd1 db is overloaded',
]

Expand Down
14 changes: 14 additions & 0 deletions test/unit/platform-d1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ describe('createPlatformD1', () => {
expect(run).toHaveBeenCalledTimes(2)
})

it('retries a read blocked by a D1 import job', async () => {
const run = vi.fn()
.mockRejectedValueOnce(new Error('D1_ERROR: Currently processing an import job.'))
.mockResolvedValue({ id: 1 })
const { db } = database(run)
const env = { DB: db } as Cloudflare.Env

const platformDb = createPlatformD1(env, { sleep: async () => {} }).database
const row = await platformDb.prepare('SELECT id FROM skills').first<{ id: number }>()

expect(row).toEqual({ id: 1 })
expect(run).toHaveBeenCalledTimes(2)
})

it('retries a read blocked by a D1 export', async () => {
const run = vi.fn()
.mockRejectedValueOnce(new Error('D1_ERROR: Currently processing a long-running export.'))
Expand Down
Loading