feat: request.spec GUC for db-root-spec

The request.spec GUC contains the schema cache structure in json.

It's only available when the root endpoint(/) is requested and when
db-root-spec is not empty.

Also correct db-root-spec to accept a schema.
This commit is contained in:
steve-chavez
2021-05-30 16:48:08 -05:00
committed by Steve Chavez
parent f169661ce6
commit d99909c403
11 changed files with 69 additions and 50 deletions
+5 -1
View File
@@ -26,5 +26,9 @@ spec =
it "accepts application/json" $
request methodGet "/"
[("Accept", "application/json")] "" `shouldRespondWith`
[json| [{"table": "items"}, {"table": "subitems"}] |]
[json| {
"tableName": "orders_view", "tableSchema": "test",
"tableDeletable": true, "tableUpdatable": true,
"tableInsertable": true, "tableDescription": null
} |]
{ matchHeaders = [matchContentTypeJson] }
+11 -4
View File
@@ -1,5 +1,6 @@
module Main where
import qualified Data.Aeson as JSON
import qualified Hasql.Pool as P
import qualified Hasql.Transaction.Sessions as HT
@@ -68,19 +69,25 @@ main = do
let
-- For tests that run with the same refDbStructure
app cfg = do
appState <- AppState.initWithPool pool $ cfg testDbConn
let config = cfg testDbConn
appState <- AppState.initWithPool pool config
AppState.putPgVersion appState actualPgVersion
AppState.putDbStructure appState baseDbStructure
when (isJust $ configDbRootSpec config) $
AppState.putJsonDbS appState $ toS $ JSON.encode baseDbStructure
return ((), postgrest LogCrit appState $ pure ())
-- For tests that run with a different DbStructure(depends on configSchemas)
appDbs cfg = do
let config = cfg testDbConn
customDbStructure <-
loadDbStructure pool
(configDbSchemas $ cfg testDbConn)
(configDbExtraSearchPath $ cfg testDbConn)
appState <- AppState.initWithPool pool $ cfg testDbConn
(configDbSchemas config)
(configDbExtraSearchPath config)
appState <- AppState.initWithPool pool config
AppState.putDbStructure appState customDbStructure
when (isJust $ configDbRootSpec config) $
AppState.putJsonDbS appState $ toS $ JSON.encode baseDbStructure
return ((), postgrest LogCrit appState $ pure ())
let withApp = app testCfg
+1 -1
View File
@@ -165,7 +165,7 @@ testCfgExtraSearchPath :: Text -> AppConfig
testCfgExtraSearchPath testDbConn = (testCfg testDbConn) { configDbExtraSearchPath = ["public", "extensions"] }
testCfgRootSpec :: Text -> AppConfig
testCfgRootSpec testDbConn = (testCfg testDbConn) { configDbRootSpec = Just "root"}
testCfgRootSpec testDbConn = (testCfg testDbConn) { configDbRootSpec = Just $ QualifiedIdentifier mempty "root"}
testCfgHtmlRawOutput :: Text -> AppConfig
testCfgHtmlRawOutput testDbConn = (testCfg testDbConn) { configRawMediaTypes = ["text/html"] }
+3 -13
View File
@@ -1713,9 +1713,9 @@ returns integer as $$
select a + b;
$$ language sql;
create function root() returns jsonb as $_$
create or replace function root() returns json as $_$
declare
openapi jsonb = $$
openapi json = $$
{
"swagger": "2.0",
"info":{
@@ -1724,22 +1724,12 @@ openapi jsonb = $$
}
}
$$;
simple jsonb = $$
[
{
"table":"items"
},
{
"table":"subitems"
}
]
$$;
begin
case current_setting('request.header.accept', true)
when 'application/openapi+json' then
return openapi;
when 'application/json' then
return simple;
return (current_setting('request.spec', true)::json)->'dbRelationships'->0->'relTable';
else
return openapi;
end case;