detect primary keys for views (fix #217)
This commit is contained in:
@@ -110,16 +110,31 @@ columns table = do
|
|||||||
primaryKeyColumns :: QualifiedIdentifier -> H.Tx P.Postgres s [Text]
|
primaryKeyColumns :: QualifiedIdentifier -> H.Tx P.Postgres s [Text]
|
||||||
primaryKeyColumns table = do
|
primaryKeyColumns table = do
|
||||||
r <- H.listEx $ [H.stmt|
|
r <- H.listEx $ [H.stmt|
|
||||||
select kc.column_name
|
WITH table_pk AS
|
||||||
from
|
( SELECT kc.table_schema,
|
||||||
information_schema.table_constraints tc,
|
kc.table_name,
|
||||||
information_schema.key_column_usage kc
|
kc.column_name
|
||||||
where
|
FROM information_schema.table_constraints tc,
|
||||||
tc.constraint_type = 'PRIMARY KEY'
|
information_schema.key_column_usage kc
|
||||||
and kc.table_name = tc.table_name and kc.table_schema = tc.table_schema
|
WHERE tc.constraint_type = 'PRIMARY KEY'
|
||||||
and kc.constraint_name = tc.constraint_name
|
AND kc.table_name = tc.table_name
|
||||||
and kc.table_schema = ?
|
AND kc.table_schema = tc.table_schema
|
||||||
and kc.table_name = ? |] (qiSchema table) (qiName table)
|
AND kc.constraint_name = tc.constraint_name
|
||||||
|
AND kc.table_schema = ?)
|
||||||
|
SELECT column_name
|
||||||
|
FROM table_pk
|
||||||
|
WHERE table_pk.table_name = ?
|
||||||
|
UNION
|
||||||
|
(
|
||||||
|
SELECT vcu.column_name
|
||||||
|
FROM information_schema.view_column_usage AS vcu
|
||||||
|
JOIN table_pk ON table_pk.table_schema = vcu.view_schema
|
||||||
|
AND table_pk.TABLE_NAME = vcu.TABLE_NAME
|
||||||
|
AND table_pk.column_name = vcu.column_name
|
||||||
|
WHERE vcu.view_schema = ?
|
||||||
|
AND vcu.view_name = ?
|
||||||
|
)
|
||||||
|
|] (qiSchema table) (qiName table) (qiSchema table) (qiName table)
|
||||||
return $ map runIdentity r
|
return $ map runIdentity r
|
||||||
|
|
||||||
doesProcExist :: Text -> Text -> H.Tx P.Postgres s Bool
|
doesProcExist :: Text -> Text -> H.Tx P.Postgres s Bool
|
||||||
|
|||||||
@@ -145,6 +145,94 @@ spec = around withApp $ do
|
|||||||
}
|
}
|
||||||
|]
|
|]
|
||||||
|
|
||||||
|
it "it includes primary key for views" $
|
||||||
|
request methodOptions "/insertable_view_with_join" [] "" `shouldRespondWith`
|
||||||
|
[json|
|
||||||
|
{
|
||||||
|
"pkey":["id"],
|
||||||
|
"columns":[
|
||||||
|
{
|
||||||
|
"references":null,
|
||||||
|
"default":null,
|
||||||
|
"precision":64,
|
||||||
|
"updatable":false,
|
||||||
|
"schema":"1",
|
||||||
|
"name":"id",
|
||||||
|
"type":"bigint",
|
||||||
|
"maxLen":null,
|
||||||
|
"enum":[],
|
||||||
|
"nullable":true,
|
||||||
|
"position":1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"references":null,
|
||||||
|
"default":null,
|
||||||
|
"precision":32,
|
||||||
|
"updatable":false,
|
||||||
|
"schema":"1",
|
||||||
|
"name":"auto_inc_fk",
|
||||||
|
"type":"integer",
|
||||||
|
"maxLen":null,
|
||||||
|
"enum":[],
|
||||||
|
"nullable":true,
|
||||||
|
"position":2
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"references":null,
|
||||||
|
"default":null,
|
||||||
|
"precision":null,
|
||||||
|
"updatable":false,
|
||||||
|
"schema":"1",
|
||||||
|
"name":"simple_fk",
|
||||||
|
"type":"character varying",
|
||||||
|
"maxLen":255,
|
||||||
|
"enum":[],
|
||||||
|
"nullable":true,
|
||||||
|
"position":3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"references":null,
|
||||||
|
"default":null,
|
||||||
|
"precision":null,
|
||||||
|
"updatable":false,
|
||||||
|
"schema":"1",
|
||||||
|
"name":"nullable_string",
|
||||||
|
"type":"character varying",
|
||||||
|
"maxLen":null,
|
||||||
|
"enum":[],
|
||||||
|
"nullable":true,
|
||||||
|
"position":4
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"references":null,
|
||||||
|
"default":null,
|
||||||
|
"precision":null,
|
||||||
|
"updatable":false,
|
||||||
|
"schema":"1",
|
||||||
|
"name":"non_nullable_string",
|
||||||
|
"type":"character varying",
|
||||||
|
"maxLen":null,
|
||||||
|
"enum":[],
|
||||||
|
"nullable":true,
|
||||||
|
"position":5
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"references":null,
|
||||||
|
"default":null,
|
||||||
|
"precision":null,
|
||||||
|
"updatable":false,
|
||||||
|
"schema":"1",
|
||||||
|
"name":"inserted_at",
|
||||||
|
"type":"timestamp with time zone",
|
||||||
|
"maxLen":null,
|
||||||
|
"enum":[],
|
||||||
|
"nullable":true,
|
||||||
|
"position":6
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|]
|
||||||
|
|
||||||
it "includes foreign key data" $ do
|
it "includes foreign key data" $ do
|
||||||
pendingWith "have to resolve issue #107"
|
pendingWith "have to resolve issue #107"
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user