Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3cef4b70b0 | ||
|
|
6f97c34a86 | ||
|
|
bdac90491d | ||
|
|
5961f7a116 | ||
|
|
17cd2725fd | ||
|
|
5e7606134a | ||
|
|
dfa9055c34 | ||
|
|
6907e7f979 | ||
|
|
8cf68c63d9 | ||
|
|
30b5859b28 | ||
|
|
0a1d83ce8f | ||
|
|
d7511a2637 | ||
|
|
2066220244 | ||
|
|
93f10adb3c | ||
|
|
56bd5d5f91 | ||
|
|
70e95649fd | ||
|
|
2b46afe1ec | ||
|
|
fa1e92fdf2 | ||
|
|
b1a8bd2391 | ||
|
|
69a76a627f | ||
|
|
105671e51a | ||
|
|
ecf0e9213f | ||
|
|
1c6ded16d1 | ||
|
|
6fc9d5191a | ||
|
|
80f09780cc | ||
|
|
9e3454129f | ||
|
|
d34afe861a | ||
|
|
30dfadec7b | ||
|
|
2513c00039 | ||
|
|
100bf494ac | ||
|
|
e8188b0d41 | ||
|
|
3958ebbb05 | ||
|
|
37e7398a85 | ||
|
|
9ea7529f30 | ||
|
|
384767708b | ||
|
|
6bcbb124d2 | ||
|
|
f6c1ff810e | ||
|
|
f80cfbf165 | ||
|
|
d8896be2c1 |
@@ -1,11 +1,11 @@
|
|||||||
### Environment
|
### Environment
|
||||||
|
|
||||||
* Version of postgrest:
|
* PostgreSQL version: (if using docker, specify the image)
|
||||||
|
* PostgREST version: (if using docker, specify the image)
|
||||||
* Operating system:
|
* Operating system:
|
||||||
* Running in docker:
|
|
||||||
* [ ] yes
|
|
||||||
* [ ] no
|
|
||||||
|
|
||||||
### Description of issue
|
### Description of issue
|
||||||
|
|
||||||
(Expected behavior vs actual behavior.)
|
(Expected behavior vs actual behavior)
|
||||||
|
|
||||||
|
(Steps to reproduce: Include a minimal SQL definition plus how you make the request to PostgREST and the response body)
|
||||||
|
|||||||
+14
@@ -0,0 +1,14 @@
|
|||||||
|
# Sponsors & Backers
|
||||||
|
|
||||||
|
PostgREST ongoing development is only possible thanks to our Sponsors and Backers, listed below. If you'd like to join them, you can do so by supporting the PostgREST organization on [Patreon](https://www.patreon.com/postgrest).
|
||||||
|
|
||||||
|
## Lead Backers
|
||||||
|
|
||||||
|
- [Christiaan Westerbeek](https://devotis.nl)
|
||||||
|
- [Daniel Babiak](https://github.com/d-babiak)
|
||||||
|
- [Michel Pelletier](https://github.com/michelp/)
|
||||||
|
|
||||||
|
## Backers
|
||||||
|
|
||||||
|
- Tsingson Qin
|
||||||
|
- Jay Hannah
|
||||||
+32
-1
@@ -9,8 +9,37 @@ This project adheres to [Semantic Versioning](http://semver.org/).
|
|||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
|
## [5.1.0] - 2018-08-31
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- #1099, Add support for getting json/jsonb by array index - @steve-chavez
|
||||||
|
- #1145, Add materialized view columns to OpenAPI output - @steve-chavez
|
||||||
|
- #709, Allow embedding on views with subselects/CTE - @steve-chavez
|
||||||
|
- #1148, OpenAPI: add `required` section for the non-nullable columns - @laughedelic
|
||||||
|
- #1158, Add summary to OpenAPI doc for RPC functions - @mdr1384
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
- #1113, Fix UPSERT failing when having a camel case PK column - @steve-chavez
|
||||||
|
- #945, Fix slow start-up time on big schemas - @steve-chavez
|
||||||
|
- #1129, Fix view embedding when table is capitalized - @steve-chavez
|
||||||
|
- #1149, OpenAPI: Change `GET` response type to array - @laughedelic
|
||||||
|
- #1152, Fix RPC failing when having arguments with reserved or uppercase keywords - @mdr1384
|
||||||
|
- #905, Fix intermittent empty replies - @steve-chavez
|
||||||
|
- #1139, Fix JWTIssuedAtFuture failure for valid iat claim - @steve-chavez
|
||||||
|
- #1141, Fix app.settings resetting on pool timeout - @steve-chavez
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
|
- #1099, Numbers in json path `?select=data->1->>key` now get treated as json array indexes instead of keys - @steve-chavez
|
||||||
|
- #1128, Allow finishing a json path with a single arrow `->`. Now a json can be obtained without resorting to casting, Previously: `/json_arr?select=data->>2::json`, now: `/json_arr?select=data->2` - @steve-chavez
|
||||||
|
- #724, Change server-host default of *4 to 127.0.0.1
|
||||||
|
|
||||||
|
### Deprecated
|
||||||
|
|
||||||
|
- #724, SIGHUP deprecated, SIGUSR1 should be used instead
|
||||||
|
|
||||||
## [0.5.0.0] - 2018-05-14
|
## [0.5.0.0] - 2018-05-14
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
@@ -32,7 +61,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
|
|||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
- Computed columns now only work if they belong to the db-schema - @steve-chavez
|
- #828, A `SET SCHEMA <db-schema>` is done on each request, this has the following implications:
|
||||||
|
- Computed columns now only work if they belong to the db-schema
|
||||||
|
- Stored procedures might require a `search_path` to work properly, for further details see https://postgrest.org/en/v5.0/api.html#explicit-qualification
|
||||||
- To use RPC now the `json_to_record/json_to_recordset` functions are needed, these are available starting from PostgreSQL 9.4 - @steve-chavez
|
- To use RPC now the `json_to_record/json_to_recordset` functions are needed, these are available starting from PostgreSQL 9.4 - @steve-chavez
|
||||||
- Overloaded functions now depend on the `dbStructure`, restart/sighup may be needed for their correct functioning - @steve-chavez
|
- Overloaded functions now depend on the `dbStructure`, restart/sighup may be needed for their correct functioning - @steve-chavez
|
||||||
- #1098, Removed support for:
|
- #1098, Removed support for:
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
</a>
|
</a>
|
||||||
[](https://gitter.im/begriffs/postgrest)
|
[](https://gitter.im/begriffs/postgrest)
|
||||||
[](http://postgrest.org)
|
[](http://postgrest.org)
|
||||||
|
[](http://hackage.haskell.org/package/postgrest)
|
||||||
[](https://hub.docker.com/r/postgrest/postgrest/)
|
[](https://hub.docker.com/r/postgrest/postgrest/)
|
||||||
[](https://www.patreon.com/postgrest)
|
[](https://www.patreon.com/postgrest)
|
||||||
|
|
||||||
@@ -108,10 +109,10 @@ and the [API guide](http://postgrest.org/en/stable/api.html).
|
|||||||
|
|
||||||
### Thanks
|
### Thanks
|
||||||
|
|
||||||
The PostgREST organization is grateful to the generous project
|
The PostgREST organization is grateful to:
|
||||||
[contributors](https://github.com/PostgREST/postgrest/graphs/contributors)
|
|
||||||
who have improved PostgREST immensely with their code and good
|
- The project [sponsors and backers](https://github.com/PostgREST/postgrest/blob/master/BACKERS.md) who support PostgREST's development.
|
||||||
judgement. See more details in the
|
- The project [contributors](https://github.com/PostgREST/postgrest/graphs/contributors) who have improved PostgREST immensely with their code
|
||||||
[changelog](https://github.com/PostgREST/postgrest/blob/master/CHANGELOG.md).
|
and good judgement. See more details in the [changelog](https://github.com/PostgREST/postgrest/blob/master/CHANGELOG.md).
|
||||||
|
|
||||||
The cool logo came from [Mikey Casalaina](https://github.com/casalaina).
|
The cool logo came from [Mikey Casalaina](https://github.com/casalaina).
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "PostgREST",
|
"name": "PostgREST",
|
||||||
"description": "RESTful API for any PostgreSQL database.",
|
"description": "RESTful API for any PostgreSQL database.",
|
||||||
"logo": "https://halcyon.sh/logo.svg",
|
"logo": "https://avatars2.githubusercontent.com/u/15115011",
|
||||||
"repository": "https://github.com/PostgREST/postgrest",
|
"repository": "https://github.com/PostgREST/postgrest",
|
||||||
"env": {
|
"env": {
|
||||||
"BUILDPACK_URL": {
|
"BUILDPACK_URL": {
|
||||||
@@ -10,10 +10,10 @@
|
|||||||
},
|
},
|
||||||
"POSTGREST_VER": {
|
"POSTGREST_VER": {
|
||||||
"description": "Version of PostgREST to deploy",
|
"description": "Version of PostgREST to deploy",
|
||||||
"value": "0.5.0.0"
|
"value": "5.1.0"
|
||||||
},
|
},
|
||||||
"DB_URI": {
|
"DB_URI": {
|
||||||
"description": "Database connection string",
|
"description": "Database connection string, e.g. postgres://user:pass@xxxxxxx.rds.amazonaws.com/mydb",
|
||||||
"required": true
|
"required": true
|
||||||
},
|
},
|
||||||
"DB_SCHEMA": {
|
"DB_SCHEMA": {
|
||||||
|
|||||||
+2
-1
@@ -35,7 +35,8 @@ ENV PGRST_DB_URI= \
|
|||||||
PGRST_SECRET_IS_BASE64=false \
|
PGRST_SECRET_IS_BASE64=false \
|
||||||
PGRST_JWT_AUD= \
|
PGRST_JWT_AUD= \
|
||||||
PGRST_MAX_ROWS= \
|
PGRST_MAX_ROWS= \
|
||||||
PGRST_PRE_REQUEST=
|
PGRST_PRE_REQUEST= \
|
||||||
|
PGRST_ROLE_CLAIM_KEY=".role"
|
||||||
|
|
||||||
# PostgREST reads /etc/postgrest.conf so map the configuration
|
# PostgREST reads /etc/postgrest.conf so map the configuration
|
||||||
# file in when you run this container
|
# file in when you run this container
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ server-proxy-uri = "$(PGRST_SERVER_PROXY_URI)"
|
|||||||
jwt-secret = "$(PGRST_JWT_SECRET)"
|
jwt-secret = "$(PGRST_JWT_SECRET)"
|
||||||
secret-is-base64 = "$(PGRST_SECRET_IS_BASE64)"
|
secret-is-base64 = "$(PGRST_SECRET_IS_BASE64)"
|
||||||
jwt-aud = "$(PGRST_JWT_AUD)"
|
jwt-aud = "$(PGRST_JWT_AUD)"
|
||||||
|
role-claim-key = "$(PGRST_ROLE_CLAIM_KEY)"
|
||||||
|
|
||||||
max-rows = "$(PGRST_MAX_ROWS)"
|
max-rows = "$(PGRST_MAX_ROWS)"
|
||||||
pre-request = "$(PGRST_PRE_REQUEST)"
|
pre-request = "$(PGRST_PRE_REQUEST)"
|
||||||
|
|||||||
+13
-20
@@ -7,8 +7,7 @@ import PostgREST.App (postgrest)
|
|||||||
import PostgREST.Config (AppConfig (..),
|
import PostgREST.Config (AppConfig (..),
|
||||||
minimumPgVersion,
|
minimumPgVersion,
|
||||||
prettyVersion, readOptions)
|
prettyVersion, readOptions)
|
||||||
import PostgREST.DbStructure (getDbStructure, getPgVersion,
|
import PostgREST.DbStructure (getDbStructure, getPgVersion)
|
||||||
fillSessionWithSettings)
|
|
||||||
import PostgREST.Error (encodeError)
|
import PostgREST.Error (encodeError)
|
||||||
import PostgREST.OpenAPI (isMalformedProxyUri)
|
import PostgREST.OpenAPI (isMalformedProxyUri)
|
||||||
import PostgREST.Types (DbStructure, Schema, PgVersion(..))
|
import PostgREST.Types (DbStructure, Schema, PgVersion(..))
|
||||||
@@ -33,8 +32,7 @@ import qualified Hasql.Pool as P
|
|||||||
import qualified Hasql.Session as H
|
import qualified Hasql.Session as H
|
||||||
import Network.Wai.Handler.Warp (defaultSettings,
|
import Network.Wai.Handler.Warp (defaultSettings,
|
||||||
runSettings, setHost,
|
runSettings, setHost,
|
||||||
setPort, setServerName,
|
setPort, setServerName)
|
||||||
setTimeout)
|
|
||||||
import System.IO (BufferMode (..),
|
import System.IO (BufferMode (..),
|
||||||
hSetBuffering)
|
hSetBuffering)
|
||||||
|
|
||||||
@@ -64,11 +62,10 @@ connectionWorker
|
|||||||
:: ThreadId -- ^ This thread is killed if pg version is unsupported
|
:: ThreadId -- ^ This thread is killed if pg version is unsupported
|
||||||
-> P.Pool -- ^ The PostgreSQL connection pool
|
-> P.Pool -- ^ The PostgreSQL connection pool
|
||||||
-> Schema -- ^ Schema PostgREST is serving up
|
-> Schema -- ^ Schema PostgREST is serving up
|
||||||
-> [(Text, Text)] -- ^ Settings or Environment passed in through the config
|
|
||||||
-> IORef (Maybe DbStructure) -- ^ mutable reference to 'DbStructure'
|
-> IORef (Maybe DbStructure) -- ^ mutable reference to 'DbStructure'
|
||||||
-> IORef Bool -- ^ Used as a binary Semaphore
|
-> IORef Bool -- ^ Used as a binary Semaphore
|
||||||
-> IO ()
|
-> IO ()
|
||||||
connectionWorker mainTid pool schema settings refDbStructure refIsWorkerOn = do
|
connectionWorker mainTid pool schema refDbStructure refIsWorkerOn = do
|
||||||
isWorkerOn <- readIORef refIsWorkerOn
|
isWorkerOn <- readIORef refIsWorkerOn
|
||||||
unless isWorkerOn $ do
|
unless isWorkerOn $ do
|
||||||
atomicWriteIORef refIsWorkerOn True
|
atomicWriteIORef refIsWorkerOn True
|
||||||
@@ -86,7 +83,6 @@ connectionWorker mainTid pool schema settings refDbStructure refIsWorkerOn = do
|
|||||||
("Cannot run in this PostgreSQL version, PostgREST needs at least "
|
("Cannot run in this PostgreSQL version, PostgREST needs at least "
|
||||||
<> pgvName minimumPgVersion)
|
<> pgvName minimumPgVersion)
|
||||||
killThread mainTid
|
killThread mainTid
|
||||||
fillSessionWithSettings settings
|
|
||||||
dbStructure <- getDbStructure schema actualPgVersion
|
dbStructure <- getDbStructure schema actualPgVersion
|
||||||
liftIO $ atomicWriteIORef refDbStructure $ Just dbStructure
|
liftIO $ atomicWriteIORef refDbStructure $ Just dbStructure
|
||||||
case result of
|
case result of
|
||||||
@@ -152,8 +148,7 @@ main = do
|
|||||||
appSettings =
|
appSettings =
|
||||||
setHost ((fromString . toS) host) -- Warp settings
|
setHost ((fromString . toS) host) -- Warp settings
|
||||||
. setPort port
|
. setPort port
|
||||||
. setServerName (toS $ "postgrest/" <> prettyVersion)
|
. setServerName (toS $ "postgrest/" <> prettyVersion) $
|
||||||
. setTimeout 3600 $
|
|
||||||
defaultSettings
|
defaultSettings
|
||||||
|
|
||||||
-- Checks that the provided proxy uri is formated correctly
|
-- Checks that the provided proxy uri is formated correctly
|
||||||
@@ -186,7 +181,6 @@ main = do
|
|||||||
mainTid
|
mainTid
|
||||||
pool
|
pool
|
||||||
(configSchema conf)
|
(configSchema conf)
|
||||||
(configSettings conf)
|
|
||||||
refDbStructure
|
refDbStructure
|
||||||
refIsWorkerOn
|
refIsWorkerOn
|
||||||
--
|
--
|
||||||
@@ -204,15 +198,15 @@ main = do
|
|||||||
throwTo mainTid UserInterrupt
|
throwTo mainTid UserInterrupt
|
||||||
) Nothing
|
) Nothing
|
||||||
|
|
||||||
void $ installHandler sigHUP (
|
forM_ [sigHUP, sigUSR1] $ \sig ->
|
||||||
Catch $ connectionWorker
|
void $ installHandler sig (
|
||||||
mainTid
|
Catch $ connectionWorker
|
||||||
pool
|
mainTid
|
||||||
(configSchema conf)
|
pool
|
||||||
(configSettings conf)
|
(configSchema conf)
|
||||||
refDbStructure
|
refDbStructure
|
||||||
refIsWorkerOn
|
refIsWorkerOn
|
||||||
) Nothing
|
) Nothing
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
@@ -230,7 +224,6 @@ main = do
|
|||||||
mainTid
|
mainTid
|
||||||
pool
|
pool
|
||||||
(configSchema conf)
|
(configSchema conf)
|
||||||
(configSettings conf)
|
|
||||||
refDbStructure
|
refDbStructure
|
||||||
refIsWorkerOn)
|
refIsWorkerOn)
|
||||||
|
|
||||||
|
|||||||
+22
-19
@@ -2,14 +2,16 @@ name: postgrest
|
|||||||
description: Reads the schema of a PostgreSQL database and creates RESTful routes
|
description: Reads the schema of a PostgreSQL database and creates RESTful routes
|
||||||
for the tables and views, supporting all HTTP verbs that security
|
for the tables and views, supporting all HTTP verbs that security
|
||||||
permits.
|
permits.
|
||||||
version: 0.5.0.0
|
version: 5.1.0
|
||||||
synopsis: REST API for any Postgres database
|
synopsis: REST API for any Postgres database
|
||||||
license: MIT
|
license: MIT
|
||||||
license-file: LICENSE
|
license-file: LICENSE
|
||||||
author: Joe Nelson, Adam Baker
|
author: Joe Nelson, Adam Baker
|
||||||
homepage: https://github.com/PostgREST/postgrest
|
homepage: https://postgrest.org
|
||||||
maintainer: cred+github@begriffs.com
|
maintainer: Steve Chávez <stevechavezast@gmail.com>
|
||||||
category: Web
|
bug-reports: https://github.com/PostgREST/postgrest/issues
|
||||||
|
category: Executable, PostgreSQL, Network APIs
|
||||||
|
extra-source-files: CHANGELOG.md
|
||||||
build-type: Simple
|
build-type: Simple
|
||||||
cabal-version: >=1.10
|
cabal-version: >=1.10
|
||||||
source-repository head
|
source-repository head
|
||||||
@@ -30,11 +32,11 @@ executable postgrest
|
|||||||
"-with-rtsopts=-N -I2"
|
"-with-rtsopts=-N -I2"
|
||||||
default-language: Haskell2010
|
default-language: Haskell2010
|
||||||
build-depends: auto-update
|
build-depends: auto-update
|
||||||
, base
|
, base >= 4.8 && < 4.10
|
||||||
, hasql
|
, hasql == 1.1
|
||||||
, hasql-pool
|
, hasql-pool == 0.4.3
|
||||||
, postgrest
|
, postgrest
|
||||||
, protolude
|
, protolude == 0.2.2
|
||||||
, text
|
, text
|
||||||
, time
|
, time
|
||||||
, warp
|
, warp
|
||||||
@@ -51,7 +53,7 @@ library
|
|||||||
default-extensions: OverloadedStrings, QuasiQuotes, NoImplicitPrelude
|
default-extensions: OverloadedStrings, QuasiQuotes, NoImplicitPrelude
|
||||||
build-depends: aeson
|
build-depends: aeson
|
||||||
, ansi-wl-pprint
|
, ansi-wl-pprint
|
||||||
, base >= 4.8 && < 6
|
, base >= 4.8 && < 4.10
|
||||||
, base64-bytestring
|
, base64-bytestring
|
||||||
, bytestring
|
, bytestring
|
||||||
, case-insensitive
|
, case-insensitive
|
||||||
@@ -62,24 +64,23 @@ library
|
|||||||
, contravariant-extras
|
, contravariant-extras
|
||||||
, either
|
, either
|
||||||
, gitrev
|
, gitrev
|
||||||
, hasql
|
, hasql == 1.1
|
||||||
, hasql-pool
|
, hasql-pool == 0.4.3
|
||||||
, hasql-transaction
|
, hasql-transaction == 0.5.2
|
||||||
, heredoc
|
, heredoc
|
||||||
, HTTP
|
, HTTP
|
||||||
, http-types
|
, http-types
|
||||||
, insert-ordered-containers
|
, insert-ordered-containers
|
||||||
, interpolatedstring-perl6
|
, interpolatedstring-perl6
|
||||||
, jose
|
, jose == 0.7.0.0
|
||||||
, lens
|
, lens
|
||||||
, lens-aeson
|
, lens-aeson
|
||||||
, network-uri
|
, network-uri
|
||||||
, optparse-applicative >= 0.13 && < 0.15
|
, optparse-applicative >= 0.13 && < 0.15
|
||||||
, parsec
|
, parsec
|
||||||
, protolude >= 0.2
|
, protolude == 0.2.2
|
||||||
, Ranged-sets == 0.3.0
|
, Ranged-sets == 0.3.0
|
||||||
, regex-tdfa
|
, regex-tdfa
|
||||||
, safe
|
|
||||||
, scientific
|
, scientific
|
||||||
, swagger2
|
, swagger2
|
||||||
, text
|
, text
|
||||||
@@ -123,7 +124,9 @@ Test-Suite spec
|
|||||||
, Feature.CorsSpec
|
, Feature.CorsSpec
|
||||||
, Feature.DeleteSpec
|
, Feature.DeleteSpec
|
||||||
, Feature.InsertSpec
|
, Feature.InsertSpec
|
||||||
|
, Feature.JsonOperatorSpec
|
||||||
, Feature.NoJwtSpec
|
, Feature.NoJwtSpec
|
||||||
|
, Feature.PgVersion95Spec
|
||||||
, Feature.PgVersion96Spec
|
, Feature.PgVersion96Spec
|
||||||
, Feature.ProxySpec
|
, Feature.ProxySpec
|
||||||
, Feature.QueryLimitedSpec
|
, Feature.QueryLimitedSpec
|
||||||
@@ -142,15 +145,15 @@ Test-Suite spec
|
|||||||
, aeson-qq
|
, aeson-qq
|
||||||
, async
|
, async
|
||||||
, auto-update
|
, auto-update
|
||||||
, base
|
, base >= 4.8 && < 4.10
|
||||||
, bytestring
|
, bytestring
|
||||||
, base64-bytestring
|
, base64-bytestring
|
||||||
, case-insensitive
|
, case-insensitive
|
||||||
, cassava
|
, cassava
|
||||||
, containers
|
, containers
|
||||||
, contravariant
|
, contravariant
|
||||||
, hasql
|
, hasql == 1.1
|
||||||
, hasql-pool
|
, hasql-pool == 0.4.3
|
||||||
, heredoc
|
, heredoc
|
||||||
, hjsonschema == 1.5.0.1
|
, hjsonschema == 1.5.0.1
|
||||||
, hspec
|
, hspec
|
||||||
@@ -162,7 +165,7 @@ Test-Suite spec
|
|||||||
, monad-control
|
, monad-control
|
||||||
, postgrest
|
, postgrest
|
||||||
, process
|
, process
|
||||||
, protolude
|
, protolude == 0.2.2
|
||||||
, regex-tdfa
|
, regex-tdfa
|
||||||
, time
|
, time
|
||||||
, transformers-base
|
, transformers-base
|
||||||
|
|||||||
@@ -105,7 +105,7 @@ userApiRequest :: Schema -> Request -> RequestBody -> Either ApiRequestError Api
|
|||||||
userApiRequest schema req reqBody
|
userApiRequest schema req reqBody
|
||||||
| isTargetingProc && method `notElem` ["GET", "POST"] = Left ActionInappropriate
|
| isTargetingProc && method `notElem` ["GET", "POST"] = Left ActionInappropriate
|
||||||
| topLevelRange == emptyRange = Left InvalidRange
|
| topLevelRange == emptyRange = Left InvalidRange
|
||||||
| shouldParsePayload && isLeft payload = either (Left . InvalidBody . toS) undefined payload
|
| shouldParsePayload && isLeft payload = either (Left . InvalidBody . toS) witness payload
|
||||||
| otherwise = Right ApiRequest {
|
| otherwise = Right ApiRequest {
|
||||||
iAction = action
|
iAction = action
|
||||||
, iTarget = target
|
, iTarget = target
|
||||||
|
|||||||
@@ -61,7 +61,6 @@ import PostgREST.OpenAPI
|
|||||||
|
|
||||||
import Data.Function (id)
|
import Data.Function (id)
|
||||||
import Protolude hiding (intercalate, Proxy)
|
import Protolude hiding (intercalate, Proxy)
|
||||||
import Safe (headMay)
|
|
||||||
|
|
||||||
postgrest :: AppConfig -> IORef (Maybe DbStructure) -> P.Pool -> IO UTCTime -> IO () -> Application
|
postgrest :: AppConfig -> IORef (Maybe DbStructure) -> P.Pool -> IO UTCTime -> IO () -> Application
|
||||||
postgrest conf refDbStructure pool getTime worker =
|
postgrest conf refDbStructure pool getTime worker =
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ module PostgREST.Auth (
|
|||||||
) where
|
) where
|
||||||
|
|
||||||
import Control.Lens.Operators
|
import Control.Lens.Operators
|
||||||
|
import Control.Lens (set)
|
||||||
import qualified Data.Aeson as JSON
|
import qualified Data.Aeson as JSON
|
||||||
import qualified Data.HashMap.Strict as M
|
import qualified Data.HashMap.Strict as M
|
||||||
import Data.Time.Clock (UTCTime)
|
import Data.Time.Clock (UTCTime)
|
||||||
@@ -48,7 +49,7 @@ jwtClaims secret audience payload time jspath =
|
|||||||
case secret of
|
case secret of
|
||||||
Nothing -> return JWTMissingSecret
|
Nothing -> return JWTMissingSecret
|
||||||
Just s -> do
|
Just s -> do
|
||||||
let validation = defaultJWTValidationSettings (maybe (const True) (==) audience)
|
let validation = set allowedSkew 1 $ defaultJWTValidationSettings (maybe (const True) (==) audience)
|
||||||
eJwt <- runExceptT $ do
|
eJwt <- runExceptT $ do
|
||||||
jwt <- decodeCompact payload
|
jwt <- decodeCompact payload
|
||||||
verifyClaimsAt validation s time jwt
|
verifyClaimsAt validation s time jwt
|
||||||
|
|||||||
+7
-10
@@ -132,7 +132,7 @@ readOptions = do
|
|||||||
<*> C.key "db-anon-role"
|
<*> C.key "db-anon-role"
|
||||||
<*> (mfilter (/= "") <$> C.key "server-proxy-uri")
|
<*> (mfilter (/= "") <$> C.key "server-proxy-uri")
|
||||||
<*> C.key "db-schema"
|
<*> C.key "db-schema"
|
||||||
<*> (fromMaybe "*4" . mfilter (/= "") <$> C.key "server-host")
|
<*> (fromMaybe "127.0.0.1" . mfilter (/= "") <$> C.key "server-host")
|
||||||
<*> (fromMaybe 3000 . join . fmap coerceInt <$> C.key "server-port")
|
<*> (fromMaybe 3000 . join . fmap coerceInt <$> C.key "server-port")
|
||||||
<*> (fmap encodeUtf8 . mfilter (/= "") <$> C.key "jwt-secret")
|
<*> (fmap encodeUtf8 . mfilter (/= "") <$> C.key "jwt-secret")
|
||||||
<*> (fromMaybe False . join . fmap coerceBool <$> C.key "secret-is-base64")
|
<*> (fromMaybe False . join . fmap coerceBool <$> C.key "secret-is-base64")
|
||||||
@@ -141,7 +141,7 @@ readOptions = do
|
|||||||
<*> (join . fmap coerceInt <$> C.key "max-rows")
|
<*> (join . fmap coerceInt <$> C.key "max-rows")
|
||||||
<*> (mfilter (/= "") <$> C.key "pre-request")
|
<*> (mfilter (/= "") <$> C.key "pre-request")
|
||||||
<*> pure False
|
<*> pure False
|
||||||
<*> (fmap parsedPairToTextPair <$> C.subassocs "app.settings")
|
<*> (fmap (fmap coerceText) <$> C.subassocs "app.settings")
|
||||||
<*> (maybe (Right [JSPKey "role"]) parseRoleClaimKey <$> C.key "role-claim-key")
|
<*> (maybe (Right [JSPKey "role"]) parseRoleClaimKey <$> C.key "role-claim-key")
|
||||||
|
|
||||||
case mAppConf of
|
case mAppConf of
|
||||||
@@ -152,13 +152,6 @@ readOptions = do
|
|||||||
return appConf
|
return appConf
|
||||||
|
|
||||||
where
|
where
|
||||||
parsedPairToTextPair :: (Name, Value) -> (Text, Text)
|
|
||||||
parsedPairToTextPair (k, v) = (k, newValue)
|
|
||||||
where
|
|
||||||
newValue = case v of
|
|
||||||
String textVal -> textVal
|
|
||||||
_ -> show v
|
|
||||||
|
|
||||||
parseJwtAudience :: Name -> C.ConfigParserM (Maybe StringOrURI)
|
parseJwtAudience :: Name -> C.ConfigParserM (Maybe StringOrURI)
|
||||||
parseJwtAudience k =
|
parseJwtAudience k =
|
||||||
C.key k >>= \case
|
C.key k >>= \case
|
||||||
@@ -168,6 +161,10 @@ readOptions = do
|
|||||||
(Just "") -> pure Nothing
|
(Just "") -> pure Nothing
|
||||||
aud' -> pure aud'
|
aud' -> pure aud'
|
||||||
|
|
||||||
|
coerceText :: Value -> Text
|
||||||
|
coerceText (String s) = s
|
||||||
|
coerceText v = show v
|
||||||
|
|
||||||
coerceInt :: (Read i, Integral i) => Value -> Maybe i
|
coerceInt :: (Read i, Integral i) => Value -> Maybe i
|
||||||
coerceInt (Number x) = rightToMaybe $ floatingOrInteger x
|
coerceInt (Number x) = rightToMaybe $ floatingOrInteger x
|
||||||
coerceInt (String x) = readMaybe $ toS x
|
coerceInt (String x) = readMaybe $ toS x
|
||||||
@@ -209,7 +206,7 @@ readOptions = do
|
|||||||
|db-anon-role = "postgres"
|
|db-anon-role = "postgres"
|
||||||
|db-pool = 10
|
|db-pool = 10
|
||||||
|
|
|
|
||||||
|server-host = "*4"
|
|server-host = "127.0.0.1"
|
||||||
|server-port = 3000
|
|server-port = 3000
|
||||||
|
|
|
|
||||||
|## base url for swagger output
|
|## base url for swagger output
|
||||||
|
|||||||
@@ -231,7 +231,7 @@ getJoinConditions (Relation Table{tableSchema=tSchema, tableName=tN} cols Table{
|
|||||||
| typ == Many ->
|
| typ == Many ->
|
||||||
let ltN = fromMaybe "" (tableName <$> lt) in
|
let ltN = fromMaybe "" (tableName <$> lt) in
|
||||||
zipWith (toJoinCondition tN ltN) cols (fromMaybe [] lc1) ++ zipWith (toJoinCondition ftN ltN) fcs (fromMaybe [] lc2)
|
zipWith (toJoinCondition tN ltN) cols (fromMaybe [] lc1) ++ zipWith (toJoinCondition ftN ltN) fcs (fromMaybe [] lc2)
|
||||||
| typ == Root -> undefined
|
| typ == Root -> witness
|
||||||
where
|
where
|
||||||
toJoinCondition :: Text -> Text -> Column -> Column -> JoinCondition
|
toJoinCondition :: Text -> Text -> Column -> Column -> JoinCondition
|
||||||
toJoinCondition tb ftb c fc =
|
toJoinCondition tb ftb c fc =
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ module PostgREST.DbStructure (
|
|||||||
, accessibleProcs
|
, accessibleProcs
|
||||||
, schemaDescription
|
, schemaDescription
|
||||||
, getPgVersion
|
, getPgVersion
|
||||||
, fillSessionWithSettings
|
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import qualified Hasql.Decoders as HD
|
import qualified Hasql.Decoders as HD
|
||||||
@@ -33,14 +32,11 @@ import GHC.Exts (groupWith)
|
|||||||
import Protolude
|
import Protolude
|
||||||
import Unsafe (unsafeHead)
|
import Unsafe (unsafeHead)
|
||||||
|
|
||||||
import Data.Functor.Contravariant (contramap)
|
|
||||||
import Contravariant.Extras (contrazip2)
|
|
||||||
|
|
||||||
getDbStructure :: Schema -> PgVersion -> H.Session DbStructure
|
getDbStructure :: Schema -> PgVersion -> H.Session DbStructure
|
||||||
getDbStructure schema pgVer = do
|
getDbStructure schema pgVer = do
|
||||||
tabs <- H.query () allTables
|
tabs <- H.query () allTables
|
||||||
cols <- H.query schema $ allColumns tabs
|
cols <- H.query schema $ allColumns tabs
|
||||||
syns <- H.query () $ allSynonyms cols
|
syns <- H.query schema $ allSynonyms cols
|
||||||
childRels <- H.query () $ allChildRelations tabs cols
|
childRels <- H.query () $ allChildRelations tabs cols
|
||||||
keys <- H.query () $ allPrimaryKeys tabs
|
keys <- H.query () $ allPrimaryKeys tabs
|
||||||
procs <- H.query schema allProcs
|
procs <- H.query schema allProcs
|
||||||
@@ -388,7 +384,7 @@ allColumns tabs =
|
|||||||
pg_catalog.pg_namespace n
|
pg_catalog.pg_namespace n
|
||||||
WHERE
|
WHERE
|
||||||
r.contype IN ('f', 'p')
|
r.contype IN ('f', 'p')
|
||||||
AND c.relkind IN ('r', 'v', 'f', 'mv')
|
AND c.relkind IN ('r', 'v', 'f', 'm')
|
||||||
AND r.conrelid = c.oid
|
AND r.conrelid = c.oid
|
||||||
AND c.relnamespace = n.oid
|
AND c.relnamespace = n.oid
|
||||||
AND n.nspname NOT IN ('pg_catalog', 'information_schema', $1)
|
AND n.nspname NOT IN ('pg_catalog', 'information_schema', $1)
|
||||||
@@ -492,7 +488,7 @@ allColumns tabs =
|
|||||||
NOT pg_is_other_temp_schema(nc.oid)
|
NOT pg_is_other_temp_schema(nc.oid)
|
||||||
AND a.attnum > 0
|
AND a.attnum > 0
|
||||||
AND NOT a.attisdropped
|
AND NOT a.attisdropped
|
||||||
AND (c.relkind = ANY (ARRAY['r'::"char", 'v'::"char", 'f'::"char"]))
|
AND (c.relkind = ANY (ARRAY['r'::"char", 'v'::"char", 'f'::"char", 'm'::"char"]))
|
||||||
AND (nc.nspname = $1 OR kc.r_oid IS NOT NULL) /*--filter only columns that are FK/PK or in the api schema */
|
AND (nc.nspname = $1 OR kc.r_oid IS NOT NULL) /*--filter only columns that are FK/PK or in the api schema */
|
||||||
/*--AND (pg_has_role(c.relowner, 'USAGE'::text) OR has_column_privilege(c.oid, a.attnum, 'SELECT, INSERT, UPDATE, REFERENCES'::text))*/
|
/*--AND (pg_has_role(c.relowner, 'USAGE'::text) OR has_column_privilege(c.oid, a.attnum, 'SELECT, INSERT, UPDATE, REFERENCES'::text))*/
|
||||||
)
|
)
|
||||||
@@ -689,79 +685,67 @@ pkFromRow :: [Table] -> (Schema, Text, Text) -> Maybe PrimaryKey
|
|||||||
pkFromRow tabs (s, t, n) = PrimaryKey <$> table <*> pure n
|
pkFromRow tabs (s, t, n) = PrimaryKey <$> table <*> pure n
|
||||||
where table = find (\tbl -> tableSchema tbl == s && tableName tbl == t) tabs
|
where table = find (\tbl -> tableSchema tbl == s && tableName tbl == t) tabs
|
||||||
|
|
||||||
allSynonyms :: [Column] -> H.Query () [Synonym]
|
allSynonyms :: [Column] -> H.Query Schema [Synonym]
|
||||||
allSynonyms cols =
|
allSynonyms cols =
|
||||||
H.statement sql HE.unit (decodeSynonyms cols) True
|
H.statement sql (HE.value HE.text) (decodeSynonyms cols) True
|
||||||
where
|
-- query explanation at https://gist.github.com/steve-chavez/7ee0e6590cddafb532e5f00c46275569
|
||||||
-- query explanation at https://gist.github.com/ruslantalpa/2eab8c930a65e8043d8f
|
where sql = [q|
|
||||||
sql = [q|
|
with
|
||||||
with view_columns as (
|
views as (
|
||||||
select
|
select
|
||||||
c.oid as view_oid,
|
n.nspname as view_schema,
|
||||||
a.attname::information_schema.sql_identifier as column_name
|
c.relname as view_name,
|
||||||
from pg_attribute a
|
r.ev_action as view_definition
|
||||||
join pg_class c on a.attrelid = c.oid
|
from pg_class c
|
||||||
join pg_namespace nc on c.relnamespace = nc.oid
|
join pg_namespace n on n.oid = c.relnamespace
|
||||||
where
|
join pg_rewrite r on r.ev_class = c.oid
|
||||||
not pg_is_other_temp_schema(nc.oid)
|
where (c.relkind = 'v'::char) and n.nspname = $1
|
||||||
and a.attnum > 0
|
|
||||||
and not a.attisdropped
|
|
||||||
and (c.relkind = 'v'::"char")
|
|
||||||
and nc.nspname not in ('information_schema', 'pg_catalog')
|
|
||||||
),
|
),
|
||||||
view_column_usage as (
|
removed_subselects as(
|
||||||
select distinct
|
select
|
||||||
v.oid as view_oid,
|
view_schema, view_name,
|
||||||
nv.nspname::information_schema.sql_identifier as view_schema,
|
regexp_replace(view_definition, ':subselect {.*?:constraintDeps <>} :location', '', 'g') as x
|
||||||
v.relname::information_schema.sql_identifier as view_name,
|
from views
|
||||||
nt.nspname::information_schema.sql_identifier as table_schema,
|
|
||||||
t.relname::information_schema.sql_identifier as table_name,
|
|
||||||
a.attname::information_schema.sql_identifier as column_name,
|
|
||||||
pg_get_viewdef(v.oid)::information_schema.character_data as view_definition
|
|
||||||
from pg_namespace nv
|
|
||||||
join pg_class v on nv.oid = v.relnamespace
|
|
||||||
join pg_depend dv on v.oid = dv.refobjid
|
|
||||||
join pg_depend dt on dv.objid = dt.objid
|
|
||||||
join pg_class t on dt.refobjid = t.oid
|
|
||||||
join pg_namespace nt on t.relnamespace = nt.oid
|
|
||||||
join pg_attribute a on t.oid = a.attrelid and dt.refobjsubid = a.attnum
|
|
||||||
|
|
||||||
where
|
|
||||||
nv.nspname not in ('information_schema', 'pg_catalog')
|
|
||||||
and v.relkind = 'v'::"char"
|
|
||||||
and dv.refclassid = 'pg_class'::regclass::oid
|
|
||||||
and dv.classid = 'pg_rewrite'::regclass::oid
|
|
||||||
and dv.deptype = 'i'::"char"
|
|
||||||
and dv.refobjid <> dt.refobjid
|
|
||||||
and dt.classid = 'pg_rewrite'::regclass::oid
|
|
||||||
and dt.refclassid = 'pg_class'::regclass::oid
|
|
||||||
and (t.relkind = any (array['r'::"char", 'v'::"char", 'f'::"char"]))
|
|
||||||
),
|
),
|
||||||
candidates as (
|
target_lists as(
|
||||||
select
|
select
|
||||||
vcu.*,
|
view_schema, view_name,
|
||||||
(
|
regexp_split_to_array(x, 'targetList') as x
|
||||||
select case when match is not null then coalesce(match[8], match[7], match[4]) end
|
from removed_subselects
|
||||||
from regexp_matches(
|
),
|
||||||
CONCAT('SELECT ', SPLIT_PART(vcu.view_definition, 'SELECT', 2)),
|
last_target_list_wo_tail as(
|
||||||
CONCAT('SELECT.*?((',vcu.table_name,')|(\w+))\.(', vcu.column_name, ')(\s+AS\s+("([^"]+)"|([^, \n\t]+)))?.*?FROM.*?(',vcu.table_schema,'\.|)(\2|',vcu.table_name,'\s+(as\s)?\3)'),
|
select
|
||||||
'nsi'
|
view_schema, view_name,
|
||||||
) match
|
(regexp_split_to_array(x[array_upper(x, 1)], ':onConflict'))[1] as x
|
||||||
) as view_column_name
|
from target_lists
|
||||||
from view_column_usage as vcu
|
),
|
||||||
|
target_entries as(
|
||||||
|
select
|
||||||
|
view_schema, view_name,
|
||||||
|
unnest(regexp_split_to_array(x, 'TARGETENTRY')) as entry
|
||||||
|
from last_target_list_wo_tail
|
||||||
|
),
|
||||||
|
results as(
|
||||||
|
select
|
||||||
|
view_schema, view_name,
|
||||||
|
substring(entry from ':resname (.*?) :') as view_colum_name,
|
||||||
|
substring(entry from ':resorigtbl (.*?) :') as resorigtbl,
|
||||||
|
substring(entry from ':resorigcol (.*?) :') as resorigcol
|
||||||
|
from target_entries
|
||||||
)
|
)
|
||||||
select
|
select
|
||||||
c.table_schema,
|
sch.nspname as table_schema,
|
||||||
c.table_name,
|
tbl.relname as table_name,
|
||||||
c.column_name as table_column_name,
|
col.attname as table_column_name,
|
||||||
c.view_schema,
|
res.view_schema,
|
||||||
c.view_name,
|
res.view_name,
|
||||||
c.view_column_name
|
res.view_colum_name
|
||||||
from view_columns as vc, candidates as c
|
from results res
|
||||||
where
|
join pg_class tbl on tbl.oid::text = res.resorigtbl
|
||||||
vc.view_oid = c.view_oid
|
join pg_attribute col on col.attrelid = tbl.oid and col.attnum::text = res.resorigcol
|
||||||
and vc.column_name = c.view_column_name
|
join pg_namespace sch on sch.oid = tbl.relnamespace
|
||||||
order by c.view_schema, c.view_name, c.table_name, c.view_column_name
|
where resorigtbl <> '0'
|
||||||
|
order by view_schema, view_name, view_colum_name;
|
||||||
|]
|
|]
|
||||||
|
|
||||||
synonymFromRow :: [Column] -> (Text,Text,Text,Text,Text,Text) -> Maybe Synonym
|
synonymFromRow :: [Column] -> (Text,Text,Text,Text,Text,Text) -> Maybe Synonym
|
||||||
@@ -776,17 +760,3 @@ getPgVersion = H.query () $ H.statement sql HE.unit versionRow False
|
|||||||
where
|
where
|
||||||
sql = "SELECT current_setting('server_version_num')::integer, current_setting('server_version')"
|
sql = "SELECT current_setting('server_version_num')::integer, current_setting('server_version')"
|
||||||
versionRow = HD.singleRow $ PgVersion <$> HD.value HD.int4 <*> HD.value HD.text
|
versionRow = HD.singleRow $ PgVersion <$> HD.value HD.int4 <*> HD.value HD.text
|
||||||
|
|
||||||
fillSessionWithSettings :: [(Text, Text)] -> H.Session ()
|
|
||||||
fillSessionWithSettings settings =
|
|
||||||
-- Send all of the config settings to the set_config function, using pgsql's `unnest` to transform arrays of values
|
|
||||||
H.query settings $ H.statement "SELECT set_config(k, v, false) FROM unnest($1, $2) AS f1(k, v)" encoder HD.unit False
|
|
||||||
|
|
||||||
where
|
|
||||||
-- Take a list of (key, value) pairs and encode each as an array to later bind to the query
|
|
||||||
-- see Insert Many section at https://hackage.haskell.org/package/hasql-1.1.1/docs/Hasql-Encoders.html
|
|
||||||
encoder = contramap L.unzip $ contrazip2 (vector HE.text) (vector HE.text)
|
|
||||||
where
|
|
||||||
vector value =
|
|
||||||
HE.value $ HE.array $ HE.arrayDimension foldl' $ HE.arrayValue value
|
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ import PostgREST.ApiRequest (ApiRequest(..))
|
|||||||
import PostgREST.Auth (JWTAttempt(..))
|
import PostgREST.Auth (JWTAttempt(..))
|
||||||
import PostgREST.Config (AppConfig (..), corsPolicy)
|
import PostgREST.Config (AppConfig (..), corsPolicy)
|
||||||
import PostgREST.Error (simpleError)
|
import PostgREST.Error (simpleError)
|
||||||
import PostgREST.QueryBuilder (pgFmtLit, unquoted, pgFmtEnvVar)
|
import PostgREST.QueryBuilder (pgFmtLit, unquoted, pgFmtSetLocal)
|
||||||
|
|
||||||
import Protolude hiding (concat, null)
|
import Protolude hiding (concat, null)
|
||||||
|
|
||||||
@@ -32,13 +32,14 @@ runWithClaims conf eClaims app req =
|
|||||||
JWTInvalid e -> return $ unauthed $ show e
|
JWTInvalid e -> return $ unauthed $ show e
|
||||||
JWTMissingSecret -> return $ simpleError status500 [] "Server lacks JWT secret"
|
JWTMissingSecret -> return $ simpleError status500 [] "Server lacks JWT secret"
|
||||||
JWTClaims claims -> do
|
JWTClaims claims -> do
|
||||||
H.sql $ toS.mconcat $ setSchemaSql ++ setRoleSql ++ claimsSql ++ headersSql ++ cookiesSql
|
H.sql $ toS.mconcat $ setSchemaSql ++ setRoleSql ++ claimsSql ++ headersSql ++ cookiesSql ++ appSettingsSql
|
||||||
mapM_ H.sql customReqCheck
|
mapM_ H.sql customReqCheck
|
||||||
app req
|
app req
|
||||||
where
|
where
|
||||||
headersSql = map (pgFmtEnvVar "request.header.") $ iHeaders req
|
headersSql = pgFmtSetLocal "request.header." <$> iHeaders req
|
||||||
cookiesSql = map (pgFmtEnvVar "request.cookie.") $ iCookies req
|
cookiesSql = pgFmtSetLocal "request.cookie." <$> iCookies req
|
||||||
claimsSql = map (pgFmtEnvVar "request.jwt.claim.") [(c,unquoted v) | (c,v) <- M.toList claimsWithRole]
|
claimsSql = pgFmtSetLocal "request.jwt.claim." <$> [(c,unquoted v) | (c,v) <- M.toList claimsWithRole]
|
||||||
|
appSettingsSql = pgFmtSetLocal mempty <$> configSettings conf
|
||||||
setRoleSql = maybeToList $
|
setRoleSql = maybeToList $
|
||||||
(\r -> "set local role " <> r <> ";") . toS . pgFmtLit . unquoted <$> M.lookup "role" claimsWithRole
|
(\r -> "set local role " <> r <> ";") . toS . pgFmtLit . unquoted <$> M.lookup "role" claimsWithRole
|
||||||
setSchemaSql = ["set schema " <> pgFmtLit (configSchema conf) <> ";"] :: [Text]
|
setSchemaSql = ["set schema " <> pgFmtLit (configSchema conf) <> ";"] :: [Text]
|
||||||
|
|||||||
@@ -42,7 +42,8 @@ makeTableDef pks (t, cs, _) =
|
|||||||
(tn, (mempty :: Schema)
|
(tn, (mempty :: Schema)
|
||||||
& description .~ tableDescription t
|
& description .~ tableDescription t
|
||||||
& type_ .~ SwaggerObject
|
& type_ .~ SwaggerObject
|
||||||
& properties .~ fromList (map (makeProperty pks) cs))
|
& properties .~ fromList (map (makeProperty pks) cs)
|
||||||
|
& required .~ map colName (filter (not . colNullable) cs))
|
||||||
|
|
||||||
makeProperty :: [PrimaryKey] -> Column -> (Text, Referenced Schema)
|
makeProperty :: [PrimaryKey] -> Column -> (Text, Referenced Schema)
|
||||||
makeProperty pks c = (colName c, Inline s)
|
makeProperty pks c = (colName c, Inline s)
|
||||||
@@ -197,8 +198,11 @@ makePathItem (t, cs, _) = ("/" ++ unpack tn, p $ tableInsertable t)
|
|||||||
& at 206 ?~ "Partial Content"
|
& at 206 ?~ "Partial Content"
|
||||||
& at 200 ?~ Inline ((mempty :: Response)
|
& at 200 ?~ Inline ((mempty :: Response)
|
||||||
& description .~ "OK"
|
& description .~ "OK"
|
||||||
& schema ?~ (Ref $ Reference $ tableName t)
|
& schema ?~ Inline (mempty
|
||||||
|
& type_ .~ SwaggerArray
|
||||||
|
& items ?~ (SwaggerItemsObject $ Ref $ Reference $ tableName t)
|
||||||
)
|
)
|
||||||
|
)
|
||||||
postOp = tOp
|
postOp = tOp
|
||||||
& parameters .~ map ref ["body." <> tn, "preferReturn"]
|
& parameters .~ map ref ["body." <> tn, "preferReturn"]
|
||||||
& at 201 ?~ "Created"
|
& at 201 ?~ "Created"
|
||||||
@@ -219,8 +223,13 @@ makePathItem (t, cs, _) = ("/" ++ unpack tn, p $ tableInsertable t)
|
|||||||
makeProcPathItem :: ProcDescription -> (FilePath, PathItem)
|
makeProcPathItem :: ProcDescription -> (FilePath, PathItem)
|
||||||
makeProcPathItem pd = ("/rpc/" ++ toS (pdName pd), pe)
|
makeProcPathItem pd = ("/rpc/" ++ toS (pdName pd), pe)
|
||||||
where
|
where
|
||||||
|
-- Use first line of proc description as summary; rest as description (if present)
|
||||||
|
-- We strip leading newlines from description so that users can include a blank line between summary and description
|
||||||
|
(pSum, pDesc) = fmap fst &&& fmap (dropWhile (=='\n') . snd) $
|
||||||
|
breakOn "\n" <$> pdDescription pd
|
||||||
postOp = (mempty :: Operation)
|
postOp = (mempty :: Operation)
|
||||||
& description .~ pdDescription pd
|
& summary .~ pSum
|
||||||
|
& description .~ mfilter (/="") pDesc
|
||||||
& parameters .~ makeProcParam pd
|
& parameters .~ makeProcParam pd
|
||||||
& tags .~ Set.fromList ["(rpc) " <> pdName pd]
|
& tags .~ Set.fromList ["(rpc) " <> pdName pd]
|
||||||
& produces ?~ makeMimeList [CTApplicationJSON, CTSingularJSON]
|
& produces ?~ makeMimeList [CTApplicationJSON, CTSingularJSON]
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ lexeme p = ws *> p <* ws
|
|||||||
pTreePath :: Parser (EmbedPath, Field)
|
pTreePath :: Parser (EmbedPath, Field)
|
||||||
pTreePath = do
|
pTreePath = do
|
||||||
p <- pFieldName `sepBy1` pDelimiter
|
p <- pFieldName `sepBy1` pDelimiter
|
||||||
jp <- optionMaybe pJsonPath
|
jp <- option [] pJsonPath
|
||||||
return (init p, (last p, jp))
|
return (init p, (last p, jp))
|
||||||
|
|
||||||
pFieldForest :: Parser [Tree SelectItem]
|
pFieldForest :: Parser [Tree SelectItem]
|
||||||
@@ -80,14 +80,26 @@ pFieldName = do
|
|||||||
dash :: Parser Char
|
dash :: Parser Char
|
||||||
dash = isDash *> pure '-'
|
dash = isDash *> pure '-'
|
||||||
|
|
||||||
pJsonPathStep :: Parser Text
|
pJsonPath :: Parser JsonPath
|
||||||
pJsonPathStep = toS <$> try (string "->" *> pFieldName)
|
pJsonPath = many pJsonOperation
|
||||||
|
where
|
||||||
|
pJsonOperation :: Parser JsonOperation
|
||||||
|
pJsonOperation = pJsonArrow <*> pJsonOperand
|
||||||
|
|
||||||
pJsonPath :: Parser [Text]
|
pJsonArrow =
|
||||||
pJsonPath = (<>) <$> many pJsonPathStep <*> ( (:[]) <$> (string "->>" *> pFieldName) )
|
try (string "->>" $> J2Arrow) <|>
|
||||||
|
try (string "->" $> JArrow)
|
||||||
|
|
||||||
|
pJsonOperand =
|
||||||
|
let pJKey = JKey . toS <$> pFieldName
|
||||||
|
pJIdx = JIdx . toS <$> ((:) <$> option '+' (char '-') <*> many1 digit) <* pEnd
|
||||||
|
pEnd = try (void $ lookAhead (string "->")) <|>
|
||||||
|
try (void $ lookAhead (string "::")) <|>
|
||||||
|
try eof in
|
||||||
|
try pJIdx <|> try pJKey
|
||||||
|
|
||||||
pField :: Parser Field
|
pField :: Parser Field
|
||||||
pField = lexeme $ (,) <$> pFieldName <*> optionMaybe pJsonPath
|
pField = lexeme $ (,) <$> pFieldName <*> option [] pJsonPath
|
||||||
|
|
||||||
aliasSeparator :: Parser ()
|
aliasSeparator :: Parser ()
|
||||||
aliasSeparator = char ':' >> notFollowedBy (char ':')
|
aliasSeparator = char ':' >> notFollowedBy (char ':')
|
||||||
@@ -112,7 +124,7 @@ pFieldSelect = lexeme $
|
|||||||
)
|
)
|
||||||
<|> do
|
<|> do
|
||||||
s <- pStar
|
s <- pStar
|
||||||
return ((s, Nothing), Nothing, Nothing, Nothing)
|
return ((s, []), Nothing, Nothing, Nothing)
|
||||||
|
|
||||||
pOpExpr :: Parser SingleVal -> Parser OpExpr
|
pOpExpr :: Parser SingleVal -> Parser OpExpr
|
||||||
pOpExpr pSVal = try ( string "not" *> pDelimiter *> (OpExpr True <$> pOperation)) <|> OpExpr False <$> pOperation
|
pOpExpr pSVal = try ( string "not" *> pDelimiter *> (OpExpr True <$> pOperation)) <|> OpExpr False <$> pOperation
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
{-# LANGUAGE FlexibleInstances #-}
|
{-# LANGUAGE FlexibleInstances #-}
|
||||||
{-# LANGUAGE FlexibleContexts #-}
|
{-# LANGUAGE FlexibleContexts #-}
|
||||||
{-# LANGUAGE DuplicateRecordFields #-}
|
{-# LANGUAGE DuplicateRecordFields #-}
|
||||||
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
{-# LANGUAGE LambdaCase #-}
|
||||||
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
||||||
{-|
|
{-|
|
||||||
Module : PostgREST.QueryBuilder
|
Module : PostgREST.QueryBuilder
|
||||||
Description : PostgREST SQL generating functions.
|
Description : PostgREST SQL generating functions.
|
||||||
@@ -22,7 +23,7 @@ module PostgREST.QueryBuilder (
|
|||||||
, requestToCountQuery
|
, requestToCountQuery
|
||||||
, unquoted
|
, unquoted
|
||||||
, ResultsWithCount
|
, ResultsWithCount
|
||||||
, pgFmtEnvVar
|
, pgFmtSetLocal
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import qualified Hasql.Query as H
|
import qualified Hasql.Query as H
|
||||||
@@ -172,9 +173,9 @@ callProc qi pgArgs returnsScalar selectQuery countQuery countTotal isSingle para
|
|||||||
unwords [
|
unwords [
|
||||||
"_args_record AS (",
|
"_args_record AS (",
|
||||||
"SELECT * FROM " <> (if isObject then "json_to_record" else "json_to_recordset") <> "($1)",
|
"SELECT * FROM " <> (if isObject then "json_to_record" else "json_to_recordset") <> "($1)",
|
||||||
"AS _(" <> intercalate ", " ((\a -> pgaName a <> " " <> pgaType a) <$> pgArgs) <> ")",
|
"AS _(" <> intercalate ", " ((\a -> pgFmtIdent (pgaName a) <> " " <> pgaType a) <$> pgArgs) <> ")",
|
||||||
")"]
|
")"]
|
||||||
, intercalate ", " ((\a -> pgaName a <> " := (SELECT " <> pgaName a <> " FROM _args_record)") <$> pgArgs))
|
, intercalate ", " ((\a -> pgFmtIdent (pgaName a) <> " := (SELECT " <> pgFmtIdent (pgaName a) <> " FROM _args_record)") <$> pgArgs))
|
||||||
countResultF = if countTotal then "( "<> countQuery <> ")" else "null::bigint" :: Text
|
countResultF = if countTotal then "( "<> countQuery <> ")" else "null::bigint" :: Text
|
||||||
_procName = qiName qi
|
_procName = qiName qi
|
||||||
responseHeaders =
|
responseHeaders =
|
||||||
@@ -207,7 +208,7 @@ pgFmtLit x =
|
|||||||
else slashed
|
else slashed
|
||||||
|
|
||||||
requestToCountQuery :: Schema -> DbRequest -> SqlQuery
|
requestToCountQuery :: Schema -> DbRequest -> SqlQuery
|
||||||
requestToCountQuery _ (DbMutate _) = undefined
|
requestToCountQuery _ (DbMutate _) = witness
|
||||||
requestToCountQuery schema (DbRead (Node (Select{where_=logicForest}, (mainTbl, _, _, _, _)) _)) =
|
requestToCountQuery schema (DbRead (Node (Select{where_=logicForest}, (mainTbl, _, _, _, _)) _)) =
|
||||||
unwords [
|
unwords [
|
||||||
"SELECT pg_catalog.count(*)",
|
"SELECT pg_catalog.count(*)",
|
||||||
@@ -272,7 +273,7 @@ requestToQuery schema isParent (DbRead (Node (Select colSelects tbls logicForest
|
|||||||
--the following is just to remove the warning
|
--the following is just to remove the warning
|
||||||
--getQueryParts is not total but requestToQuery is called only after addJoinConditions which ensures the only
|
--getQueryParts is not total but requestToQuery is called only after addJoinConditions which ensures the only
|
||||||
--posible relations are Child Parent Many
|
--posible relations are Child Parent Many
|
||||||
getQueryParts _ _ = undefined
|
getQueryParts _ _ = witness
|
||||||
requestToQuery schema _ (DbMutate (Insert mainTbl pkCols p@(PayloadJSON _ pType pKeys) onConflct logicForest returnings)) =
|
requestToQuery schema _ (DbMutate (Insert mainTbl pkCols p@(PayloadJSON _ pType pKeys) onConflct logicForest returnings)) =
|
||||||
unwords [
|
unwords [
|
||||||
("WITH " <> ignoredBody) `emptyOnFalse` not payloadIsEmpty,
|
("WITH " <> ignoredBody) `emptyOnFalse` not payloadIsEmpty,
|
||||||
@@ -288,7 +289,7 @@ requestToQuery schema _ (DbMutate (Insert mainTbl pkCols p@(PayloadJSON _ pType
|
|||||||
-- Only used for PUT
|
-- Only used for PUT
|
||||||
("WHERE " <> intercalate " AND " (pgFmtLogicTree (QualifiedIdentifier "" "_") <$> logicForest)) `emptyOnFalse` null logicForest],
|
("WHERE " <> intercalate " AND " (pgFmtLogicTree (QualifiedIdentifier "" "_") <$> logicForest)) `emptyOnFalse` null logicForest],
|
||||||
maybe "" (\x -> (
|
maybe "" (\x -> (
|
||||||
"ON CONFLICT(" <> intercalate ", " pkCols <> ") " <> case x of
|
"ON CONFLICT(" <> intercalate ", " (pgFmtIdent <$> pkCols) <> ") " <> case x of
|
||||||
IgnoreDuplicates ->
|
IgnoreDuplicates ->
|
||||||
"DO NOTHING"
|
"DO NOTHING"
|
||||||
MergeDuplicates ->
|
MergeDuplicates ->
|
||||||
@@ -394,8 +395,8 @@ pgFmtField :: QualifiedIdentifier -> Field -> SqlFragment
|
|||||||
pgFmtField table (c, jp) = pgFmtColumn table c <> pgFmtJsonPath jp
|
pgFmtField table (c, jp) = pgFmtColumn table c <> pgFmtJsonPath jp
|
||||||
|
|
||||||
pgFmtSelectItem :: QualifiedIdentifier -> SelectItem -> SqlFragment
|
pgFmtSelectItem :: QualifiedIdentifier -> SelectItem -> SqlFragment
|
||||||
pgFmtSelectItem table (f@(_, jp), Nothing, alias, _) = pgFmtField table f <> pgFmtAs jp alias
|
pgFmtSelectItem table (f@(fName, jp), Nothing, alias, _) = pgFmtField table f <> pgFmtAs fName jp alias
|
||||||
pgFmtSelectItem table (f@(_, jp), Just cast, alias, _) = "CAST (" <> pgFmtField table f <> " AS " <> cast <> " )" <> pgFmtAs jp alias
|
pgFmtSelectItem table (f@(fName, jp), Just cast, alias, _) = "CAST (" <> pgFmtField table f <> " AS " <> cast <> " )" <> pgFmtAs fName jp alias
|
||||||
|
|
||||||
pgFmtOrderTerm :: QualifiedIdentifier -> OrderTerm -> SqlFragment
|
pgFmtOrderTerm :: QualifiedIdentifier -> OrderTerm -> SqlFragment
|
||||||
pgFmtOrderTerm qi ot = unwords [
|
pgFmtOrderTerm qi ot = unwords [
|
||||||
@@ -447,20 +448,29 @@ pgFmtLogicTree qi (Expr hasNot op forest) = notOp <> " (" <> intercalate (" " <>
|
|||||||
where notOp = if hasNot then "NOT" else ""
|
where notOp = if hasNot then "NOT" else ""
|
||||||
pgFmtLogicTree qi (Stmnt flt) = pgFmtFilter qi flt
|
pgFmtLogicTree qi (Stmnt flt) = pgFmtFilter qi flt
|
||||||
|
|
||||||
pgFmtJsonPath :: Maybe JsonPath -> SqlFragment
|
pgFmtJsonPath :: JsonPath -> SqlFragment
|
||||||
pgFmtJsonPath (Just [x]) = "->>" <> pgFmtLit x
|
pgFmtJsonPath = \case
|
||||||
pgFmtJsonPath (Just (x:xs)) = "->" <> pgFmtLit x <> pgFmtJsonPath ( Just xs )
|
[] -> ""
|
||||||
pgFmtJsonPath _ = ""
|
(JArrow x:xs) -> "->" <> pgFmtJsonOperand x <> pgFmtJsonPath xs
|
||||||
|
(J2Arrow x:xs) -> "->>" <> pgFmtJsonOperand x <> pgFmtJsonPath xs
|
||||||
|
where
|
||||||
|
pgFmtJsonOperand (JKey k) = pgFmtLit k
|
||||||
|
pgFmtJsonOperand (JIdx i) = pgFmtLit i <> "::int"
|
||||||
|
|
||||||
pgFmtAs :: Maybe JsonPath -> Maybe Alias -> SqlFragment
|
pgFmtAs :: FieldName -> JsonPath -> Maybe Alias -> SqlFragment
|
||||||
pgFmtAs Nothing Nothing = ""
|
pgFmtAs _ [] Nothing = ""
|
||||||
pgFmtAs (Just xx) Nothing = case lastMay xx of
|
pgFmtAs fName jp Nothing = case jOp <$> lastMay jp of
|
||||||
Just alias -> " AS " <> pgFmtIdent alias
|
Just (JKey key) -> " AS " <> pgFmtIdent key
|
||||||
|
Just (JIdx _) -> " AS " <> pgFmtIdent (fromMaybe fName lastKey)
|
||||||
|
-- We get the lastKey because on:
|
||||||
|
-- `select=data->1->mycol->>2`, we need to show the result as [ {"mycol": ..}, {"mycol": ..} ]
|
||||||
|
-- `select=data->3`, we need to show the result as [ {"data": ..}, {"data": ..} ]
|
||||||
|
where lastKey = jVal <$> find (\case JKey{} -> True; _ -> False) (jOp <$> reverse jp)
|
||||||
Nothing -> ""
|
Nothing -> ""
|
||||||
pgFmtAs _ (Just alias) = " AS " <> pgFmtIdent alias
|
pgFmtAs _ _ (Just alias) = " AS " <> pgFmtIdent alias
|
||||||
|
|
||||||
pgFmtEnvVar :: Text -> (Text, Text) -> SqlFragment
|
pgFmtSetLocal :: Text -> (Text, Text) -> SqlFragment
|
||||||
pgFmtEnvVar prefix (k, v) =
|
pgFmtSetLocal prefix (k, v) =
|
||||||
"set local " <> pgFmtIdent (prefix <> k) <> " = " <> pgFmtLit v <> ";"
|
"set local " <> pgFmtIdent (prefix <> k) <> " = " <> pgFmtLit v <> ";"
|
||||||
|
|
||||||
trimNullChars :: Text -> Text
|
trimNullChars :: Text -> Text
|
||||||
|
|||||||
+10
-2
@@ -238,8 +238,16 @@ instance Show LogicOperator where
|
|||||||
data LogicTree = Expr Bool LogicOperator [LogicTree] | Stmnt Filter deriving (Show, Eq)
|
data LogicTree = Expr Bool LogicOperator [LogicTree] | Stmnt Filter deriving (Show, Eq)
|
||||||
|
|
||||||
type FieldName = Text
|
type FieldName = Text
|
||||||
type JsonPath = [Text]
|
{-|
|
||||||
type Field = (FieldName, Maybe JsonPath)
|
Json path operations as specified in https://www.postgresql.org/docs/9.4/static/functions-json.html
|
||||||
|
-}
|
||||||
|
type JsonPath = [JsonOperation]
|
||||||
|
-- | Represents the single arrow `->` or double arrow `->>` operators
|
||||||
|
data JsonOperation = JArrow{jOp :: JsonOperand} | J2Arrow{jOp :: JsonOperand} deriving (Show, Eq)
|
||||||
|
-- | Represents the key(`->'key'`) or index(`->'1`::int`), the index is Text because we reuse our escaping functons and let pg do the casting with '1'::int
|
||||||
|
data JsonOperand = JKey{jVal :: Text} | JIdx{jVal :: Text} deriving (Show, Eq)
|
||||||
|
|
||||||
|
type Field = (FieldName, JsonPath)
|
||||||
type Alias = Text
|
type Alias = Text
|
||||||
type Cast = Text
|
type Cast = Text
|
||||||
type NodeName = Text
|
type NodeName = Text
|
||||||
|
|||||||
+2
-1
@@ -5,11 +5,12 @@ extra-deps:
|
|||||||
- hjsonpointer-1.1.1
|
- hjsonpointer-1.1.1
|
||||||
- hjsonschema-1.5.0.1
|
- hjsonschema-1.5.0.1
|
||||||
- Ranged-sets-0.3.0
|
- Ranged-sets-0.3.0
|
||||||
- protolude-0.2
|
- protolude-0.2.2
|
||||||
- hasql-1.1
|
- hasql-1.1
|
||||||
- hasql-pool-0.4.3
|
- hasql-pool-0.4.3
|
||||||
- hasql-transaction-0.5.2
|
- hasql-transaction-0.5.2
|
||||||
- jose-0.7.0.0
|
- jose-0.7.0.0
|
||||||
|
- postgresql-libpq-0.9.4.1
|
||||||
ghc-options:
|
ghc-options:
|
||||||
postgrest: -O2 -Werror -Wall -fwarn-identities -fno-warn-redundant-constraints
|
postgrest: -O2 -Werror -Wall -fwarn-identities -fno-warn-redundant-constraints
|
||||||
nix:
|
nix:
|
||||||
|
|||||||
@@ -193,10 +193,6 @@ spec =
|
|||||||
it "can query columns that begin with and/or reserved words" $
|
it "can query columns that begin with and/or reserved words" $
|
||||||
get "/grandchild_entities?or=(and_starting_col.eq.smth, or_starting_col.eq.smth)" `shouldRespondWith` 200
|
get "/grandchild_entities?or=(and_starting_col.eq.smth, or_starting_col.eq.smth)" `shouldRespondWith` 200
|
||||||
|
|
||||||
it "can query jsonb columns" $
|
|
||||||
get "/grandchild_entities?or=(jsonb_col->a->>b.eq.foo, jsonb_col->>b.eq.bar)&select=id" `shouldRespondWith`
|
|
||||||
[json|[{id: 4}, {id: 5}]|] { matchStatus = 200, matchHeaders = [matchContentTypeJson] }
|
|
||||||
|
|
||||||
it "fails when using IN without () and provides meaningful error message" $
|
it "fails when using IN without () and provides meaningful error message" $
|
||||||
get "/entities?or=(id.in.1,2,id.eq.3)" `shouldRespondWith`
|
get "/entities?or=(id.in.1,2,id.eq.3)" `shouldRespondWith`
|
||||||
[json|{
|
[json|{
|
||||||
|
|||||||
@@ -390,16 +390,6 @@ spec = do
|
|||||||
[json| [{ a: "keepme", b: null }] |]
|
[json| [{ a: "keepme", b: null }] |]
|
||||||
{ matchHeaders = [matchContentTypeJson] }
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
|
||||||
it "can set a json column to escaped value" $ do
|
|
||||||
_ <- post "/json" [json| { data: {"escaped":"bar"} } |]
|
|
||||||
request methodPatch "/json?data->>escaped=eq.bar"
|
|
||||||
[("Prefer", "return=representation")]
|
|
||||||
[json| { "data": { "escaped":" \"bar" } } |]
|
|
||||||
`shouldRespondWith` [json| [{ "data": { "escaped":" \"bar" } }] |]
|
|
||||||
{ matchStatus = 200
|
|
||||||
, matchHeaders = []
|
|
||||||
}
|
|
||||||
|
|
||||||
it "can update based on a computed column" $
|
it "can update based on a computed column" $
|
||||||
request methodPatch
|
request methodPatch
|
||||||
"/items?always_true=eq.false"
|
"/items?always_true=eq.false"
|
||||||
|
|||||||
@@ -0,0 +1,189 @@
|
|||||||
|
module Feature.JsonOperatorSpec where
|
||||||
|
|
||||||
|
import Test.Hspec
|
||||||
|
import Test.Hspec.Wai
|
||||||
|
import Test.Hspec.Wai.JSON
|
||||||
|
import Network.HTTP.Types
|
||||||
|
|
||||||
|
import SpecHelper
|
||||||
|
import Network.Wai (Application)
|
||||||
|
|
||||||
|
import Protolude hiding (get)
|
||||||
|
|
||||||
|
spec :: SpecWith Application
|
||||||
|
spec = describe "json and jsonb operators" $ do
|
||||||
|
context "Shaping response with select parameter" $ do
|
||||||
|
it "obtains a json subfield one level with casting" $
|
||||||
|
get "/complex_items?id=eq.1&select=settings->>foo::json" `shouldRespondWith`
|
||||||
|
[json| [{"foo":{"int":1,"bar":"baz"}}] |] -- the value of foo here is of type "text"
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
|
||||||
|
it "renames json subfield one level with casting" $
|
||||||
|
get "/complex_items?id=eq.1&select=myFoo:settings->>foo::json" `shouldRespondWith`
|
||||||
|
[json| [{"myFoo":{"int":1,"bar":"baz"}}] |] -- the value of foo here is of type "text"
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
|
||||||
|
it "fails on bad casting (data of the wrong format)" $
|
||||||
|
get "/complex_items?select=settings->foo->>bar::integer"
|
||||||
|
`shouldRespondWith` [json| {"hint":null,"details":null,"code":"22P02","message":"invalid input syntax for integer: \"baz\""} |]
|
||||||
|
{ matchStatus = 400 , matchHeaders = [] }
|
||||||
|
|
||||||
|
it "obtains a json subfield two levels (string)" $
|
||||||
|
get "/complex_items?id=eq.1&select=settings->foo->>bar" `shouldRespondWith`
|
||||||
|
[json| [{"bar":"baz"}] |]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
|
||||||
|
it "renames json subfield two levels (string)" $
|
||||||
|
get "/complex_items?id=eq.1&select=myBar:settings->foo->>bar" `shouldRespondWith`
|
||||||
|
[json| [{"myBar":"baz"}] |]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
|
||||||
|
it "obtains a json subfield two levels with casting (int)" $
|
||||||
|
get "/complex_items?id=eq.1&select=settings->foo->>int::integer" `shouldRespondWith`
|
||||||
|
[json| [{"int":1}] |] -- the value in the db is an int, but here we expect a string for now
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
|
||||||
|
it "renames json subfield two levels with casting (int)" $
|
||||||
|
get "/complex_items?id=eq.1&select=myInt:settings->foo->>int::integer" `shouldRespondWith`
|
||||||
|
[json| [{"myInt":1}] |] -- the value in the db is an int, but here we expect a string for now
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
|
||||||
|
-- TODO the status code for the error is 404, this is because 42883 represents undefined function
|
||||||
|
-- this works fine for /rpc/unexistent requests, but for this case a 500 seems more appropriate
|
||||||
|
it "fails when a double arrow ->> is followed with a single arrow ->" $ do
|
||||||
|
get "/json_arr?select=data->>c->1"
|
||||||
|
`shouldRespondWith` [json|
|
||||||
|
{"hint":"No operator matches the given name and argument type(s). You might need to add explicit type casts.",
|
||||||
|
"details":null,"code":"42883","message":"operator does not exist: text -> integer"} |]
|
||||||
|
{ matchStatus = 404 , matchHeaders = [] }
|
||||||
|
get "/json_arr?select=data->>c->b"
|
||||||
|
`shouldRespondWith` [json|
|
||||||
|
{"hint":"No operator matches the given name and argument type(s). You might need to add explicit type casts.",
|
||||||
|
"details":null,"code":"42883","message":"operator does not exist: text -> unknown"} |]
|
||||||
|
{ matchStatus = 404 , matchHeaders = [] }
|
||||||
|
|
||||||
|
context "with array index" $ do
|
||||||
|
it "can get array of ints and alias/cast it" $ do
|
||||||
|
get "/json_arr?select=data->>0::int&id=in.(1,2)" `shouldRespondWith`
|
||||||
|
[json| [{"data":1}, {"data":4}] |]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
get "/json_arr?select=idx0:data->>0::int,idx1:data->>1::int&id=in.(1,2)" `shouldRespondWith`
|
||||||
|
[json| [{"idx0":1,"idx1":2}, {"idx0":4,"idx1":5}] |]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
|
||||||
|
it "can get nested array of ints" $ do
|
||||||
|
get "/json_arr?select=data->0->>1::int&id=in.(3,4)" `shouldRespondWith`
|
||||||
|
[json| [{"data":8}, {"data":7}] |]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
get "/json_arr?select=data->0->0->>1::int&id=in.(3,4)" `shouldRespondWith`
|
||||||
|
[json| [{"data":null}, {"data":6}] |]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
|
||||||
|
it "can get array of objects" $ do
|
||||||
|
get "/json_arr?select=data->0->>a&id=in.(5,6)" `shouldRespondWith`
|
||||||
|
[json| [{"a":"A"}, {"a":"[1,2,3]"}] |]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
get "/json_arr?select=data->0->a->>2&id=in.(5,6)" `shouldRespondWith`
|
||||||
|
[json| [{"a":null}, {"a":"3"}] |]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
|
||||||
|
it "can get array in object keys" $ do
|
||||||
|
get "/json_arr?select=data->c->>0::json&id=in.(7,8)" `shouldRespondWith`
|
||||||
|
[json| [{"c":1}, {"c":{"d": [4,5,6,7,8]}}] |]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
get "/json_arr?select=data->c->0->d->>4::int&id=in.(7,8)" `shouldRespondWith`
|
||||||
|
[json| [{"d":null}, {"d":8}] |]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
|
||||||
|
it "only treats well formed numbers as indexes" $
|
||||||
|
get "/json_arr?select=data->0->0xy1->1->23-xy-45->1->xy-6->>0::int&id=eq.9" `shouldRespondWith`
|
||||||
|
[json| [{"xy-6":3}] |]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
|
||||||
|
context "finishing json path with single arrow ->" $ do
|
||||||
|
it "works when finishing with a key" $ do
|
||||||
|
get "/json_arr?select=data->c&id=in.(7,8)" `shouldRespondWith`
|
||||||
|
[json| [{"c":[1,2,3]}, {"c":[{"d": [4,5,6,7,8]}]}] |]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
get "/json_arr?select=data->0->a&id=in.(5,6)" `shouldRespondWith`
|
||||||
|
[json| [{"a":"A"}, {"a":[1,2,3]}] |]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
|
||||||
|
it "works when finishing with an index" $ do
|
||||||
|
get "/json_arr?select=data->0->a&id=in.(5,6)" `shouldRespondWith`
|
||||||
|
[json| [{"a":"A"}, {"a":[1,2,3]}] |]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
get "/json_arr?select=data->c->0->d&id=eq.8" `shouldRespondWith`
|
||||||
|
[json| [{"d":[4,5,6,7,8]}] |]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
|
||||||
|
context "filtering response" $ do
|
||||||
|
it "can filter by properties inside json column" $ do
|
||||||
|
get "/json?data->foo->>bar=eq.baz" `shouldRespondWith`
|
||||||
|
[json| [{"data": {"id": 1, "foo": {"bar": "baz"}}}] |]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
get "/json?data->foo->>bar=eq.fake" `shouldRespondWith`
|
||||||
|
[json| [] |]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
|
||||||
|
it "can filter by properties inside json column using not" $
|
||||||
|
get "/json?data->foo->>bar=not.eq.baz" `shouldRespondWith`
|
||||||
|
[json| [] |]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
|
||||||
|
it "can filter by properties inside json column using ->>" $
|
||||||
|
get "/json?data->>id=eq.1" `shouldRespondWith`
|
||||||
|
[json| [{"data": {"id": 1, "foo": {"bar": "baz"}}}] |]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
|
||||||
|
it "can be filtered with and/or" $
|
||||||
|
get "/grandchild_entities?or=(jsonb_col->a->>b.eq.foo, jsonb_col->>b.eq.bar)&select=id" `shouldRespondWith`
|
||||||
|
[json|[{id: 4}, {id: 5}]|] { matchStatus = 200, matchHeaders = [matchContentTypeJson] }
|
||||||
|
|
||||||
|
it "can filter by array indexes" $ do
|
||||||
|
get "/json_arr?select=data&data->>0=eq.1" `shouldRespondWith`
|
||||||
|
[json| [{"data":[1, 2, 3]}] |]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
get "/json_arr?select=data&data->1->>2=eq.13" `shouldRespondWith`
|
||||||
|
[json| [{"data":[[9, 8, 7], [11, 12, 13]]}] |]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
get "/json_arr?select=data&data->1->>b=eq.B" `shouldRespondWith`
|
||||||
|
[json| [{"data":[{"a": "A"}, {"b": "B"}]}] |]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
get "/json_arr?select=data&data->1->b->>1=eq.5" `shouldRespondWith`
|
||||||
|
[json| [{"data":[{"a": [1,2,3]}, {"b": [4,5]}]}] |]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
|
||||||
|
it "can filter jsonb" $ do
|
||||||
|
get "/jsonb_test?data=eq.{\"e\":1}" `shouldRespondWith`
|
||||||
|
[json| [{"id":4,"data":{"e": 1}}] |]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
get "/jsonb_test?data->a=eq.{\"b\":2}" `shouldRespondWith`
|
||||||
|
[json| [{"id":1,"data":{"a": {"b": 2}}}] |]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
get "/jsonb_test?data->c=eq.[1,2,3]" `shouldRespondWith`
|
||||||
|
[json| [{"id":2,"data":{"c": [1, 2, 3]}}] |]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
get "/jsonb_test?data->0=eq.{\"d\":\"test\"}" `shouldRespondWith`
|
||||||
|
[json| [{"id":3,"data":[{"d": "test"}]}] |]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
|
||||||
|
context "ordering response" $ do
|
||||||
|
it "orders by a json column property asc" $
|
||||||
|
get "/json?order=data->>id.asc" `shouldRespondWith`
|
||||||
|
[json| [{"data": {"id": 0}}, {"data": {"id": 1, "foo": {"bar": "baz"}}}, {"data": {"id": 3}}] |]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
|
||||||
|
it "orders by a json column with two level property nulls first" $
|
||||||
|
get "/json?order=data->foo->>bar.nullsfirst" `shouldRespondWith`
|
||||||
|
[json| [{"data": {"id": 3}}, {"data": {"id": 0}}, {"data": {"id": 1, "foo": {"bar": "baz"}}}] |]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
|
||||||
|
context "Patching record, in a nonempty table" $
|
||||||
|
it "can set a json column to escaped value" $ do
|
||||||
|
_ <- post "/json" [json| { data: {"escaped":"bar"} } |]
|
||||||
|
request methodPatch "/json?data->>escaped=eq.bar"
|
||||||
|
[("Prefer", "return=representation")]
|
||||||
|
[json| { "data": { "escaped":" \"bar" } } |]
|
||||||
|
`shouldRespondWith` [json| [{ "data": { "escaped":" \"bar" } }] |]
|
||||||
|
{ matchStatus = 200 , matchHeaders = [] }
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
module Feature.PgVersion95Spec where
|
||||||
|
|
||||||
|
import Test.Hspec hiding (pendingWith)
|
||||||
|
import Test.Hspec.Wai
|
||||||
|
import Test.Hspec.Wai.JSON
|
||||||
|
|
||||||
|
import SpecHelper
|
||||||
|
import Network.Wai (Application)
|
||||||
|
|
||||||
|
import Protolude hiding (get)
|
||||||
|
|
||||||
|
spec :: SpecWith Application
|
||||||
|
spec = describe "features supported on PostgreSQL 9.5" $
|
||||||
|
context "json array negative index" $ do
|
||||||
|
it "can select with negative indexes" $ do
|
||||||
|
get "/json_arr?select=data->>-1::int&id=in.(1,2)" `shouldRespondWith`
|
||||||
|
[json| [{"data":3}, {"data":6}] |]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
get "/json_arr?select=data->0->>-2::int&id=in.(3,4)" `shouldRespondWith`
|
||||||
|
[json| [{"data":8}, {"data":7}] |]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
get "/json_arr?select=data->-2->>a&id=in.(5,6)" `shouldRespondWith`
|
||||||
|
[json| [{"a":"A"}, {"a":"[1,2,3]"}] |]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
|
||||||
|
it "can filter with negative indexes" $ do
|
||||||
|
get "/json_arr?select=data&data->>-3=eq.1" `shouldRespondWith`
|
||||||
|
[json| [{"data":[1, 2, 3]}] |]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
get "/json_arr?select=data&data->-1->>-3=eq.11" `shouldRespondWith`
|
||||||
|
[json| [{"data":[[9, 8, 7], [11, 12, 13]]}] |]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
get "/json_arr?select=data&data->-1->>b=eq.B" `shouldRespondWith`
|
||||||
|
[json| [{"data":[{"a": "A"}, {"b": "B"}]}] |]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
get "/json_arr?select=data&data->-1->b->>-1=eq.5" `shouldRespondWith`
|
||||||
|
[json| [{"data":[{"a": [1,2,3]}, {"b": [4,5]}]}] |]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
|
||||||
|
it "should fail on badly formed negatives" $ do
|
||||||
|
get "/json_arr?select=data->>-78xy" `shouldRespondWith`
|
||||||
|
[json|
|
||||||
|
{"details": "unexpected 'x' expecting digit, \"->\", \"::\" or end of input",
|
||||||
|
"message": "\"failed to parse select parameter (data->>-78xy)\" (line 1, column 11)"} |]
|
||||||
|
{ matchStatus = 400, matchHeaders = [matchContentTypeJson] }
|
||||||
|
get "/json_arr?select=data->>--34" `shouldRespondWith`
|
||||||
|
[json|
|
||||||
|
{"details": "unexpected \"-\" expecting digit",
|
||||||
|
"message": "\"failed to parse select parameter (data->>--34)\" (line 1, column 9)"} |]
|
||||||
|
{ matchStatus = 400, matchHeaders = [matchContentTypeJson] }
|
||||||
|
get "/json_arr?select=data->>-xy-4" `shouldRespondWith`
|
||||||
|
[json|
|
||||||
|
{"details":"unexpected \"x\" expecting digit",
|
||||||
|
"message":"\"failed to parse select parameter (data->>-xy-4)\" (line 1, column 9)"} |]
|
||||||
|
{ matchStatus = 400, matchHeaders = [matchContentTypeJson] }
|
||||||
+75
-107
@@ -216,23 +216,6 @@ spec = do
|
|||||||
[json| [{"settings":{"foo":{"int":1,"bar":"baz"}}}] |]
|
[json| [{"settings":{"foo":{"int":1,"bar":"baz"}}}] |]
|
||||||
{ matchHeaders = [matchContentTypeJson] }
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
|
||||||
it "json subfield one level with casting (json)" $
|
|
||||||
get "/complex_items?id=eq.1&select=settings->>foo::json" `shouldRespondWith`
|
|
||||||
[json| [{"foo":{"int":1,"bar":"baz"}}] |] -- the value of foo here is of type "text"
|
|
||||||
{ matchHeaders = [matchContentTypeJson] }
|
|
||||||
|
|
||||||
it "rename json subfield one level with casting (json)" $
|
|
||||||
get "/complex_items?id=eq.1&select=myFoo:settings->>foo::json" `shouldRespondWith`
|
|
||||||
[json| [{"myFoo":{"int":1,"bar":"baz"}}] |] -- the value of foo here is of type "text"
|
|
||||||
{ matchHeaders = [matchContentTypeJson] }
|
|
||||||
|
|
||||||
it "fails on bad casting (data of the wrong format)" $
|
|
||||||
get "/complex_items?select=settings->foo->>bar::integer"
|
|
||||||
`shouldRespondWith` [json| {"hint":null,"details":null,"code":"22P02","message":"invalid input syntax for integer: \"baz\""} |]
|
|
||||||
{ matchStatus = 400
|
|
||||||
, matchHeaders = []
|
|
||||||
}
|
|
||||||
|
|
||||||
it "fails on bad casting (wrong cast type)" $
|
it "fails on bad casting (wrong cast type)" $
|
||||||
get "/complex_items?select=id::fakecolumntype"
|
get "/complex_items?select=id::fakecolumntype"
|
||||||
`shouldRespondWith` [json| {"hint":null,"details":null,"code":"42704","message":"type \"fakecolumntype\" does not exist"} |]
|
`shouldRespondWith` [json| {"hint":null,"details":null,"code":"42704","message":"type \"fakecolumntype\" does not exist"} |]
|
||||||
@@ -240,34 +223,12 @@ spec = do
|
|||||||
, matchHeaders = []
|
, matchHeaders = []
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
it "json subfield two levels (string)" $
|
|
||||||
get "/complex_items?id=eq.1&select=settings->foo->>bar" `shouldRespondWith`
|
|
||||||
[json| [{"bar":"baz"}] |]
|
|
||||||
{ matchHeaders = [matchContentTypeJson] }
|
|
||||||
|
|
||||||
it "rename json subfield two levels (string)" $
|
|
||||||
get "/complex_items?id=eq.1&select=myBar:settings->foo->>bar" `shouldRespondWith`
|
|
||||||
[json| [{"myBar":"baz"}] |]
|
|
||||||
{ matchHeaders = [matchContentTypeJson] }
|
|
||||||
|
|
||||||
|
|
||||||
it "json subfield two levels with casting (int)" $
|
|
||||||
get "/complex_items?id=eq.1&select=settings->foo->>int::integer" `shouldRespondWith`
|
|
||||||
[json| [{"int":1}] |] -- the value in the db is an int, but here we expect a string for now
|
|
||||||
{ matchHeaders = [matchContentTypeJson] }
|
|
||||||
|
|
||||||
it "rename json subfield two levels with casting (int)" $
|
|
||||||
get "/complex_items?id=eq.1&select=myInt:settings->foo->>int::integer" `shouldRespondWith`
|
|
||||||
[json| [{"myInt":1}] |] -- the value in the db is an int, but here we expect a string for now
|
|
||||||
{ matchHeaders = [matchContentTypeJson] }
|
|
||||||
|
|
||||||
it "requesting parents and children" $
|
it "requesting parents and children" $
|
||||||
get "/projects?id=eq.1&select=id, name, clients(*), tasks(id, name)" `shouldRespondWith`
|
get "/projects?id=eq.1&select=id, name, clients(*), tasks(id, name)" `shouldRespondWith`
|
||||||
[json|[{"id":1,"name":"Windows 7","clients":{"id":1,"name":"Microsoft"},"tasks":[{"id":1,"name":"Design w7"},{"id":2,"name":"Code w7"}]}]|]
|
[json|[{"id":1,"name":"Windows 7","clients":{"id":1,"name":"Microsoft"},"tasks":[{"id":1,"name":"Design w7"},{"id":2,"name":"Code w7"}]}]|]
|
||||||
{ matchHeaders = [matchContentTypeJson] }
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
|
||||||
it "requesting parent without specifying primary key" $ do
|
it "requesting parent without specifying primary key" $
|
||||||
get "/projects?select=name,client(name)" `shouldRespondWith`
|
get "/projects?select=name,client(name)" `shouldRespondWith`
|
||||||
[json|[
|
[json|[
|
||||||
{"name":"Windows 7","client":{"name": "Microsoft"}},
|
{"name":"Windows 7","client":{"name": "Microsoft"}},
|
||||||
@@ -277,9 +238,6 @@ spec = do
|
|||||||
{"name":"Orphan","client":null}
|
{"name":"Orphan","client":null}
|
||||||
]|]
|
]|]
|
||||||
{ matchHeaders = [matchContentTypeJson] }
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
get "/articleStars?select=createdAt,article(owner),user(name)&limit=1" `shouldRespondWith`
|
|
||||||
[json|[{"createdAt":"2015-12-08T04:22:57.472738","article":{"owner": "postgrest_test_authenticator"},"user":{"name": "Angela Martin"}}]|]
|
|
||||||
{ matchHeaders = [matchContentTypeJson] }
|
|
||||||
|
|
||||||
it "requesting parent and renaming primary key" $
|
it "requesting parent and renaming primary key" $
|
||||||
get "/projects?select=name,client(clientId:id,name)" `shouldRespondWith`
|
get "/projects?select=name,client(clientId:id,name)" `shouldRespondWith`
|
||||||
@@ -351,40 +309,11 @@ spec = do
|
|||||||
[json|[{"id":1,"tasks":[{"id":1},{"id":2},{"id":3},{"id":4}]},{"id":2,"tasks":[{"id":5},{"id":6},{"id":7}]},{"id":3,"tasks":[{"id":1},{"id":5}]}]|]
|
[json|[{"id":1,"tasks":[{"id":1},{"id":2},{"id":3},{"id":4}]},{"id":2,"tasks":[{"id":5},{"id":6},{"id":7}]},{"id":3,"tasks":[{"id":1},{"id":5}]}]|]
|
||||||
{ matchHeaders = [matchContentTypeJson] }
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
|
||||||
it "requesting parents and children on views" $
|
|
||||||
get "/projects_view?id=eq.1&select=id, name, clients(*), tasks(id, name)" `shouldRespondWith`
|
|
||||||
[json|[{"id":1,"name":"Windows 7","clients":{"id":1,"name":"Microsoft"},"tasks":[{"id":1,"name":"Design w7"},{"id":2,"name":"Code w7"}]}]|]
|
|
||||||
{ matchHeaders = [matchContentTypeJson] }
|
|
||||||
|
|
||||||
it "requesting parents and children on views with renamed keys" $
|
|
||||||
get "/projects_view_alt?t_id=eq.1&select=t_id, name, clients(*), tasks(id, name)" `shouldRespondWith`
|
|
||||||
[json|[{"t_id":1,"name":"Windows 7","clients":{"id":1,"name":"Microsoft"},"tasks":[{"id":1,"name":"Design w7"},{"id":2,"name":"Code w7"}]}]|]
|
|
||||||
{ matchHeaders = [matchContentTypeJson] }
|
|
||||||
|
|
||||||
it "detects parent relations when having many views of a private table" $ do
|
|
||||||
get "/books?select=title,author(name)&id=eq.5" `shouldRespondWith`
|
|
||||||
[json|[ { "title": "Farenheit 451", "author": { "name": "Ray Bradbury" } } ]|]
|
|
||||||
{ matchHeaders = [matchContentTypeJson] }
|
|
||||||
get "/forties_books?select=title,author(name)&limit=1" `shouldRespondWith`
|
|
||||||
[json|[ { "title": "1984", "author": { "name": "George Orwell" } } ]|]
|
|
||||||
{ matchHeaders = [matchContentTypeJson] }
|
|
||||||
get "/fifties_books?select=title,author(name)&limit=1" `shouldRespondWith`
|
|
||||||
[json|[ { "title": "The Catcher in the Rye", "author": { "name": "J.D. Salinger" } } ]|]
|
|
||||||
{ matchHeaders = [matchContentTypeJson] }
|
|
||||||
get "/sixties_books?select=title,author(name)&limit=1" `shouldRespondWith`
|
|
||||||
[json|[ { "title": "To Kill a Mockingbird", "author": { "name": "Harper Lee" } } ]|]
|
|
||||||
{ matchHeaders = [matchContentTypeJson] }
|
|
||||||
|
|
||||||
it "requesting children with composite key" $
|
it "requesting children with composite key" $
|
||||||
get "/users_tasks?user_id=eq.2&task_id=eq.6&select=*, comments(content)" `shouldRespondWith`
|
get "/users_tasks?user_id=eq.2&task_id=eq.6&select=*, comments(content)" `shouldRespondWith`
|
||||||
[json|[{"user_id":2,"task_id":6,"comments":[{"content":"Needs to be delivered ASAP"}]}]|]
|
[json|[{"user_id":2,"task_id":6,"comments":[{"content":"Needs to be delivered ASAP"}]}]|]
|
||||||
{ matchHeaders = [matchContentTypeJson] }
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
|
||||||
it "detect relations in views from exposed schema that are based on tables in private schema and have columns renames" $
|
|
||||||
get "/articles?id=eq.1&select=id,articleStars(users(*))" `shouldRespondWith`
|
|
||||||
[json|[{"id":1,"articleStars":[{"users":{"id":1,"name":"Angela Martin"}},{"users":{"id":2,"name":"Michael Scott"}},{"users":{"id":3,"name":"Dwight Schrute"}}]}]|]
|
|
||||||
{ matchHeaders = [matchContentTypeJson] }
|
|
||||||
|
|
||||||
it "can embed by FK column name" $
|
it "can embed by FK column name" $
|
||||||
get "/projects?id=in.(1,3)&select=id,name,client_id(id,name)" `shouldRespondWith`
|
get "/projects?id=in.(1,3)&select=id,name,client_id(id,name)" `shouldRespondWith`
|
||||||
[json|[{"id":1,"name":"Windows 7","client_id":{"id":1,"name":"Microsoft"}},{"id":3,"name":"IOS","client_id":{"id":2,"name":"Apple"}}]|]
|
[json|[{"id":1,"name":"Windows 7","client_id":{"id":1,"name":"Microsoft"}},{"id":3,"name":"IOS","client_id":{"id":2,"name":"Apple"}}]|]
|
||||||
@@ -400,8 +329,71 @@ spec = do
|
|||||||
[json|[{"id":1,"name":"Windows 7","client_id":1,"client":{"id":1,"name":"Microsoft"}},{"id":3,"name":"IOS","client_id":2,"client":{"id":2,"name":"Apple"}}]|]
|
[json|[{"id":1,"name":"Windows 7","client_id":1,"client":{"id":1,"name":"Microsoft"}},{"id":3,"name":"IOS","client_id":2,"client":{"id":2,"name":"Apple"}}]|]
|
||||||
{ matchHeaders = [matchContentTypeJson] }
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
|
||||||
it "can detect fk relations through views to tables in the public schema" $
|
describe "view embedding" $ do
|
||||||
get "/consumers_view?select=*,orders_view(*)" `shouldRespondWith` 200
|
it "can detect fk relations through views to tables in the public schema" $
|
||||||
|
get "/consumers_view?select=*,orders_view(*)" `shouldRespondWith` 200
|
||||||
|
|
||||||
|
it "can request parent without specifying primary key" $
|
||||||
|
get "/articleStars?select=createdAt,article(owner),user(name)&limit=1" `shouldRespondWith`
|
||||||
|
[json|[{"createdAt":"2015-12-08T04:22:57.472738","article":{"owner": "postgrest_test_authenticator"},"user":{"name": "Angela Martin"}}]|]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
|
||||||
|
it "can detect relations in views from exposed schema that are based on tables in private schema and have columns renames" $
|
||||||
|
get "/articles?id=eq.1&select=id,articleStars(users(*))" `shouldRespondWith`
|
||||||
|
[json|[{"id":1,"articleStars":[{"users":{"id":1,"name":"Angela Martin"}},{"users":{"id":2,"name":"Michael Scott"}},{"users":{"id":3,"name":"Dwight Schrute"}}]}]|]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
|
||||||
|
it "works when requesting parents and children on views" $
|
||||||
|
get "/projects_view?id=eq.1&select=id, name, clients(*), tasks(id, name)" `shouldRespondWith`
|
||||||
|
[json|[{"id":1,"name":"Windows 7","clients":{"id":1,"name":"Microsoft"},"tasks":[{"id":1,"name":"Design w7"},{"id":2,"name":"Code w7"}]}]|]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
|
||||||
|
it "works when requesting parents and children on views with renamed keys" $
|
||||||
|
get "/projects_view_alt?t_id=eq.1&select=t_id, name, clients(*), tasks(id, name)" `shouldRespondWith`
|
||||||
|
[json|[{"t_id":1,"name":"Windows 7","clients":{"id":1,"name":"Microsoft"},"tasks":[{"id":1,"name":"Design w7"},{"id":2,"name":"Code w7"}]}]|]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
|
||||||
|
it "detects parent relations when having many views of a private table" $ do
|
||||||
|
get "/books?select=title,author(name)&id=eq.5" `shouldRespondWith`
|
||||||
|
[json|[ { "title": "Farenheit 451", "author": { "name": "Ray Bradbury" } } ]|]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
get "/forties_books?select=title,author(name)&limit=1" `shouldRespondWith`
|
||||||
|
[json|[ { "title": "1984", "author": { "name": "George Orwell" } } ]|]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
get "/fifties_books?select=title,author(name)&limit=1" `shouldRespondWith`
|
||||||
|
[json|[ { "title": "The Catcher in the Rye", "author": { "name": "J.D. Salinger" } } ]|]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
get "/sixties_books?select=title,author(name)&limit=1" `shouldRespondWith`
|
||||||
|
[json|[ { "title": "To Kill a Mockingbird", "author": { "name": "Harper Lee" } } ]|]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
|
||||||
|
it "works with views that have subselects" $
|
||||||
|
get "/authors_books_number?select=*,books(title)&id=eq.1" `shouldRespondWith`
|
||||||
|
[json|[ {"id":1, "name":"George Orwell","num_in_forties":1,"num_in_fifties":0,"num_in_sixties":0,"num_in_all_decades":1,
|
||||||
|
"books":[{"title":"1984"}]} ]|]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
|
||||||
|
it "works with views that have case subselects" $
|
||||||
|
get "/authors_have_book_in_decade?select=*,books(title)&id=eq.3" `shouldRespondWith`
|
||||||
|
[json|[ {"id":3,"name":"Antoine de Saint-Exupéry","has_book_in_forties":true,"has_book_in_fifties":false,"has_book_in_sixties":false,
|
||||||
|
"books":[{"title":"The Little Prince"}]} ]|]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
|
||||||
|
it "works with views that have subselect in the FROM clause" $
|
||||||
|
get "/forties_and_fifties_books?select=title,first_publisher,author:authors(name)&id=eq.1" `shouldRespondWith`
|
||||||
|
[json|[{"title":"1984","first_publisher":"Secker & Warburg","author":{"name":"George Orwell"}}]|]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
|
||||||
|
it "works with views that have CTE" $
|
||||||
|
get "/odd_years_publications?select=title,publication_year,first_publisher,author:authors(name)&id=in.(1,2,3)" `shouldRespondWith`
|
||||||
|
[json|[
|
||||||
|
{"title":"1984","publication_year":1949,"first_publisher":"Secker & Warburg","author":{"name":"George Orwell"}},
|
||||||
|
{"title":"The Diary of a Young Girl","publication_year":1947,"first_publisher":"Contact Publishing","author":{"name":"Anne Frank"}},
|
||||||
|
{"title":"The Little Prince","publication_year":1947,"first_publisher":"Reynal & Hitchcock","author":{"name":"Antoine de Saint-Exupéry"}} ]|]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
|
||||||
|
it "works when having a capitalized table name and camelCase fk column" $
|
||||||
|
get "/foos?select=*,bars(*)" `shouldRespondWith` 200
|
||||||
|
|
||||||
describe "path fixed" $ do
|
describe "path fixed" $ do
|
||||||
it "works when requesting children 2 levels" $
|
it "works when requesting children 2 levels" $
|
||||||
@@ -409,13 +401,7 @@ spec = do
|
|||||||
[json|[{"id":1,"projects":[{"id":1,"tasks":[{"id":1},{"id":2}]},{"id":2,"tasks":[{"id":3},{"id":4}]}]}]|]
|
[json|[{"id":1,"projects":[{"id":1,"tasks":[{"id":1},{"id":2}]},{"id":2,"tasks":[{"id":3},{"id":4}]}]}]|]
|
||||||
{ matchHeaders = [matchContentTypeJson] }
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
|
||||||
it "works with parent relation" $ do
|
it "works with parent relation" $
|
||||||
get "/message?select=id,body,sender:person_detail.sender(name,sent),recipient:person_detail.recipient(name,received)&id=lt.4" `shouldRespondWith`
|
|
||||||
[json|
|
|
||||||
[{"id":1,"body":"Hello Jane","sender":{"name":"John","sent":2},"recipient":{"name":"Jane","received":2}},
|
|
||||||
{"id":2,"body":"Hi John","sender":{"name":"Jane","sent":1},"recipient":{"name":"John","received":1}},
|
|
||||||
{"id":3,"body":"How are you doing?","sender":{"name":"John","sent":2},"recipient":{"name":"Jane","received":2}}] |]
|
|
||||||
{ matchHeaders = [matchContentTypeJson] }
|
|
||||||
get "/message?select=id,body,sender:person.sender(name),recipient:person.recipient(name)&id=lt.4" `shouldRespondWith`
|
get "/message?select=id,body,sender:person.sender(name),recipient:person.recipient(name)&id=lt.4" `shouldRespondWith`
|
||||||
[json|
|
[json|
|
||||||
[{"id":1,"body":"Hello Jane","sender":{"name":"John"},"recipient":{"name":"Jane"}},
|
[{"id":1,"body":"Hello Jane","sender":{"name":"John"},"recipient":{"name":"Jane"}},
|
||||||
@@ -423,6 +409,14 @@ spec = do
|
|||||||
{"id":3,"body":"How are you doing?","sender":{"name":"John"},"recipient":{"name":"Jane"}}] |]
|
{"id":3,"body":"How are you doing?","sender":{"name":"John"},"recipient":{"name":"Jane"}}] |]
|
||||||
{ matchHeaders = [matchContentTypeJson] }
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
|
||||||
|
it "works with a parent view relation" $
|
||||||
|
get "/message?select=id,body,sender:person_detail.sender(name,sent),recipient:person_detail.recipient(name,received)&id=lt.4" `shouldRespondWith`
|
||||||
|
[json|
|
||||||
|
[{"id":1,"body":"Hello Jane","sender":{"name":"John","sent":2},"recipient":{"name":"Jane","received":2}},
|
||||||
|
{"id":2,"body":"Hi John","sender":{"name":"Jane","sent":1},"recipient":{"name":"John","received":1}},
|
||||||
|
{"id":3,"body":"How are you doing?","sender":{"name":"John","sent":2},"recipient":{"name":"Jane","received":2}}] |]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
|
||||||
it "works with many<->many relation" $
|
it "works with many<->many relation" $
|
||||||
get "/tasks?select=id,users:users.users_tasks(id)" `shouldRespondWith`
|
get "/tasks?select=id,users:users.users_tasks(id)" `shouldRespondWith`
|
||||||
[json|[{"id":1,"users":[{"id":1},{"id":3}]},{"id":2,"users":[{"id":1}]},{"id":3,"users":[{"id":1}]},{"id":4,"users":[{"id":1}]},{"id":5,"users":[{"id":2},{"id":3}]},{"id":6,"users":[{"id":2}]},{"id":7,"users":[{"id":2}]},{"id":8,"users":[]}]|]
|
[json|[{"id":1,"users":[{"id":1},{"id":3}]},{"id":2,"users":[{"id":1}]},{"id":3,"users":[{"id":1}]},{"id":4,"users":[{"id":1}]},{"id":5,"users":[{"id":2},{"id":3}]},{"id":6,"users":[{"id":2}]},{"id":7,"users":[{"id":2}]},{"id":8,"users":[]}]|]
|
||||||
@@ -630,16 +624,6 @@ spec = do
|
|||||||
, matchHeaders = ["Content-Range" <:> "0-1/*"]
|
, matchHeaders = ["Content-Range" <:> "0-1/*"]
|
||||||
}
|
}
|
||||||
|
|
||||||
it "by a json column property asc" $
|
|
||||||
get "/json?order=data->>id.asc" `shouldRespondWith`
|
|
||||||
[json| [{"data": {"id": 0}}, {"data": {"id": 1, "foo": {"bar": "baz"}}}, {"data": {"id": 3}}] |]
|
|
||||||
{ matchHeaders = [matchContentTypeJson] }
|
|
||||||
|
|
||||||
it "by a json column with two level property nulls first" $
|
|
||||||
get "/json?order=data->foo->>bar.nullsfirst" `shouldRespondWith`
|
|
||||||
[json| [{"data": {"id": 3}}, {"data": {"id": 0}}, {"data": {"id": 1, "foo": {"bar": "baz"}}}] |]
|
|
||||||
{ matchHeaders = [matchContentTypeJson] }
|
|
||||||
|
|
||||||
it "without other constraints" $
|
it "without other constraints" $
|
||||||
get "/items?order=id.asc" `shouldRespondWith` 200
|
get "/items?order=id.asc" `shouldRespondWith` 200
|
||||||
|
|
||||||
@@ -771,22 +755,6 @@ spec = do
|
|||||||
respHeaders `shouldSatisfy` matchHeader
|
respHeaders `shouldSatisfy` matchHeader
|
||||||
"Content-Location" "/simple_pk"
|
"Content-Location" "/simple_pk"
|
||||||
|
|
||||||
describe "jsonb" $ do
|
|
||||||
it "can filter by properties inside json column" $ do
|
|
||||||
get "/json?data->foo->>bar=eq.baz" `shouldRespondWith`
|
|
||||||
[json| [{"data": {"id": 1, "foo": {"bar": "baz"}}}] |]
|
|
||||||
{ matchHeaders = [matchContentTypeJson] }
|
|
||||||
get "/json?data->foo->>bar=eq.fake" `shouldRespondWith`
|
|
||||||
[json| [] |]
|
|
||||||
{ matchHeaders = [matchContentTypeJson] }
|
|
||||||
it "can filter by properties inside json column using not" $
|
|
||||||
get "/json?data->foo->>bar=not.eq.baz" `shouldRespondWith`
|
|
||||||
[json| [] |]
|
|
||||||
{ matchHeaders = [matchContentTypeJson] }
|
|
||||||
it "can filter by properties inside json column using ->>" $
|
|
||||||
get "/json?data->>id=eq.1" `shouldRespondWith`
|
|
||||||
[json| [{"data": {"id": 1, "foo": {"bar": "baz"}}}] |]
|
|
||||||
{ matchHeaders = [matchContentTypeJson] }
|
|
||||||
|
|
||||||
describe "weird requests" $ do
|
describe "weird requests" $ do
|
||||||
it "can query as normal" $ do
|
it "can query as normal" $ do
|
||||||
|
|||||||
@@ -78,6 +78,16 @@ spec =
|
|||||||
get "/rpc/sayhello" `shouldRespondWith` 404
|
get "/rpc/sayhello" `shouldRespondWith` 404
|
||||||
get "/rpc/sayhello?any_arg=value" `shouldRespondWith` 404
|
get "/rpc/sayhello?any_arg=value" `shouldRespondWith` 404
|
||||||
|
|
||||||
|
it "works when having uppercase identifiers" $ do
|
||||||
|
get "/rpc/quotedFunction?user=mscott&fullName=Michael Scott&SSN=401-32-XXXX" `shouldRespondWith`
|
||||||
|
[json|{"user": "mscott", "fullName": "Michael Scott", "SSN": "401-32-XXXX"}|]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
post "/rpc/quotedFunction"
|
||||||
|
[json|{"user": "dschrute", "fullName": "Dwight Schrute", "SSN": "030-18-XXXX"}|]
|
||||||
|
`shouldRespondWith`
|
||||||
|
[json|{"user": "dschrute", "fullName": "Dwight Schrute", "SSN": "030-18-XXXX"}|]
|
||||||
|
{ matchHeaders = [matchContentTypeJson] }
|
||||||
|
|
||||||
context "shaping the response returned by a proc" $ do
|
context "shaping the response returned by a proc" $ do
|
||||||
it "returns a project" $ do
|
it "returns a project" $ do
|
||||||
post "/rpc/getproject" [json| { "id": 1} |] `shouldRespondWith`
|
post "/rpc/getproject" [json| { "id": 1} |] `shouldRespondWith`
|
||||||
|
|||||||
@@ -85,6 +85,27 @@ spec = do
|
|||||||
|
|
||||||
deleteResponse `shouldBe` Just "No Content"
|
deleteResponse `shouldBe` Just "No Content"
|
||||||
|
|
||||||
|
it "includes an array type for GET responses" $ do
|
||||||
|
r <- simpleBody <$> get "/"
|
||||||
|
|
||||||
|
let childGetSchema = r ^? key "paths"
|
||||||
|
. key "/child_entities"
|
||||||
|
. key "get"
|
||||||
|
. key "responses"
|
||||||
|
. key "200"
|
||||||
|
. key "schema"
|
||||||
|
|
||||||
|
liftIO $
|
||||||
|
childGetSchema `shouldBe` Just
|
||||||
|
[aesonQQ|
|
||||||
|
{
|
||||||
|
"items": {
|
||||||
|
"$ref": "#/definitions/child_entities"
|
||||||
|
},
|
||||||
|
"type": "array"
|
||||||
|
}
|
||||||
|
|]
|
||||||
|
|
||||||
it "includes definitions to tables" $ do
|
it "includes definitions to tables" $ do
|
||||||
r <- simpleBody <$> get "/"
|
r <- simpleBody <$> get "/"
|
||||||
|
|
||||||
@@ -113,7 +134,10 @@ spec = do
|
|||||||
"format": "integer",
|
"format": "integer",
|
||||||
"type": "integer"
|
"type": "integer"
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
"required": [
|
||||||
|
"id"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|]
|
|]
|
||||||
|
|
||||||
@@ -163,15 +187,54 @@ spec = do
|
|||||||
]
|
]
|
||||||
|]
|
|]
|
||||||
|
|
||||||
|
describe "Materialized view" $
|
||||||
|
|
||||||
|
it "includes materialized view properties" $ do
|
||||||
|
r <- simpleBody <$> get "/"
|
||||||
|
|
||||||
|
let method s = key "paths" . key "/materialized_projects" . key s
|
||||||
|
summary = r ^? method "get" . key "summary"
|
||||||
|
description = r ^? method "get" . key "description"
|
||||||
|
parameters = r ^? method "get" . key "parameters"
|
||||||
|
|
||||||
|
liftIO $ do
|
||||||
|
|
||||||
|
summary `shouldBe` Just "A materialized view for projects"
|
||||||
|
|
||||||
|
description `shouldBe` Just "Just a test for materialized views"
|
||||||
|
|
||||||
|
parameters `shouldBe` Just
|
||||||
|
[aesonQQ|
|
||||||
|
[
|
||||||
|
{ "$ref": "#/parameters/rowFilter.materialized_projects.id" },
|
||||||
|
{ "$ref": "#/parameters/rowFilter.materialized_projects.name" },
|
||||||
|
{ "$ref": "#/parameters/rowFilter.materialized_projects.client_id" },
|
||||||
|
{ "$ref": "#/parameters/select" },
|
||||||
|
{ "$ref": "#/parameters/order" },
|
||||||
|
{ "$ref": "#/parameters/range" },
|
||||||
|
{ "$ref": "#/parameters/rangeUnit" },
|
||||||
|
{ "$ref": "#/parameters/offset" },
|
||||||
|
{ "$ref": "#/parameters/limit" },
|
||||||
|
{ "$ref": "#/parameters/preferCount" }
|
||||||
|
]
|
||||||
|
|]
|
||||||
|
|
||||||
describe "RPC" $ do
|
describe "RPC" $ do
|
||||||
|
|
||||||
it "includes body schema for arguments" $ do
|
it "includes function summary/description and body schema for arguments" $ do
|
||||||
r <- simpleBody <$> get "/"
|
r <- simpleBody <$> get "/"
|
||||||
let args = r ^? key "paths" . key "/rpc/varied_arguments"
|
|
||||||
. key "post" . key "parameters"
|
|
||||||
. nth 0 . key "schema"
|
|
||||||
|
|
||||||
liftIO $
|
let method s = key "paths" . key "/rpc/varied_arguments" . key s
|
||||||
|
args = r ^? method "post" . key "parameters" . nth 0 . key "schema"
|
||||||
|
summary = r ^? method "post" . key "summary"
|
||||||
|
description = r ^? method "post" . key "description"
|
||||||
|
|
||||||
|
liftIO $ do
|
||||||
|
|
||||||
|
summary `shouldBe` Just "An RPC function"
|
||||||
|
|
||||||
|
description `shouldBe` Just "Just a test for RPC function arguments"
|
||||||
|
|
||||||
args `shouldBe` Just
|
args `shouldBe` Just
|
||||||
[aesonQQ|
|
[aesonQQ|
|
||||||
{
|
{
|
||||||
@@ -213,7 +276,8 @@ spec = do
|
|||||||
"type": "integer"
|
"type": "integer"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"type": "object"
|
"type": "object",
|
||||||
|
"description": "An RPC function\n\nJust a test for RPC function arguments"
|
||||||
}
|
}
|
||||||
|]
|
|]
|
||||||
|
|
||||||
|
|||||||
@@ -198,3 +198,30 @@ spec =
|
|||||||
[("Prefer", "return=representation"), ("Accept", "application/vnd.pgrst.object+json")]
|
[("Prefer", "return=representation"), ("Accept", "application/vnd.pgrst.object+json")]
|
||||||
[str| [ { "name": "Ruby", "rank": 11 } ]|]
|
[str| [ { "name": "Ruby", "rank": 11 } ]|]
|
||||||
`shouldRespondWith` [json|{ "name": "Ruby", "rank": 11 }|] { matchHeaders = [matchContentTypeSingular] }
|
`shouldRespondWith` [json|{ "name": "Ruby", "rank": 11 }|] { matchHeaders = [matchContentTypeSingular] }
|
||||||
|
|
||||||
|
context "with a camel case pk column" $ do
|
||||||
|
it "works with POST and merge-duplicates/ignore-duplicates headers" $ do
|
||||||
|
request methodPost "/UnitTest" [("Prefer", "return=representation"), ("Prefer", "resolution=merge-duplicates")]
|
||||||
|
[json| [
|
||||||
|
{ "idUnitTest": 1, "nameUnitTest": "name of unittest 1" },
|
||||||
|
{ "idUnitTest": 2, "nameUnitTest": "name of unittest 2" }
|
||||||
|
]|] `shouldRespondWith` [json|[
|
||||||
|
{ "idUnitTest": 1, "nameUnitTest": "name of unittest 1" },
|
||||||
|
{ "idUnitTest": 2, "nameUnitTest": "name of unittest 2" }
|
||||||
|
]|]
|
||||||
|
{ matchStatus = 201
|
||||||
|
, matchHeaders = ["Preference-Applied" <:> "resolution=merge-duplicates", matchContentTypeJson]
|
||||||
|
}
|
||||||
|
request methodPost "/UnitTest" [("Prefer", "return=representation"), ("Prefer", "resolution=ignore-duplicates")]
|
||||||
|
[json| [
|
||||||
|
{ "idUnitTest": 1, "nameUnitTest": "name of unittest 1" },
|
||||||
|
{ "idUnitTest": 2, "nameUnitTest": "name of unittest 2" }
|
||||||
|
]|] `shouldRespondWith` [json|[]|]
|
||||||
|
{ matchStatus = 201
|
||||||
|
, matchHeaders = ["Preference-Applied" <:> "resolution=ignore-duplicates", matchContentTypeJson]
|
||||||
|
}
|
||||||
|
|
||||||
|
it "works with PUT" $ do
|
||||||
|
put "/UnitTest?idUnitTest=eq.1" [str| [ { "idUnitTest": 1, "nameUnitTest": "unit test 1" } ]|] `shouldRespondWith` 204
|
||||||
|
get "/UnitTest?idUnitTest=eq.1" `shouldRespondWith`
|
||||||
|
[json| [ { "idUnitTest": 1, "nameUnitTest": "unit test 1" } ]|] { matchHeaders = [matchContentTypeJson] }
|
||||||
|
|||||||
+7
-3
@@ -6,8 +6,8 @@ import SpecHelper
|
|||||||
import qualified Hasql.Pool as P
|
import qualified Hasql.Pool as P
|
||||||
|
|
||||||
import PostgREST.App (postgrest)
|
import PostgREST.App (postgrest)
|
||||||
import PostgREST.Config (pgVersion95, pgVersion96, configSettings)
|
import PostgREST.Config (pgVersion95, pgVersion96)
|
||||||
import PostgREST.DbStructure (getDbStructure, getPgVersion, fillSessionWithSettings)
|
import PostgREST.DbStructure (getDbStructure, getPgVersion)
|
||||||
import PostgREST.Types (DbStructure(..))
|
import PostgREST.Types (DbStructure(..))
|
||||||
import Control.AutoUpdate (defaultUpdateSettings, mkAutoUpdate, updateAction)
|
import Control.AutoUpdate (defaultUpdateSettings, mkAutoUpdate, updateAction)
|
||||||
import Data.Function (id)
|
import Data.Function (id)
|
||||||
@@ -22,6 +22,7 @@ import qualified Feature.ConcurrentSpec
|
|||||||
import qualified Feature.CorsSpec
|
import qualified Feature.CorsSpec
|
||||||
import qualified Feature.DeleteSpec
|
import qualified Feature.DeleteSpec
|
||||||
import qualified Feature.InsertSpec
|
import qualified Feature.InsertSpec
|
||||||
|
import qualified Feature.JsonOperatorSpec
|
||||||
import qualified Feature.NoJwtSpec
|
import qualified Feature.NoJwtSpec
|
||||||
import qualified Feature.QueryLimitedSpec
|
import qualified Feature.QueryLimitedSpec
|
||||||
import qualified Feature.QuerySpec
|
import qualified Feature.QuerySpec
|
||||||
@@ -33,6 +34,7 @@ import qualified Feature.ProxySpec
|
|||||||
import qualified Feature.AndOrParamsSpec
|
import qualified Feature.AndOrParamsSpec
|
||||||
import qualified Feature.RpcSpec
|
import qualified Feature.RpcSpec
|
||||||
import qualified Feature.NonexistentSchemaSpec
|
import qualified Feature.NonexistentSchemaSpec
|
||||||
|
import qualified Feature.PgVersion95Spec
|
||||||
import qualified Feature.PgVersion96Spec
|
import qualified Feature.PgVersion96Spec
|
||||||
import qualified Feature.UpsertSpec
|
import qualified Feature.UpsertSpec
|
||||||
|
|
||||||
@@ -64,11 +66,12 @@ main = do
|
|||||||
nonexistentSchemaApp = return $ postgrest (testNonexistentSchemaCfg testDbConn) refDbStructure pool getTime $ pure ()
|
nonexistentSchemaApp = return $ postgrest (testNonexistentSchemaCfg testDbConn) refDbStructure pool getTime $ pure ()
|
||||||
|
|
||||||
let reset :: IO ()
|
let reset :: IO ()
|
||||||
reset = P.use pool (fillSessionWithSettings (configSettings $ testCfg testDbConn)) >> resetDb testDbConn
|
reset = resetDb testDbConn
|
||||||
|
|
||||||
actualPgVersion = pgVersion dbStructure
|
actualPgVersion = pgVersion dbStructure
|
||||||
extraSpecs =
|
extraSpecs =
|
||||||
[("Feature.UpsertSpec", Feature.UpsertSpec.spec) | actualPgVersion >= pgVersion95] ++
|
[("Feature.UpsertSpec", Feature.UpsertSpec.spec) | actualPgVersion >= pgVersion95] ++
|
||||||
|
[("Feature.PgVersion95Spec", Feature.PgVersion95Spec.spec) | actualPgVersion >= pgVersion95] ++
|
||||||
[("Feature.PgVersion96Spec", Feature.PgVersion96Spec.spec) | actualPgVersion >= pgVersion96]
|
[("Feature.PgVersion96Spec", Feature.PgVersion96Spec.spec) | actualPgVersion >= pgVersion96]
|
||||||
|
|
||||||
specs = uncurry describe <$> [
|
specs = uncurry describe <$> [
|
||||||
@@ -77,6 +80,7 @@ main = do
|
|||||||
, ("Feature.CorsSpec" , Feature.CorsSpec.spec)
|
, ("Feature.CorsSpec" , Feature.CorsSpec.spec)
|
||||||
, ("Feature.DeleteSpec" , Feature.DeleteSpec.spec)
|
, ("Feature.DeleteSpec" , Feature.DeleteSpec.spec)
|
||||||
, ("Feature.InsertSpec" , Feature.InsertSpec.spec)
|
, ("Feature.InsertSpec" , Feature.InsertSpec.spec)
|
||||||
|
, ("Feature.JsonOperatorSpec" , Feature.JsonOperatorSpec.spec)
|
||||||
, ("Feature.QuerySpec" , Feature.QuerySpec.spec)
|
, ("Feature.QuerySpec" , Feature.QuerySpec.spec)
|
||||||
, ("Feature.RpcSpec" , Feature.RpcSpec.spec)
|
, ("Feature.RpcSpec" , Feature.RpcSpec.spec)
|
||||||
, ("Feature.RangeSpec" , Feature.RangeSpec.spec)
|
, ("Feature.RangeSpec" , Feature.RangeSpec.spec)
|
||||||
|
|||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
#! /bin/bash
|
#! /usr/bin/env bash
|
||||||
if [ -z "$1" ]
|
if [ -z "$1" ]
|
||||||
then
|
then
|
||||||
echo "Please supply the connection uri for the user with create database privileges"
|
echo "Please supply the connection uri for the user with create database privileges"
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
#! /bin/bash
|
#! /usr/bin/env bash
|
||||||
if [ -z "$1" ]
|
if [ -z "$1" ]
|
||||||
then
|
then
|
||||||
echo "Please supply the connection uri for the user with create database privileges"
|
echo "Please supply the connection uri for the user with create database privileges"
|
||||||
|
|||||||
Vendored
+40
-9
@@ -373,16 +373,27 @@ INSERT INTO authors VALUES (7, 'Harper Lee');
|
|||||||
INSERT INTO authors VALUES (8, 'Kurt Vonnegut');
|
INSERT INTO authors VALUES (8, 'Kurt Vonnegut');
|
||||||
INSERT INTO authors VALUES (9, 'Ken Kesey');
|
INSERT INTO authors VALUES (9, 'Ken Kesey');
|
||||||
|
|
||||||
|
TRUNCATE TABLE publishers CASCADE;
|
||||||
|
INSERT INTO publishers VALUES (1, 'Secker & Warburg');
|
||||||
|
INSERT INTO publishers VALUES (2, 'Contact Publishing');
|
||||||
|
INSERT INTO publishers VALUES (3, 'Reynal & Hitchcock');
|
||||||
|
INSERT INTO publishers VALUES (4, 'Little, Brown and Company');
|
||||||
|
INSERT INTO publishers VALUES (5, 'Ballantine Books');
|
||||||
|
INSERT INTO publishers VALUES (6, 'Faber and Faber');
|
||||||
|
INSERT INTO publishers VALUES (7, 'J. B. Lippincott & Co.');
|
||||||
|
INSERT INTO publishers VALUES (8, 'Delacorte');
|
||||||
|
INSERT INTO publishers VALUES (9, 'Viking Press & Signet Books');
|
||||||
|
|
||||||
TRUNCATE TABLE books CASCADE;
|
TRUNCATE TABLE books CASCADE;
|
||||||
INSERT INTO books VALUES (1, '1984', 1949, 1);
|
INSERT INTO books VALUES (1, '1984', 1949, 1, 1);
|
||||||
INSERT INTO books VALUES (2, 'The Diary of a Young Girl', 1947, 2);
|
INSERT INTO books VALUES (2, 'The Diary of a Young Girl', 1947, 2, 2);
|
||||||
INSERT INTO books VALUES (3, 'The Little Prince', 1947, 3);
|
INSERT INTO books VALUES (3, 'The Little Prince', 1947, 3, 3);
|
||||||
INSERT INTO books VALUES (4, 'The Catcher in the Rye', 1951, 4);
|
INSERT INTO books VALUES (4, 'The Catcher in the Rye', 1951, 4, 4);
|
||||||
INSERT INTO books VALUES (5, 'Farenheit 451', 1953, 5);
|
INSERT INTO books VALUES (5, 'Farenheit 451', 1953, 5, 5);
|
||||||
INSERT INTO books VALUES (6, 'Lord of the Flies', 1954, 6);
|
INSERT INTO books VALUES (6, 'Lord of the Flies', 1954, 6, 6);
|
||||||
INSERT INTO books VALUES (7, 'To Kill a Mockingbird', 1960, 7);
|
INSERT INTO books VALUES (7, 'To Kill a Mockingbird', 1960, 7, 7);
|
||||||
INSERT INTO books VALUES (8, 'Slaughterhouse-Five', 1969, 8);
|
INSERT INTO books VALUES (8, 'Slaughterhouse-Five', 1969, 8, 8);
|
||||||
INSERT INTO books VALUES (9, 'One Flew Over the Cuckoo''s Nest', 1962, 9);
|
INSERT INTO books VALUES (9, 'One Flew Over the Cuckoo''s Nest', 1962, 9, 9);
|
||||||
|
|
||||||
SET search_path = test, pg_catalog;
|
SET search_path = test, pg_catalog;
|
||||||
|
|
||||||
@@ -411,3 +422,23 @@ INSERT INTO zone VALUES (4, 'store 4', 3, 1);
|
|||||||
|
|
||||||
-- for foreign table projects_dump
|
-- for foreign table projects_dump
|
||||||
copy (select id, name, client_id from projects) to '/tmp/projects_dump.csv' with csv;
|
copy (select id, name, client_id from projects) to '/tmp/projects_dump.csv' with csv;
|
||||||
|
|
||||||
|
TRUNCATE TABLE "UnitTest" CASCADE;
|
||||||
|
INSERT INTO "UnitTest" VALUES (1, 'unit test 1');
|
||||||
|
|
||||||
|
TRUNCATE TABLE json_arr CASCADE;
|
||||||
|
INSERT INTO json_arr VALUES (1, '[1, 2, 3]');
|
||||||
|
INSERT INTO json_arr VALUES (2, '[4, 5, 6]');
|
||||||
|
INSERT INTO json_arr VALUES (3, '[[9, 8, 7], [11, 12, 13]]');
|
||||||
|
INSERT INTO json_arr VALUES (4, '[[[5, 6], 7, 8]]');
|
||||||
|
INSERT INTO json_arr VALUES (5, '[{"a": "A"}, {"b": "B"}]');
|
||||||
|
INSERT INTO json_arr VALUES (6, '[{"a": [1,2,3]}, {"b": [4,5]}]');
|
||||||
|
INSERT INTO json_arr VALUES (7, '{"c": [1,2,3], "d": [4,5]}');
|
||||||
|
INSERT INTO json_arr VALUES (8, '{"c": [{"d": [4,5,6,7,8]}]}');
|
||||||
|
INSERT INTO json_arr VALUES (9, '[{"0xy1": [1,{"23-xy-45": [2, {"xy-6": [3]}]}]}]');
|
||||||
|
|
||||||
|
TRUNCATE TABLE jsonb_test CASCADE;
|
||||||
|
INSERT INTO jsonb_test VALUES (1, '{ "a": {"b": 2} }');
|
||||||
|
INSERT INTO jsonb_test VALUES (2, '{ "c": [1,2,3] }');
|
||||||
|
INSERT INTO jsonb_test VALUES (3, '[{ "d": "test" }]');
|
||||||
|
INSERT INTO jsonb_test VALUES (4, '{ "e": 1 }');
|
||||||
|
|||||||
Vendored
+10
@@ -79,6 +79,16 @@ GRANT ALL ON TABLE
|
|||||||
, space
|
, space
|
||||||
, zone
|
, zone
|
||||||
, projects_dump
|
, projects_dump
|
||||||
|
, "UnitTest"
|
||||||
|
, json_arr
|
||||||
|
, jsonb_test
|
||||||
|
, authors_books_number
|
||||||
|
, authors_have_book_in_decade
|
||||||
|
, forties_and_fifties_books
|
||||||
|
, odd_years_publications
|
||||||
|
, foos
|
||||||
|
, bars
|
||||||
|
, materialized_projects
|
||||||
TO postgrest_test_anonymous;
|
TO postgrest_test_anonymous;
|
||||||
|
|
||||||
GRANT INSERT ON TABLE insertonly TO postgrest_test_anonymous;
|
GRANT INSERT ON TABLE insertonly TO postgrest_test_anonymous;
|
||||||
|
|||||||
Vendored
+142
-14
@@ -222,6 +222,10 @@ AS $_$
|
|||||||
SELECT 'Hi'::text;
|
SELECT 'Hi'::text;
|
||||||
$_$;
|
$_$;
|
||||||
|
|
||||||
|
COMMENT ON FUNCTION varied_arguments(double precision, character varying, boolean, date, money, enum_menagerie_type, integer) IS
|
||||||
|
$_$An RPC function
|
||||||
|
|
||||||
|
Just a test for RPC function arguments$_$;
|
||||||
|
|
||||||
--
|
--
|
||||||
-- Name: jwt_test(); Type: FUNCTION; Schema: test; Owner: -
|
-- Name: jwt_test(); Type: FUNCTION; Schema: test; Owner: -
|
||||||
@@ -1120,17 +1124,17 @@ create view images_base64 as (
|
|||||||
select name, replace(encode(img, 'base64'), E'\n', '') as img from images
|
select name, replace(encode(img, 'base64'), E'\n', '') as img from images
|
||||||
);
|
);
|
||||||
|
|
||||||
create function test.ret_enum(val text) returns test.enum_menagerie_type as $$
|
create function test.ret_enum(val text) returns test.enum_menagerie_type as $$
|
||||||
select val::test.enum_menagerie_type;
|
select val::test.enum_menagerie_type;
|
||||||
$$ language sql;
|
$$ language sql;
|
||||||
|
|
||||||
create domain one_nine as integer check (value >= 1 and value <= 9);
|
create domain one_nine as integer check (value >= 1 and value <= 9);
|
||||||
|
|
||||||
create function test.ret_array() returns integer[] as $$
|
create function test.ret_array() returns integer[] as $$
|
||||||
select '{1,2,3}'::integer[];
|
select '{1,2,3}'::integer[];
|
||||||
$$ language sql;
|
$$ language sql;
|
||||||
|
|
||||||
create function test.ret_domain(val integer) returns test.one_nine as $$
|
create function test.ret_domain(val integer) returns test.one_nine as $$
|
||||||
select val::test.one_nine;
|
select val::test.one_nine;
|
||||||
$$ language sql;
|
$$ language sql;
|
||||||
|
|
||||||
@@ -1144,20 +1148,20 @@ $$ language sql;
|
|||||||
|
|
||||||
create function test.ret_scalars() returns table(
|
create function test.ret_scalars() returns table(
|
||||||
a text, b test.enum_menagerie_type, c test.one_nine, d int4range
|
a text, b test.enum_menagerie_type, c test.one_nine, d int4range
|
||||||
) as $$
|
) as $$
|
||||||
select row('scalars'::text, enum_first(null::test.enum_menagerie_type),
|
select row('scalars'::text, enum_first(null::test.enum_menagerie_type),
|
||||||
1::test.one_nine, int4range(10, 20));
|
1::test.one_nine, int4range(10, 20));
|
||||||
$$ language sql;
|
$$ language sql;
|
||||||
|
|
||||||
create type test.point_2d as (x integer, y integer);
|
create type test.point_2d as (x integer, y integer);
|
||||||
|
|
||||||
create function test.ret_point_2d() returns test.point_2d as $$
|
create function test.ret_point_2d() returns test.point_2d as $$
|
||||||
select row(10, 5)::test.point_2d;
|
select row(10, 5)::test.point_2d;
|
||||||
$$ language sql;
|
$$ language sql;
|
||||||
|
|
||||||
create type private.point_3d as (x integer, y integer, z integer);
|
create type private.point_3d as (x integer, y integer, z integer);
|
||||||
|
|
||||||
create function test.ret_point_3d() returns private.point_3d as $$
|
create function test.ret_point_3d() returns private.point_3d as $$
|
||||||
select row(7, -3, 4)::private.point_3d;
|
select row(7, -3, 4)::private.point_3d;
|
||||||
$$ language sql;
|
$$ language sql;
|
||||||
|
|
||||||
@@ -1171,17 +1175,17 @@ create function test.ret_rows_with_base64_bin() returns setof test.images_base64
|
|||||||
select i.name, i.img from test.images_base64 i;
|
select i.name, i.img from test.images_base64 i;
|
||||||
$$ language sql;
|
$$ language sql;
|
||||||
|
|
||||||
create function test.single_article(id integer) returns test.articles as $$
|
create function test.single_article(id integer) returns test.articles as $$
|
||||||
select a.* from test.articles a where a.id = $1;
|
select a.* from test.articles a where a.id = $1;
|
||||||
$$ language sql;
|
$$ language sql;
|
||||||
|
|
||||||
create function test.get_guc_value(name text) returns text as $$
|
create function test.get_guc_value(name text) returns text as $$
|
||||||
select nullif(current_setting(name), '')::text;
|
select nullif(current_setting(name), '')::text;
|
||||||
$$ language sql;
|
$$ language sql;
|
||||||
|
|
||||||
create table w_or_wo_comma_names ( name text );
|
create table w_or_wo_comma_names ( name text );
|
||||||
|
|
||||||
create table items_with_different_col_types (
|
create table items_with_different_col_types (
|
||||||
int_data integer,
|
int_data integer,
|
||||||
text_data text,
|
text_data text,
|
||||||
bool_data bool,
|
bool_data bool,
|
||||||
@@ -1194,20 +1198,20 @@ create table items_with_different_col_types (
|
|||||||
|
|
||||||
-- Tables used for testing complex boolean logic with and/or query params
|
-- Tables used for testing complex boolean logic with and/or query params
|
||||||
|
|
||||||
create table entities (
|
create table entities (
|
||||||
id integer primary key,
|
id integer primary key,
|
||||||
name text,
|
name text,
|
||||||
arr integer[],
|
arr integer[],
|
||||||
text_search_vector tsvector
|
text_search_vector tsvector
|
||||||
);
|
);
|
||||||
|
|
||||||
create table child_entities (
|
create table child_entities (
|
||||||
id integer primary key,
|
id integer primary key,
|
||||||
name text,
|
name text,
|
||||||
parent_id integer references entities(id)
|
parent_id integer references entities(id)
|
||||||
);
|
);
|
||||||
|
|
||||||
create table grandchild_entities (
|
create table grandchild_entities (
|
||||||
id integer primary key,
|
id integer primary key,
|
||||||
name text,
|
name text,
|
||||||
parent_id integer references child_entities(id),
|
parent_id integer references child_entities(id),
|
||||||
@@ -1396,11 +1400,17 @@ create table private.authors(
|
|||||||
name text
|
name text
|
||||||
);
|
);
|
||||||
|
|
||||||
|
create table private.publishers(
|
||||||
|
id integer primary key,
|
||||||
|
name text
|
||||||
|
);
|
||||||
|
|
||||||
create table private.books(
|
create table private.books(
|
||||||
id integer primary key,
|
id integer primary key,
|
||||||
title text,
|
title text,
|
||||||
publication_year smallint,
|
publication_year smallint,
|
||||||
author_id integer references private.authors(id)
|
author_id integer references private.authors(id),
|
||||||
|
first_publisher_id integer references private.publishers(id)
|
||||||
);
|
);
|
||||||
|
|
||||||
create view test.authors as select id, name from private.authors;
|
create view test.authors as select id, name from private.authors;
|
||||||
@@ -1451,3 +1461,121 @@ comment on foreign table projects_dump is
|
|||||||
$$A temporary projects dump
|
$$A temporary projects dump
|
||||||
|
|
||||||
Just a test for foreign tables$$;
|
Just a test for foreign tables$$;
|
||||||
|
|
||||||
|
create table "UnitTest"(
|
||||||
|
"idUnitTest" integer primary key,
|
||||||
|
"nameUnitTest" text
|
||||||
|
);
|
||||||
|
|
||||||
|
create table json_arr(
|
||||||
|
id integer primary key,
|
||||||
|
data pg_catalog.json
|
||||||
|
);
|
||||||
|
|
||||||
|
create table jsonb_test(
|
||||||
|
id integer primary key,
|
||||||
|
data jsonb
|
||||||
|
);
|
||||||
|
|
||||||
|
create view test.authors_books_number as
|
||||||
|
select
|
||||||
|
id,
|
||||||
|
name,
|
||||||
|
(
|
||||||
|
select
|
||||||
|
count(*)
|
||||||
|
from forties_books where author_id = authors.id
|
||||||
|
) as num_in_forties,
|
||||||
|
(
|
||||||
|
select
|
||||||
|
count(*)
|
||||||
|
from fifties_books where author_id = authors.id
|
||||||
|
) as num_in_fifties,
|
||||||
|
(
|
||||||
|
select
|
||||||
|
count(*)
|
||||||
|
from sixties_books where author_id = authors.id
|
||||||
|
) as num_in_sixties,
|
||||||
|
(
|
||||||
|
select
|
||||||
|
count(*)
|
||||||
|
from (
|
||||||
|
select id
|
||||||
|
from forties_books where author_id = authors.id
|
||||||
|
union
|
||||||
|
select id
|
||||||
|
from fifties_books where author_id = authors.id
|
||||||
|
union
|
||||||
|
select id
|
||||||
|
from sixties_books where author_id = authors.id
|
||||||
|
) _
|
||||||
|
) as num_in_all_decades
|
||||||
|
from private.authors;
|
||||||
|
|
||||||
|
create view test.authors_have_book_in_decade as
|
||||||
|
select
|
||||||
|
id,
|
||||||
|
name,
|
||||||
|
case
|
||||||
|
when (x.id in (select author_id from test.forties_books))
|
||||||
|
then true
|
||||||
|
else false
|
||||||
|
end as has_book_in_forties,
|
||||||
|
case
|
||||||
|
when (x.id in (select author_id from test.fifties_books))
|
||||||
|
then true
|
||||||
|
else false
|
||||||
|
end as has_book_in_fifties,
|
||||||
|
case
|
||||||
|
when (x.id in (select author_id from test.sixties_books))
|
||||||
|
then true
|
||||||
|
else false
|
||||||
|
end as has_book_in_sixties
|
||||||
|
from private.authors x;
|
||||||
|
|
||||||
|
create view test.forties_and_fifties_books as
|
||||||
|
select x.id, x.title, x.publication_year, y.name as first_publisher, x.author_id
|
||||||
|
from (
|
||||||
|
select id, title, publication_year, author_id, first_publisher_id from private.books
|
||||||
|
where publication_year >= 1940 and publication_year < 1960) x
|
||||||
|
join private.publishers y on y.id = x.first_publisher_id;
|
||||||
|
|
||||||
|
create view test.odd_years_publications as
|
||||||
|
with
|
||||||
|
odd_years_books as(
|
||||||
|
select id, title, publication_year, author_id, first_publisher_id
|
||||||
|
from private.books
|
||||||
|
where publication_year % 2 <> 0
|
||||||
|
)
|
||||||
|
select
|
||||||
|
x.id, x.title, x.publication_year,
|
||||||
|
y.name as first_publisher, x.author_id
|
||||||
|
from odd_years_books x
|
||||||
|
join private.publishers y on y.id = x.first_publisher_id;
|
||||||
|
|
||||||
|
CREATE TABLE test."Foo"(
|
||||||
|
id int primary key,
|
||||||
|
name text
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE test.bar(
|
||||||
|
id int primary key,
|
||||||
|
name text,
|
||||||
|
"fooId" int references "Foo"(id)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE VIEW test.foos as select id,name from "Foo";
|
||||||
|
CREATE VIEW test.bars as select id, "fooId", name from bar;
|
||||||
|
|
||||||
|
create materialized view materialized_projects as
|
||||||
|
select id, name, client_id from projects;
|
||||||
|
|
||||||
|
comment on materialized view materialized_projects is
|
||||||
|
$$A materialized view for projects
|
||||||
|
|
||||||
|
Just a test for materialized views$$;
|
||||||
|
|
||||||
|
create or replace function test."quotedFunction"("user" text, "fullName" text, "SSN" text)
|
||||||
|
returns jsonb AS $$
|
||||||
|
select format('{"user": "%s", "fullName": "%s", "SSN": "%s"}', "user", "fullName", "SSN")::jsonb;
|
||||||
|
$$ language sql;
|
||||||
|
|||||||
@@ -120,6 +120,49 @@ invalidRoleClaimKey(){
|
|||||||
pgrStop
|
pgrStop
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# ensure iat claim is successful in the presence of pgrst time cache, see https://github.com/PostgREST/postgrest/issues/1139
|
||||||
|
ensureIatClaimWorks(){
|
||||||
|
pgrStart "./configs/simple.config"
|
||||||
|
while pgrStarted && test "$( rootStatus )" -ne 200
|
||||||
|
do
|
||||||
|
# wait for the server to start
|
||||||
|
sleep 0.1 \
|
||||||
|
|| sleep 1 # fallback: subsecond sleep is not standard and may fail
|
||||||
|
done
|
||||||
|
for i in {1..10}; do \
|
||||||
|
iatJwt=$(psql -qtAX postgrest_test -c "select jwt.sign(row_to_json(r), 'reallyreallyreallyreallyverysafe') from ( select 'postgrest_test_author' as role, extract(epoch from now()) as iat) r")
|
||||||
|
httpStatus="$( authorsStatus $iatJwt )"
|
||||||
|
if test "$httpStatus" -ne 200
|
||||||
|
then
|
||||||
|
ko "iat claim rejected with $httpStatus"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
sleep .5;\
|
||||||
|
done
|
||||||
|
ok "accepted iat claim"
|
||||||
|
pgrStop
|
||||||
|
}
|
||||||
|
|
||||||
|
# ensure app settings don't reset on pool timeout of 10 seconds, see https://github.com/PostgREST/postgrest/issues/1141
|
||||||
|
ensureAppSettings(){
|
||||||
|
pgrStart "./configs/app-settings.config"
|
||||||
|
while pgrStarted && test "$( rootStatus )" -ne 200
|
||||||
|
do
|
||||||
|
# wait for the server to start
|
||||||
|
sleep 0.1 \
|
||||||
|
|| sleep 1 # fallback: subsecond sleep is not standard and may fail
|
||||||
|
done
|
||||||
|
sleep 11
|
||||||
|
response=$(curl -s "http://localhost:$pgrPort/rpc/get_guc_value?name=app.settings.external_api_secret")
|
||||||
|
if test "$response" = "\"0123456789abcdef\""
|
||||||
|
then
|
||||||
|
ok "GET /rpc/get_guc_value response is $response"
|
||||||
|
else
|
||||||
|
ko "GET /rpc/get_guc_value response was $response"
|
||||||
|
fi
|
||||||
|
pgrStop
|
||||||
|
}
|
||||||
|
|
||||||
# PRE: curl must be available
|
# PRE: curl must be available
|
||||||
test -n "$(command -v curl)" || bailOut 'curl is not available'
|
test -n "$(command -v curl)" || bailOut 'curl is not available'
|
||||||
|
|
||||||
@@ -157,6 +200,9 @@ invalidRoleClaimKey '.#$%&$%/'
|
|||||||
invalidRoleClaimKey ''
|
invalidRoleClaimKey ''
|
||||||
invalidRoleClaimKey 1234
|
invalidRoleClaimKey 1234
|
||||||
|
|
||||||
|
ensureIatClaimWorks
|
||||||
|
ensureAppSettings
|
||||||
|
|
||||||
cleanUp
|
cleanUp
|
||||||
|
|
||||||
exit $failedTests
|
exit $failedTests
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
db-uri = "postgres:///postgrest_test"
|
||||||
|
db-schema = "test"
|
||||||
|
db-anon-role = "postgrest_test_anonymous"
|
||||||
|
db-pool = 1
|
||||||
|
server-host = "127.0.0.1"
|
||||||
|
server-port = 49421
|
||||||
|
|
||||||
|
app.settings.external_api_secret = "0123456789abcdef"
|
||||||
@@ -2,7 +2,7 @@ db-uri = "postgres:///postgrest_test"
|
|||||||
db-schema = "test"
|
db-schema = "test"
|
||||||
db-anon-role = "postgrest_test_anonymous"
|
db-anon-role = "postgrest_test_anonymous"
|
||||||
db-pool = 1
|
db-pool = 1
|
||||||
server-host = "*4"
|
server-host = "127.0.0.1"
|
||||||
server-port = 49421
|
server-port = 49421
|
||||||
|
|
||||||
# Read secret from a file: /dev/stdin (alias for standard input)
|
# Read secret from a file: /dev/stdin (alias for standard input)
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ db-uri = "postgres:///postgrest_test"
|
|||||||
db-schema = "test"
|
db-schema = "test"
|
||||||
db-anon-role = "postgrest_test_anonymous"
|
db-anon-role = "postgrest_test_anonymous"
|
||||||
db-pool = 1
|
db-pool = 1
|
||||||
server-host = "*4"
|
server-host = "127.0.0.1"
|
||||||
server-port = 49421
|
server-port = 49421
|
||||||
role-claim-key = "$(ROLE_CLAIM_KEY)"
|
role-claim-key = "$(ROLE_CLAIM_KEY)"
|
||||||
jwt-secret = "reallyreallyreallyreallyverysafe"
|
jwt-secret = "reallyreallyreallyreallyverysafe"
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ db-uri = "postgres:///postgrest_test"
|
|||||||
db-schema = "test"
|
db-schema = "test"
|
||||||
db-anon-role = "postgrest_test_anonymous"
|
db-anon-role = "postgrest_test_anonymous"
|
||||||
db-pool = 1
|
db-pool = 1
|
||||||
server-host = "*4"
|
server-host = "127.0.0.1"
|
||||||
server-port = 49421
|
server-port = 49421
|
||||||
|
|
||||||
# Read secret from a file: /dev/stdin (alias for standard input)
|
# Read secret from a file: /dev/stdin (alias for standard input)
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
db-uri = "postgres:///postgrest_test"
|
||||||
|
db-schema = "test"
|
||||||
|
db-anon-role = "postgrest_test_anonymous"
|
||||||
|
db-pool = 1
|
||||||
|
server-host = "127.0.0.1"
|
||||||
|
server-port = 49421
|
||||||
|
jwt-secret = "reallyreallyreallyreallyverysafe"
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
#!/bin/bash
|
#! /usr/bin/env bash
|
||||||
currentTest=1
|
currentTest=1
|
||||||
failedTests=0
|
failedTests=0
|
||||||
result(){ echo "$1 $currentTest $2"; currentTest=$(( $currentTest + 1 )); }
|
result(){ echo "$1 $currentTest $2"; currentTest=$(( $currentTest + 1 )); }
|
||||||
@@ -96,20 +96,20 @@ setUp
|
|||||||
|
|
||||||
echo "Running memory usage tests.."
|
echo "Running memory usage tests.."
|
||||||
|
|
||||||
jsonKeyTest "1M" "POST" "/rpc/leak" "15M"
|
jsonKeyTest "1M" "POST" "/rpc/leak" "20M"
|
||||||
jsonKeyTest "1M" "POST" "/leak" "15M"
|
jsonKeyTest "1M" "POST" "/leak" "20M"
|
||||||
jsonKeyTest "1M" "PATCH" "/leak?id=eq.1" "15M"
|
jsonKeyTest "1M" "PATCH" "/leak?id=eq.1" "20M"
|
||||||
|
|
||||||
jsonKeyTest "10M" "POST" "/rpc/leak" "105M"
|
jsonKeyTest "10M" "POST" "/rpc/leak" "105M"
|
||||||
jsonKeyTest "10M" "POST" "/leak" "105M"
|
jsonKeyTest "10M" "POST" "/leak" "105M"
|
||||||
jsonKeyTest "10M" "PATCH" "/leak?id=eq.1" "105M"
|
jsonKeyTest "10M" "PATCH" "/leak?id=eq.1" "105M"
|
||||||
|
|
||||||
jsonKeyTest "100M" "POST" "/rpc/leak" "895M"
|
jsonKeyTest "50M" "POST" "/rpc/leak" "500M"
|
||||||
jsonKeyTest "100M" "POST" "/leak" "895M"
|
jsonKeyTest "50M" "POST" "/leak" "500M"
|
||||||
jsonKeyTest "100M" "PATCH" "/leak?id=eq.1" "895M"
|
jsonKeyTest "50M" "PATCH" "/leak?id=eq.1" "500M"
|
||||||
|
|
||||||
postJsonArrayTest "1000" "/perf_articles" "20M"
|
postJsonArrayTest "1000" "/perf_articles" "20M"
|
||||||
postJsonArrayTest "10000" "/perf_articles" "120M"
|
postJsonArrayTest "10000" "/perf_articles" "150M"
|
||||||
postJsonArrayTest "100000" "/perf_articles" "1.15G"
|
postJsonArrayTest "100000" "/perf_articles" "1.15G"
|
||||||
|
|
||||||
cleanUp
|
cleanUp
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ db-uri = "postgres:///postgrest_test"
|
|||||||
db-schema = "test"
|
db-schema = "test"
|
||||||
db-anon-role = "postgrest_test_anonymous"
|
db-anon-role = "postgrest_test_anonymous"
|
||||||
db-pool = 1
|
db-pool = 1
|
||||||
server-host = "*4"
|
server-host = "127.0.0.1"
|
||||||
server-port = 49421
|
server-port = 49421
|
||||||
|
|
||||||
jwt-secret = "reallyreallyreallyreallyverysafe"
|
jwt-secret = "reallyreallyreallyreallyverysafe"
|
||||||
|
|||||||
Reference in New Issue
Block a user