Skip to content

Commit

Permalink
Filter private junctions and private FK on columns
Browse files Browse the repository at this point in the history
  • Loading branch information
steve-chavez committed Apr 17, 2021
1 parent ee13150 commit 6b04491
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/PostgREST/App.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
31 changes: 20 additions & 11 deletions src/PostgREST/DbStructure.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 6b04491

Please sign in to comment.