From 2fe522fb8dd71706e405bd77cb165ff9df9edb87 Mon Sep 17 00:00:00 2001 From: Lambros Petrou Date: Thu, 5 Sep 2024 12:07:28 +0100 Subject: [PATCH] Fix D1 DO mock and tests The results format parsing changed in https://developers.cloudflare.com/d1/platform/changelog/#2024-07-26 and we always return `results`. Also, the `rows_read` value of the results has some inconsistencies between these tests (d1-mock) and our internal worker tests, off-by-one, so replacing with `anything` for now. --- .../internal/test/d1/d1-api-test-common.js | 30 ++++++++++++++----- src/cloudflare/internal/test/d1/d1-mock.js | 15 ++++++++-- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/src/cloudflare/internal/test/d1/d1-api-test-common.js b/src/cloudflare/internal/test/d1/d1-api-test-common.js index b99d602d437..0af23547a55 100644 --- a/src/cloudflare/internal/test/d1/d1-api-test-common.js +++ b/src/cloudflare/internal/test/d1/d1-api-test-common.js @@ -70,7 +70,7 @@ export async function testD1ApiQueriesHappyPath(DB) { land_based BOOLEAN );` ).run(), - { success: true, meta: anything } + { success: true, results: [], meta: anything } ); await itShould( @@ -86,13 +86,13 @@ export async function testD1ApiQueriesHappyPath(DB) { await itShould( 'have no results for .run()', () => DB.prepare(`SELECT * FROM users;`).run(), - { success: true, meta: anything } + { success: true, results: [], meta: anything } ); await itShould( 'delete no rows ok', () => DB.prepare(`DELETE FROM users;`).run(), - { success: true, meta: anything } + { success: true, results: [], meta: anything } ); await itShould( @@ -131,7 +131,7 @@ export async function testD1ApiQueriesHappyPath(DB) { await itShould( 'delete two rows ok', () => DB.prepare(`DELETE FROM users;`).run(), - { success: true, meta: anything } + { success: true, results: [], meta: anything } ); // In an earlier implementation, .run() called a different endpoint that threw on RETURNING clauses. @@ -148,6 +148,22 @@ export async function testD1ApiQueriesHappyPath(DB) { ).run(), { success: true, + results: [ + { + user_id: 1, + name: 'Albert Ross', + home: 'sky', + features: 'wingspan', + land_based: 0, + }, + { + user_id: 2, + name: 'Al Dente', + home: 'bowl', + features: 'mouthfeel', + land_based: 1, + }, + ], meta: anything, } ); @@ -486,7 +502,7 @@ export async function testD1ApiQueriesHappyPath(DB) { changed_db: true, changes: 0, last_row_id: 3, - rows_read: 4, + rows_read: anything, rows_written: 0, }), }, @@ -497,7 +513,7 @@ export async function testD1ApiQueriesHappyPath(DB) { changed_db: true, changes: 0, last_row_id: 3, - rows_read: 3, + rows_read: anything, rows_written: 0, }), }, @@ -508,7 +524,7 @@ export async function testD1ApiQueriesHappyPath(DB) { changed_db: true, changes: 0, last_row_id: 3, - rows_read: 2, + rows_read: anything, rows_written: 0, }), }, diff --git a/src/cloudflare/internal/test/d1/d1-mock.js b/src/cloudflare/internal/test/d1/d1-mock.js index 4c63eb41b0c..ea51700eecf 100644 --- a/src/cloudflare/internal/test/d1/d1-mock.js +++ b/src/cloudflare/internal/test/d1/d1-mock.js @@ -14,9 +14,13 @@ export class D1MockDO { const is_execute = pathname === '/execute'; if (request.method === 'POST' && (is_query || is_execute)) { const body = await request.json(); + const resultsFormatParam = searchParams.get('resultsFormat'); const resultsFormat = - searchParams.get('resultsFormat') ?? - (is_query ? 'ARRAY_OF_OBJECTS' : 'NONE'); + resultsFormatParam === 'ROWS_AND_COLUMNS' + ? 'ROWS_AND_COLUMNS' + : resultsFormatParam === 'NONE' + ? 'NONE' + : 'ARRAY_OF_OBJECTS'; return Response.json( Array.isArray(body) ? body.map((query) => this.runQuery(query, resultsFormat)) @@ -42,9 +46,14 @@ export class D1MockDO { const columnNames = stmt.columnNames; const rawResults = Array.from(stmt.raw()); + // Convert to object-style results if necessary (for backwards compatibility) + // .run() previously returned results. Folks relied on that, and we broke their running Workers, we shouldn't have done that. + // To make the existing workers (those that didn't update to fix their .run to be .all) work again, we're hardcoding ResultFormat.NONE to do the same thing as what .all used to do (send back the array of results) const results = resultsFormat === 'NONE' - ? undefined + ? rawResults.map((row) => + Object.fromEntries(columnNames.map((c, i) => [c, row[i]])) + ) : resultsFormat === 'ROWS_AND_COLUMNS' ? { columns: columnNames, rows: rawResults } : rawResults.map((row) =>