Repository navigation
fix(postgrest)!: stop transforms from silently changing or dropping part of a request - #2026
Conversation
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 2 minutes. View limit details
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Merge Risk: ⚪ Minimal · up to
|
f94247d to
5b7aee5
Compare
…art of a request head() throws after a write instead of turning it into a HEAD, maybeSingle() asks for a single object on writes so PostgREST rolls back a multi-row write, typed count() returns a PostgrestTypedCountBuilder with filters only, an untyped rpc(...).head() sends its arguments as query parameters, rpc(get: true) accepts no params, and range() rejects an end before the start.
…eep maybeSingle() client-side for function calls head() on a function call throws when an argument would be read as an option or a filter, maybeSingle() leaves the Accept header of an rpc call alone, single() clears a preceding maybeSingle(), and the table executor rejects a count request that carries transforms.
d357f98 to
3e9dc14
Compare
Linear: https://linear.app/supabase/issue/SDK-2333/fixpostgrest-transforms-that-silently-change-or-drop-part-of-a-request
What changed
Six builder methods compiled but sent something other than what was asked for. Each one now either sends the right request or fails at the call site.
head()after a write. It used to turn an insert, upsert, update or delete into aHEAD, so the write never happened and the call still succeeded. Both surfaces now throw aStateErrorwhenhead()is called after a write. A select, a count and a function call still send aHEAD.maybeSingle()on a write. An insert, upsert, update or delete now sendsAccept: application/vnd.pgrst.object+json, assingle()does. A delete or update that matches several rows is rolled back by PostgREST before the error reaches the caller. Before, it was committed and then the call threw. The 406 that PostgREST returns when no row matched is turned back intonull(count0). Reads and function calls still enforce the limit client-side, so a read that finds nothing does not cause a 406.single()aftermaybeSingle()clears the flag, so a zero-row result still throwsPGRST116.count(). It returns a newPostgrestTypedCountBuilderwithwhere()and the request options (retry,requestTimeout,abortSignal,setHeader) but no transforms.limit()andrange()used to be dropped silently, andcsv(),select()andexplain()crashed with a null check error.PostgrestHttpTableExecutoralso rejects a hand-built count request that carries a limit, offset, ordering, embed scope or result shape.rpc(...).head(). The arguments are moved into the query string, which the typed executor already did. AHEADhas no body to fall back on, so on both surfaceshead()throws anArgumentErrorfor arguments PostgREST would not pass to the function: a name likelimitororder, a value likeeq.1, anull, or arguments that are not aMap. The check is the one the typed executor already used to decide whether a read-only call can be aGET, now shared aspostgrestArgumentsFitQuery.rpc('fn', get: true)without params. It sends a bareGETinstead of throwingArgumentError: argument must be a Map: null. Both GET and HEAD calls now share one helper that builds the query string from the arguments.range(from, to)withto < from. It throws anArgumentErroron the untyped builder (includingreferencedTable), the typed builder andPostgrestEmbeddedScope.range. The typed executor now sets the offset and limit through an internalwindow()step instead of re-deriving arange(), so.range(5, 9).limit(0)still sendsoffset=5&limit=0and does not trip the new check.maybeSingle()on writesItem 2 deliberately partly reverses #1779, which stopped sending the object
Acceptheader for every request method so that a write matching no row would not log a 406. Without the header, PostgREST commits every row a write matched before the client can count them, so amaybeSingle()update or delete with a too broad filter changes several rows and only then throws. Only the header lets PostgREST roll that write back. The known cost is that a write matching no row logs a 406 on the server again, while the caller still getsnull. Reads and function calls, where nothing is changed and "fetch it if it exists" is the common case, keep the client-side check and stay free of 406s.Breaking changes
MIGRATION.mdgets one section, "Transforms that sent a different request now throw or narrow", which is also linked from "Changes the compiler will not catch".PostgrestTypedCountBuilderand its members are registered insdk-compliance.yaml.checkPostgrestRange,checkPostgrestHeadArguments,postgrestArgumentsFitQueryandPostgrestTransformBuilder.windoware@internal, and the top-level ones are hidden from the library export.Tests
test/transform_request_test.dartadds a regression test for each item, run againstMockSupabaseHttpClient. Each one checks the method, query parameters and headers that go out, on both surfaces where both exist. It also covers the review follow-ups:head()arguments,maybeSingle()on an rpc call,maybeSingle().single()and the executor's count guard.test/maybe_single_test.dartnow checks the read-only behavior. The write cases moved into the new file.test/basic_test.dartadds two tests against the local stack. AmaybeSingle()update that matches three rows throwsPGRST116, and afterwards all three rows are still unchanged. An update that matches no row resolves tonull.Summary by CodeRabbit
New Features
Behavior Changes
head()is supported for reads and eligible function calls; using it after a write now throws an error.maybeSingle()returnsnullfor zero-row results. Writes matching multiple rows are rolled back before an error is reported.