emit references for columns with foreign keys
This commit is contained in:
@@ -36,6 +36,9 @@ data ForeignKey = ForeignKey {
|
|||||||
fkTable::String, fkCol::String
|
fkTable::String, fkCol::String
|
||||||
} deriving (Eq, Show)
|
} deriving (Eq, Show)
|
||||||
|
|
||||||
|
instance JSON.ToJSON ForeignKey where
|
||||||
|
toJSON fk = JSON.object ["table".=fkTable fk, "column".=fkCol fk]
|
||||||
|
|
||||||
foreignKeys :: String -> String -> Connection -> IO (Map.Map String ForeignKey)
|
foreignKeys :: String -> String -> Connection -> IO (Map.Map String ForeignKey)
|
||||||
foreignKeys schema table conn = do
|
foreignKeys schema table conn = do
|
||||||
r <- quickQuery conn
|
r <- quickQuery conn
|
||||||
@@ -78,6 +81,7 @@ instance JSON.ToJSON Column where
|
|||||||
, "updatable" .= colUpdatable c
|
, "updatable" .= colUpdatable c
|
||||||
, "maxLen" .= colMaxLen c
|
, "maxLen" .= colMaxLen c
|
||||||
, "precision" .= colPrecision c
|
, "precision" .= colPrecision c
|
||||||
|
, "references".= colFK c
|
||||||
, "default" .= colDefault c ]
|
, "default" .= colDefault c ]
|
||||||
|
|
||||||
data TableOptions = TableOptions {
|
data TableOptions = TableOptions {
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
{-# OPTIONS_GHC -fno-warn-missing-signatures #-}
|
||||||
{-# LANGUAGE OverloadedStrings, QuasiQuotes #-}
|
{-# LANGUAGE OverloadedStrings, QuasiQuotes #-}
|
||||||
module Feature.StructureSpec where
|
module Feature.StructureSpec where
|
||||||
|
|
||||||
@@ -8,15 +9,26 @@ import Test.Hspec.Wai.JSON
|
|||||||
import SpecHelper
|
import SpecHelper
|
||||||
|
|
||||||
import Network.HTTP.Types
|
import Network.HTTP.Types
|
||||||
|
import Codec.Binary.Base64.String (encode)
|
||||||
|
import Data.Monoid ((<>))
|
||||||
|
import Data.String.Conversions (cs)
|
||||||
|
|
||||||
|
uName = "a user"
|
||||||
|
uPass = "nobody can ever know"
|
||||||
|
uRole = "dbapi_test"
|
||||||
|
|
||||||
spec :: Spec
|
spec :: Spec
|
||||||
spec = around appWithFixture $ do
|
spec = around withDatabaseConnection $
|
||||||
|
aroundWith (withUser uName uPass uRole) $ aroundWith withApp $ do
|
||||||
describe "GET /" $
|
describe "GET /" $
|
||||||
it "lists views in schema" $
|
it "lists views in schema" $
|
||||||
get "/" `shouldRespondWith`
|
request methodGet "/"
|
||||||
[json| [
|
[("Authorization", "Basic "<>(cs.encode $ cs uName<>":"<>cs uPass))] ""
|
||||||
{"schema":"1","name":"auto_incrementing_pk","insertable":true}
|
`shouldRespondWith` [json| [
|
||||||
|
{"schema":"1","name":"authors_only","insertable":true}
|
||||||
|
, {"schema":"1","name":"auto_incrementing_pk","insertable":true}
|
||||||
, {"schema":"1","name":"compound_pk","insertable":true}
|
, {"schema":"1","name":"compound_pk","insertable":true}
|
||||||
|
, {"schema":"1","name":"has_fk","insertable":true}
|
||||||
, {"schema":"1","name":"items","insertable":true}
|
, {"schema":"1","name":"items","insertable":true}
|
||||||
, {"schema":"1","name":"menagerie","insertable":true}
|
, {"schema":"1","name":"menagerie","insertable":true}
|
||||||
, {"schema":"1","name":"no_pk","insertable":true}
|
, {"schema":"1","name":"no_pk","insertable":true}
|
||||||
@@ -24,7 +36,7 @@ spec = around appWithFixture $ do
|
|||||||
] |]
|
] |]
|
||||||
{matchStatus = 200}
|
{matchStatus = 200}
|
||||||
|
|
||||||
describe "Table info" $
|
describe "Table info" $ do
|
||||||
it "is available with OPTIONS verb" $
|
it "is available with OPTIONS verb" $
|
||||||
request methodOptions "/auto_incrementing_pk" [] "" `shouldRespondWith` [json|
|
request methodOptions "/auto_incrementing_pk" [] "" `shouldRespondWith` [json|
|
||||||
{
|
{
|
||||||
@@ -39,6 +51,7 @@ spec = around appWithFixture $ do
|
|||||||
"maxLen": null,
|
"maxLen": null,
|
||||||
"nullable": false,
|
"nullable": false,
|
||||||
"position": 1,
|
"position": 1,
|
||||||
|
"references": null,
|
||||||
"default": "nextval('\"1\".auto_incrementing_pk_id_seq'::regclass)"
|
"default": "nextval('\"1\".auto_incrementing_pk_id_seq'::regclass)"
|
||||||
}, {
|
}, {
|
||||||
"precision": null,
|
"precision": null,
|
||||||
@@ -49,6 +62,7 @@ spec = around appWithFixture $ do
|
|||||||
"maxLen": null,
|
"maxLen": null,
|
||||||
"nullable": true,
|
"nullable": true,
|
||||||
"position": 2,
|
"position": 2,
|
||||||
|
"references": null,
|
||||||
"default": null
|
"default": null
|
||||||
}, {
|
}, {
|
||||||
"precision": null,
|
"precision": null,
|
||||||
@@ -59,6 +73,7 @@ spec = around appWithFixture $ do
|
|||||||
"maxLen": null,
|
"maxLen": null,
|
||||||
"nullable": false,
|
"nullable": false,
|
||||||
"position": 3,
|
"position": 3,
|
||||||
|
"references": null,
|
||||||
"default": null
|
"default": null
|
||||||
}, {
|
}, {
|
||||||
"precision": null,
|
"precision": null,
|
||||||
@@ -69,8 +84,54 @@ spec = around appWithFixture $ do
|
|||||||
"maxLen": null,
|
"maxLen": null,
|
||||||
"nullable": true,
|
"nullable": true,
|
||||||
"position": 4,
|
"position": 4,
|
||||||
|
"references": null,
|
||||||
"default": "now()"
|
"default": "now()"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|]
|
|]
|
||||||
|
|
||||||
|
it "includes foreign key data" $
|
||||||
|
request methodOptions "/has_fk"
|
||||||
|
[("Authorization", "Basic "<>(cs.encode $ cs uName<>":"<>cs uPass))] ""
|
||||||
|
`shouldRespondWith` [json|
|
||||||
|
{
|
||||||
|
"pkey": ["id"],
|
||||||
|
"columns":[
|
||||||
|
{
|
||||||
|
"default": "nextval('\"1\".has_fk_id_seq'::regclass)",
|
||||||
|
"precision": 64,
|
||||||
|
"updatable": true,
|
||||||
|
"schema": "1",
|
||||||
|
"name": "id",
|
||||||
|
"type": "bigint",
|
||||||
|
"maxLen": null,
|
||||||
|
"nullable": false,
|
||||||
|
"position": 1,
|
||||||
|
"references": null
|
||||||
|
}, {
|
||||||
|
"default": null,
|
||||||
|
"precision": 32,
|
||||||
|
"updatable": true,
|
||||||
|
"schema": "1",
|
||||||
|
"name": "auto_inc_fk",
|
||||||
|
"type": "integer",
|
||||||
|
"maxLen": null,
|
||||||
|
"nullable": true,
|
||||||
|
"position": 2,
|
||||||
|
"references": {"table": "auto_incrementing_pk", "column": "id"}
|
||||||
|
}, {
|
||||||
|
"default": null,
|
||||||
|
"precision": null,
|
||||||
|
"updatable": true,
|
||||||
|
"schema": "1",
|
||||||
|
"name": "simple_fk",
|
||||||
|
"type": "character varying",
|
||||||
|
"maxLen": 255,
|
||||||
|
"nullable": true,
|
||||||
|
"position": 3,
|
||||||
|
"references": {"table": "simple_pk", "column": "k"}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|]
|
||||||
|
|||||||
Reference in New Issue
Block a user