diff --git a/src/PostgREST/DbStructure.hs b/src/PostgREST/DbStructure.hs index 6c10d36e89..cb3f6fe99a 100644 --- a/src/PostgREST/DbStructure.hs +++ b/src/PostgREST/DbStructure.hs @@ -21,6 +21,7 @@ These queries are executed once at startup or when PostgREST is reloaded. module PostgREST.DbStructure ( DbStructure(..) , getDbStructure + , removeInternalSchemas , accessibleTables , accessibleProcs , schemaDescription @@ -80,6 +81,19 @@ tableCols dbs tSchema tName = filter (\Column{colTable=Table{tableSchema=s, tabl tablePKCols :: DbStructure -> Schema -> TableName -> [Text] tablePKCols dbs tSchema tName = pkName <$> filter (\pk -> tSchema == (tableSchema . pkTable) pk && tName == (tableName . pkTable) pk) (dbPrimaryKeys dbs) +-- | Remove internal schemas(not exposed through the API) from the DbStructure +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 + , dbPrimaryKeys = filter (\x -> tableSchema (pkTable x) `elem` schemas) $ dbPrimaryKeys dbStruct + , dbProcs = M.filterWithKey (\(QualifiedIdentifier sch _) _ -> sch `elem` schemas) $ dbProcs dbStruct + , pgVersion = pgVersion dbStruct + } + -- | The source table column a view column refers to type SourceColumn = (Column, ViewColumn) type ViewColumn = Column diff --git a/src/PostgREST/Middleware.hs b/src/PostgREST/Middleware.hs index d856196205..6c328a1aa0 100644 --- a/src/PostgREST/Middleware.hs +++ b/src/PostgREST/Middleware.hs @@ -40,7 +40,8 @@ import System.IO.Unsafe (unsafePerformIO) import System.Log.FastLogger (toLogStr) import PostgREST.Config (AppConfig (..), LogLevel (..)) -import PostgREST.DbStructure (DbStructure) +import PostgREST.DbStructure (DbStructure, + removeInternalSchemas) import PostgREST.Error (Error, errorResponseFor) import PostgREST.GucHeader (addHeadersIfNotIncluded) import PostgREST.Query.SqlFragment (fromQi, intercalateSnippet, @@ -78,7 +79,8 @@ runPgLocals conf claims app req dbs = do setConfigLocal mempty ("search_path", toS schemas) preReqSql = (\f -> "select " <> fromQi f <> "();") <$> configDbPreRequest conf specSql = case iTarget req of - TargetProc{tpIsRootSpec=True} -> [setConfigLocal mempty ("request.spec", toS $ JSON.encode dbs)] + TargetProc{tpIsRootSpec=True} -> [setConfigLocal mempty + ("request.spec", toS $ JSON.encode $ removeInternalSchemas dbs $ toList $ configDbSchemas conf)] _ -> mempty -- | Do a pg set_config(setting, value, true) call. This is equivalent to a SET LOCAL. setConfigLocal :: ByteString -> (ByteString, ByteString) -> H.Snippet @@ -183,3 +185,4 @@ optionalRollback AppConfig{..} ApiRequest{..} transaction = do [(HTTP.hPreferenceApplied, BS.pack (show Rollback))] | otherwise = identity +