From a253ff325d3e4fb9620ebb20a083d23630e82c6e Mon Sep 17 00:00:00 2001 From: Ruslan Talpa Date: Thu, 26 May 2016 10:32:46 +0300 Subject: [PATCH] don't check if proc exists, just call it and return 400 if it does not --- src/PostgREST/App.hs | 28 ++++++++++++---------------- src/PostgREST/DbStructure.hs | 14 -------------- test/Feature/QuerySpec.hs | 5 +++++ 3 files changed, 17 insertions(+), 30 deletions(-) diff --git a/src/PostgREST/App.hs b/src/PostgREST/App.hs index 25c6f090f..94a7df5bb 100644 --- a/src/PostgREST/App.hs +++ b/src/PostgREST/App.hs @@ -186,22 +186,18 @@ app dbStructure conf apiRequest = (ActionInvoke, TargetProc qi, Just (PayloadJSON (UniformObjects payload))) -> do - exists <- H.query qi doesProcExist - if exists - then do - let p = V.head payload - jwtSecret = configJwtSecret conf - respondToRange $ do - row <- H.query () (callProc qi p topLevelRange shouldCount) - returnJWT <- H.query qi doesProcReturnJWT - let (tableTotal, queryTotal, body) = fromMaybe (Just 0, 0, emptyArray) row - (status, contentRange) = rangeHeader queryTotal tableTotal - in - return $ responseLBS status [jsonH, contentRange] - (if returnJWT - then "{\"token\":\"" <> cs (tokenJWT jwtSecret body) <> "\"}" - else cs $ encode body) - else return notFound + let p = V.head payload + jwtSecret = configJwtSecret conf + respondToRange $ do + row <- H.query () (callProc qi p topLevelRange shouldCount) + returnJWT <- H.query qi doesProcReturnJWT + let (tableTotal, queryTotal, body) = fromMaybe (Just 0, 0, emptyArray) row + (status, contentRange) = rangeHeader queryTotal tableTotal + in + return $ responseLBS status [jsonH, contentRange] + (if returnJWT + then "{\"token\":\"" <> cs (tokenJWT jwtSecret body) <> "\"}" + else cs $ encode body) (ActionRead, TargetRoot, Nothing) -> do let encodeApi ti = encodeOpenAPI ti host port diff --git a/src/PostgREST/DbStructure.hs b/src/PostgREST/DbStructure.hs index c6e6a77f1..c8efa61b2 100644 --- a/src/PostgREST/DbStructure.hs +++ b/src/PostgREST/DbStructure.hs @@ -6,7 +6,6 @@ module PostgREST.DbStructure ( getDbStructure , accessibleTables -, doesProcExist , doesProcReturnJWT ) where @@ -104,19 +103,6 @@ decodeSynonyms cols = <*> HD.value HD.text <*> HD.value HD.text <*> HD.value HD.text <*> HD.value HD.text -doesProcExist :: H.Query QualifiedIdentifier Bool -doesProcExist = - H.statement sql encodeQi (HD.singleRow (HD.value HD.bool)) True - where - sql = [q| SELECT EXISTS ( - SELECT 1 - FROM pg_catalog.pg_namespace n - JOIN pg_catalog.pg_proc p - ON pronamespace = n.oid - WHERE nspname = $1 - AND proname = $2 - ) |] - doesProcReturnJWT :: H.Query QualifiedIdentifier Bool doesProcReturnJWT = H.statement sql encodeQi (HD.singleRow (HD.value HD.bool)) True diff --git a/test/Feature/QuerySpec.hs b/test/Feature/QuerySpec.hs index 73e31b506..24ae58671 100644 --- a/test/Feature/QuerySpec.hs +++ b/test/Feature/QuerySpec.hs @@ -477,6 +477,11 @@ spec = do request methodPost "/rpc/sayhello" (acceptHdrs "application/json") "sdfsdf" `shouldRespondWith` 400 + -- it used to be 404 and it makes sense but in another part we decided that it's good to return + -- PostgreSQL errors (and have the proxy handle them) and this saves us an aditional query on each rpc request + it "responds with 400 on an unexisting proc" $ + post "/rpc/fake" [json| {} |] `shouldRespondWith` 400 + context "unsupported verbs" $ do it "DELETE fails" $