Skip to content

Commit

Permalink
fix: Dump media handlers and timezones with --dump-schema
Browse files Browse the repository at this point in the history
Those were left out of the schema dump when the features were introduced, probably
because ByteString doesn't have a toJSON instance. Changing the type to Text solves
this easily.

Resolves #3237
  • Loading branch information
wolfgangwalther committed Feb 19, 2024
1 parent 4a796e0 commit 6bc6967
Show file tree
Hide file tree
Showing 9 changed files with 1,802 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- #3205, Fix wrong subquery error returning a status of 400 Bad Request - @steve-chavez
- #3224, Return status code 406 for non-accepted media type instead of code 415 - @wolfgangwalther
- #3160, Fix using select= query parameter for custom media type handlers - @wolfgangwalther
- #3237, Dump media handlers and timezones with --dump-schema - @wolfgangwalther

### Deprecated

Expand Down
1 change: 1 addition & 0 deletions nix/tools/tests.nix
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ let
ps.pyyaml
ps.requests
ps.requests-unixsocket
ps.syrupy
]);

testIO =
Expand Down
2 changes: 1 addition & 1 deletion src/PostgREST/ApiRequest/Preferences.hs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ fromHeaders allowTxDbOverride acceptedTzNames headers =
listStripPrefix prefix prefList = listToMaybe $ mapMaybe (BS.stripPrefix prefix) prefList

timezonePref = listStripPrefix "timezone=" prefs
isTimezonePrefAccepted = (S.member <$> timezonePref <*> pure acceptedTzNames) == Just True
isTimezonePrefAccepted = (S.member <$> (decodeUtf8 <$> timezonePref) <*> pure acceptedTzNames) == Just True

maxAffectedPref = listStripPrefix "max-affected=" prefs >>= readMaybe . BS.unpack

Expand Down
2 changes: 1 addition & 1 deletion src/PostgREST/Config/Database.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import Protolude

type RoleSettings = (HM.HashMap ByteString (HM.HashMap ByteString ByteString))
type RoleIsolationLvl = HM.HashMap ByteString SQL.IsolationLevel
type TimezoneNames = Set ByteString -- cache timezone names for prefer timezone=
type TimezoneNames = Set Text -- cache timezone names for prefer timezone=

toIsolationLevel :: (Eq a, IsString a) => a -> SQL.IsolationLevel
toIsolationLevel a = case a of
Expand Down
14 changes: 8 additions & 6 deletions src/PostgREST/MediaType.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-}

Expand All @@ -10,6 +11,7 @@ module PostgREST.MediaType
, decodeMediaType
) where

import qualified Data.Aeson as JSON
import qualified Data.ByteString as BS
import qualified Data.ByteString.Internal as BS (c2w)

Expand All @@ -28,23 +30,23 @@ data MediaType
| MTUrlEncoded
| MTOctetStream
| MTAny
| MTOther ByteString
| MTOther Text
-- vendored media types
| MTVndArrayJSONStrip
| MTVndSingularJSON Bool
-- TODO MTVndPlan should only have its options as [Text]. Its ResultAggregate should have the typed attributes.
| MTVndPlan MediaType MTVndPlanFormat [MTVndPlanOption]
deriving (Eq, Show, Generic)
deriving (Eq, Show, Generic, JSON.ToJSON)
instance Hashable MediaType

data MTVndPlanOption
= PlanAnalyze | PlanVerbose | PlanSettings | PlanBuffers | PlanWAL
deriving (Eq, Show, Generic)
deriving (Eq, Show, Generic, JSON.ToJSON)
instance Hashable MTVndPlanOption

data MTVndPlanFormat
= PlanJSON | PlanText
deriving (Eq, Show, Generic)
deriving (Eq, Show, Generic, JSON.ToJSON)
instance Hashable MTVndPlanFormat

-- | Convert MediaType to a Content-Type HTTP Header
Expand All @@ -70,7 +72,7 @@ toMime (MTVndSingularJSON False) = "application/vnd.pgrst.object+json"
toMime MTUrlEncoded = "application/x-www-form-urlencoded"
toMime MTOctetStream = "application/octet-stream"
toMime MTAny = "*/*"
toMime (MTOther ct) = ct
toMime (MTOther ct) = encodeUtf8 ct
toMime (MTVndPlan mt fmt opts) =
"application/vnd.pgrst.plan+" <> toMimePlanFormat fmt <>
("; for=\"" <> toMime mt <> "\"") <>
Expand Down Expand Up @@ -132,7 +134,7 @@ decodeMediaType mt =
"application/vnd.pgrst.array+json":rest -> checkArrayNullStrip rest
"application/vnd.pgrst.array":rest -> checkArrayNullStrip rest
"*/*":_ -> MTAny
other:_ -> MTOther other
other:_ -> MTOther $ decodeUtf8 other
_ -> MTAny
where
checkArrayNullStrip ["nulls=stripped"] = MTVndArrayJSONStrip
Expand Down
9 changes: 4 additions & 5 deletions src/PostgREST/SchemaCache.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import Control.Monad.Extra (whenJust)

import Data.Aeson ((.=))
import qualified Data.Aeson as JSON
import qualified Data.Aeson.Types as JSON
import qualified Data.HashMap.Strict as HM
import qualified Data.HashMap.Strict.InsOrd as HMI
import qualified Data.Set as S
Expand Down Expand Up @@ -86,13 +85,13 @@ data SchemaCache = SchemaCache
}

instance JSON.ToJSON SchemaCache where
toJSON (SchemaCache tabs rels routs reps _ _) = JSON.object [
toJSON (SchemaCache tabs rels routs reps hdlers tzs) = JSON.object [
"dbTables" .= JSON.toJSON tabs
, "dbRelationships" .= JSON.toJSON rels
, "dbRoutines" .= JSON.toJSON routs
, "dbRepresentations" .= JSON.toJSON reps
, "dbMediaHandlers" .= JSON.emptyArray
, "dbTimezones" .= JSON.emptyArray
, "dbMediaHandlers" .= JSON.toJSON hdlers
, "dbTimezones" .= JSON.toJSON tzs
]

showSummary :: SchemaCache -> Text
Expand Down Expand Up @@ -1222,7 +1221,7 @@ timezones = SQL.Statement sql HE.noParams decodeTimezones
where
sql = "SELECT name FROM pg_timezone_names"
decodeTimezones :: HD.Result TimezoneNames
decodeTimezones = S.fromList . map encodeUtf8 <$> HD.rowList (column HD.text)
decodeTimezones = S.fromList <$> HD.rowList (column HD.text)

param :: HE.Value a -> HE.Params a
param = HE.param . HE.nonNullable
Expand Down
2 changes: 1 addition & 1 deletion src/PostgREST/SchemaCache/Routine.hs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ data MediaHandler
-- custom
| CustomFunc QualifiedIdentifier RelIdentifier
| NoAgg
deriving (Eq, Show)
deriving (Eq, Show, Generic, JSON.ToJSON)

funcReturnsSingle :: Routine -> Bool
funcReturnsSingle proc = case proc of
Expand Down
Loading

0 comments on commit 6bc6967

Please sign in to comment.