From 6b04491e0441c1e8dae079ef5fc91e5010858fad Mon Sep 17 00:00:00 2001 From: steve-chavez Date: Sat, 17 Apr 2021 15:28:44 -0500 Subject: [PATCH] Filter private junctions and private FK on columns --- src/PostgREST/App.hs | 2 +- src/PostgREST/DbStructure.hs | 31 ++++++++++++++++++++----------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/PostgREST/App.hs b/src/PostgREST/App.hs index 858fda1531..fc784189bf 100644 --- a/src/PostgREST/App.hs +++ b/src/PostgREST/App.hs @@ -518,7 +518,7 @@ readRequest :: Monad m => QualifiedIdentifier -> RequestContext -> Handler m Rea readRequest QualifiedIdentifier{..} (RequestContext AppConfig{..} dbStructure apiRequest _) = liftEither $ ReqBuilder.readRequest qiSchema qiName configDbMaxRows - (dbRelations dbStructure) + (dbRelationships dbStructure) apiRequest contentTypeHeaders :: RequestContext -> [HTTP.Header] diff --git a/src/PostgREST/DbStructure.hs b/src/PostgREST/DbStructure.hs index cb3f6fe99a..a82978543c 100644 --- a/src/PostgREST/DbStructure.hs +++ b/src/PostgREST/DbStructure.hs @@ -64,12 +64,12 @@ import Protolude.Unsafe (unsafeHead) data DbStructure = DbStructure - { dbTables :: [Table] - , dbColumns :: [Column] - , dbRelations :: [Relation] - , dbPrimaryKeys :: [PrimaryKey] - , dbProcs :: ProcsMap - , pgVersion :: PgVersion + { dbTables :: [Table] + , dbColumns :: [Column] + , dbRelationships :: [Relation] + , dbPrimaryKeys :: [PrimaryKey] + , dbProcs :: ProcsMap + , pgVersion :: PgVersion } deriving (Generic, JSON.ToJSON) @@ -85,14 +85,23 @@ tablePKCols dbs tSchema tName = pkName <$> filter (\pk -> tSchema == (tableSche removeInternalSchemas :: DbStructure -> [Schema] -> DbStructure removeInternalSchemas dbStruct schemas = DbStructure { - dbTables = filter (\x -> tableSchema x `elem` schemas) $ dbTables dbStruct - , dbColumns = filter (\x -> tableSchema (colTable x) `elem` schemas) $ dbColumns dbStruct - , dbRelations = filter (\x -> tableSchema (relTable x) `elem` schemas && - tableSchema (relFTable x) `elem` schemas) $ dbRelations dbStruct + dbTables = filter (\x -> tableSchema x `elem` schemas) $ dbTables dbStruct + , dbColumns = removeInternalFK <$> filter (\x -> tableSchema (colTable x) `elem` schemas) (dbColumns dbStruct) + , dbRelationships = filter (\x -> tableSchema (relTable x) `elem` schemas && + tableSchema (relForeignTable x) `elem` schemas && + hasInternalJunction x) $ dbRelationships dbStruct , dbPrimaryKeys = filter (\x -> tableSchema (pkTable x) `elem` schemas) $ dbPrimaryKeys dbStruct , dbProcs = M.filterWithKey (\(QualifiedIdentifier sch _) _ -> sch `elem` schemas) $ dbProcs dbStruct , pgVersion = pgVersion dbStruct } + where + removeInternalFK col = case colFK col of + Just fk | tableSchema (colTable $ fkCol fk) `notElem` schemas -> col{colFK = Nothing} + | otherwise -> col + Nothing -> col + hasInternalJunction rel = case relCardinality rel of + M2M Junction{junTable} -> tableSchema junTable `elem` schemas + _ -> False -- | The source table column a view column refers to type SourceColumn = (Column, ViewColumn) @@ -118,7 +127,7 @@ getDbStructure schemas extraSearchPath pgVer prepared = do return DbStructure { dbTables = tabs , dbColumns = cols' - , dbRelations = rels + , dbRelationships = rels , dbPrimaryKeys = keys' , dbProcs = procs , pgVersion = pgVer