fix(memory): cap the limit with LIMIT -1 when offset is supplied without a limit

WX
18d ago
+97-0

Description

D1 and libSQL memory adapters append OFFSET ? without a preceding LIMIT clause when a query supplies an offset only — queryConversations({ offset: 2 }) and queryWorkflowRuns({ offset: 2 }) on both adapters (and getConversationsByUserId, which forwards its options). SQLite requires LIMIT before OFFSET, so the database rejects the statement with SQLITE_ERROR: near "OFFSET": syntax error instead of returning the remaining rows.

The adapters now emit LIMIT -1 (SQLite for "no cap") only when an offset is present and no explicit limit was supplied:

  • explicit limits stay bound parameters, exactly as today
  • the falsy-limit (0) and missing-offset behaviors are unchanged
  • Postgres is untouched — its grammar allows OFFSET without LIMIT

Evidence

  • new specs pin the SQL shape on all four paths (D1 + libSQL, conversations + workflow runs): offset-only → LIMIT -1 OFFSET ? with [offset] args; explicit limit → LIMIT ? OFFSET ? unchanged
  • real-database check with @libsql/client on :memory:: old shape reproduces the issue's near "OFFSET": syntax error, new shape returns rows 3,4,5 for OFFSET 2 over 5 rows
  • vitest run packages/cloudflare-d1 packages/libsql/src/memory-v2-adapter.spec.ts → 9 passed; the libSQL observability suite has 27 pre-existing failures on this Windows environment (file-DB directory creation) with an identical failure set before and after the change

Fixes #1429


Summary by cubic

Fixes offset-only pagination in the D1 and libSQL memory adapters. queryConversations, queryWorkflowRuns, and getConversationsByUserId now emit LIMIT -1 before OFFSET when a query supplies an offset without a limit, so SQLite no longer rejects the statement with near "OFFSET": syntax error and returns the remaining rows. Explicit limits stay bound parameters, and Postgres is untouched.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed offset-only pagination in D1 and libSQL memory adapters. Queries can now skip records without requiring a limit, while explicit limits continue to work as expected.