Replace BackendSourceMetadata type alias with a newtype

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5654
GitOrigin-RevId: 89697c8838a7cd6d6de7a2cced5c651bac63fc63
This commit is contained in:
Solomon 2022-08-28 17:58:03 -07:00 committed by hasura-bot
parent 18f9bf481c
commit 033b00d091
8 changed files with 59 additions and 55 deletions

View File

@ -17,7 +17,7 @@ module Hasura.RQL.DDL.Metadata
)
where
import Control.Lens ((.~), (^.), (^?))
import Control.Lens (to, (.~), (^.), (^?))
import Control.Monad.Trans.Control (MonadBaseControl)
import Data.Aeson qualified as J
import Data.Aeson.Ordered qualified as AO
@ -96,7 +96,7 @@ runClearMetadata _ = do
metadata <- getMetadata
-- Clean up all sources, drop hdb_catalog schema from source
for_ (OMap.toList $ _metaSources metadata) $ \(sourceName, backendSourceMetadata) ->
AB.dispatchAnyBackend @BackendMetadata backendSourceMetadata \(_sourceMetadata :: SourceMetadata b) -> do
AB.dispatchAnyBackend @BackendMetadata (unBackendSourceMetadata backendSourceMetadata) \(_sourceMetadata :: SourceMetadata b) -> do
sourceInfo <- askSourceInfo @b sourceName
-- We do not bother dropping all dependencies on the source, because the
-- metadata is going to be replaced with an empty metadata. And dropping the
@ -108,7 +108,7 @@ runClearMetadata _ = do
-- We can infer whether the server is started with `--database-url` option
-- (or corresponding env variable) by checking the existence of @'defaultSource'
-- in current metadata.
let maybeDefaultSourceMetadata = metadata ^? metaSources . ix defaultSource
let maybeDefaultSourceMetadata = metadata ^? metaSources . ix defaultSource . to unBackendSourceMetadata
emptyMetadata' = case maybeDefaultSourceMetadata of
Nothing -> emptyMetadata
Just exists ->
@ -116,16 +116,17 @@ runClearMetadata _ = do
-- which contains only default source without any tables and functions.
let emptyDefaultSource =
AB.dispatchAnyBackend @Backend exists \(s :: SourceMetadata b) ->
AB.mkAnyBackend @b $
SourceMetadata
@b
defaultSource
(_smKind @b s)
mempty
mempty
(_smConfiguration @b s)
Nothing
emptySourceCustomization
BackendSourceMetadata $
AB.mkAnyBackend @b $
SourceMetadata
@b
defaultSource
(_smKind @b s)
mempty
mempty
(_smConfiguration @b s)
Nothing
emptySourceCustomization
in emptyMetadata
& metaSources %~ OMap.insert defaultSource emptyDefaultSource
runReplaceMetadataV1 $ RMWithSources emptyMetadata'
@ -205,11 +206,12 @@ runReplaceMetadataV2 ReplaceMetadataV2 {..} = do
onNothing maybeDefaultSourceMetadata $
throw400 NotSupported "cannot import metadata without sources since no default source is defined"
let newDefaultSourceMetadata =
AB.mkAnyBackend
defaultSourceMetadata
{ _smTables = _mnsTables,
_smFunctions = _mnsFunctions
}
BackendSourceMetadata $
AB.mkAnyBackend
defaultSourceMetadata
{ _smTables = _mnsTables,
_smFunctions = _mnsFunctions
}
pure $
Metadata
(OMap.singleton defaultSource newDefaultSourceMetadata)
@ -328,7 +330,7 @@ runReplaceMetadataV2 ReplaceMetadataV2 {..} = do
-- using `DROP IF EXISTS..` meaning this silently fails without throwing an error.
for_ (OMap.toList newSources) $ \(source, newBackendSourceMetadata) -> do
onJust (OMap.lookup source oldSources) $ \oldBackendSourceMetadata ->
compose source newBackendSourceMetadata oldBackendSourceMetadata \(newSourceMetadata :: SourceMetadata b) -> do
compose source (unBackendSourceMetadata newBackendSourceMetadata) (unBackendSourceMetadata oldBackendSourceMetadata) \(newSourceMetadata :: SourceMetadata b) -> do
dispatch oldBackendSourceMetadata \oldSourceMetadata -> do
let oldTriggersMap = getTriggersMap oldSourceMetadata
newTriggersMap = getTriggersMap newSourceMetadata
@ -373,7 +375,7 @@ runReplaceMetadataV2 ReplaceMetadataV2 {..} = do
m ()
compose sourceName x y f = AB.composeAnyBackend @BackendEventTrigger f x y (logger $ HL.UnstructuredLog HL.LevelInfo $ SB.fromText $ "Event trigger clean up couldn't be done on the source " <> sourceName <<> " because it has changed its type")
dispatch = AB.dispatchAnyBackend @BackendEventTrigger
dispatch (BackendSourceMetadata bs) = AB.dispatchAnyBackend @BackendEventTrigger bs
-- | Only includes the cron triggers with `included_in_metadata` set to `True`
processCronTriggersMetadata :: Metadata -> Metadata

View File

@ -48,12 +48,12 @@ runSetQueryTagsConfig (SetQueryTagsConfig sourceName queryTagsConfig) = do
Just exists -> do
let backendType = getBackendType exists
case backendType of
Postgres _ -> setQueryTagsConfigInMetadata exists (Just queryTagsConfig)
Postgres _ -> setQueryTagsConfigInMetadata (unBackendSourceMetadata exists) (Just queryTagsConfig)
_ -> queryTagsNotSupported backendType
where
getBackendType :: BackendSourceMetadata -> BackendType
getBackendType exists =
AB.dispatchAnyBackend @Backend exists $ \(_sourceMetadata :: SourceMetadata b) ->
AB.dispatchAnyBackend @Backend (unBackendSourceMetadata exists) $ \(_sourceMetadata :: SourceMetadata b) ->
reify $ backendTag @b
setQueryTagsConfigInMetadata exists qtConfig = do

View File

@ -610,7 +610,7 @@ buildSchemaCacheRule logger env = proc (metadata, invalidationKeys) -> do
sourceRoles =
HS.fromList $
concat $
OMap.elems sources >>= \e ->
OMap.elems sources >>= \(BackendSourceMetadata e) ->
AB.dispatchAnyBackend @Backend e \(SourceMetadata _ _ tables _functions _ _ _) -> do
table <- OMap.elems tables
pure $
@ -1335,11 +1335,11 @@ instance (Backend b) => Inc.Cacheable (BackendConfigAndSourceMetadata b)
joinBackendConfigsToSources ::
BackendMap BackendConfigWrapper ->
InsOrdHashMap SourceName (AB.AnyBackend SourceMetadata) ->
InsOrdHashMap SourceName BackendSourceMetadata ->
InsOrdHashMap SourceName (AB.AnyBackend BackendConfigAndSourceMetadata)
joinBackendConfigsToSources backendConfigs sources =
flip OMap.map sources $ \abSourceMetadata ->
AB.dispatchAnyBackend @Backend abSourceMetadata $ \(sourceMetadata :: SourceMetadata b) ->
AB.dispatchAnyBackend @Backend (unBackendSourceMetadata abSourceMetadata) $ \(sourceMetadata :: SourceMetadata b) ->
let _bcasmBackendConfig = maybe mempty unBackendConfigWrapper (BackendMap.lookup @b backendConfigs)
_bcasmSourceMetadata = sourceMetadata
in AB.mkAnyBackend @b BackendConfigAndSourceMetadata {..}

View File

@ -128,11 +128,8 @@ runRenameSource RenameSource {..} = do
renameBackendSourceMetadata oldKey newKey m =
case OMap.lookup oldKey m of
Just val ->
OMap.insert
newKey
(AB.mapBackend val (renameSource newKey))
. OMap.delete oldKey
$ m
let renamedSource = BackendSourceMetadata (AB.mapBackend (unBackendSourceMetadata val) (renameSource newKey))
in OMap.insert newKey renamedSource $ OMap.delete oldKey $ m
Nothing -> m
renameSource :: forall b. SourceName -> SourceMetadata b -> SourceMetadata b

View File

@ -169,13 +169,13 @@ instance FromJSON Metadata where
network
backendConfigs
where
parseSourceMetadata :: Value -> Parser (AB.AnyBackend SourceMetadata)
parseSourceMetadata :: Value -> Parser BackendSourceMetadata
parseSourceMetadata = withObject "SourceMetadata" \o -> do
backendSourceKind <- explicitParseFieldMaybe AB.parseBackendSourceKindFromJSON o "kind" .!= AB.mkAnyBackend PostgresVanillaKind
AB.dispatchAnyBackend @Backend
backendSourceKind
( \(kind :: BackendSourceKind b) ->
AB.mkAnyBackend @b <$> parseJSONWithContext kind (Object o)
BackendSourceMetadata . AB.mkAnyBackend @b <$> parseJSONWithContext kind (Object o)
)
emptyMetadata :: Metadata

View File

@ -8,7 +8,7 @@
module Hasura.RQL.Types.Metadata.Common
( Actions,
BackendConfigWrapper (..),
BackendSourceMetadata,
BackendSourceMetadata (..),
CatalogState (..),
CatalogStateType (..),
ComputedFieldMetadata (..),
@ -400,6 +400,7 @@ type CronTriggers = InsOrdHashMap TriggerName CronTriggerMetadata
type InheritedRoles = InsOrdHashMap RoleName InheritedRole
-- | Source configuration for a source of backend type @b@ as stored in the Metadata DB.
data SourceMetadata b = SourceMetadata
{ _smName :: SourceName,
_smKind :: BackendSourceKind b,
@ -456,18 +457,18 @@ backendSourceMetadataCodec =
else Nothing
runBackendType :: BackendSourceMetadata -> BackendType
runBackendType input = AB.runBackend input \sourceMeta ->
runBackendType (BackendSourceMetadata input) = AB.runBackend input \sourceMeta ->
backendTypeFromBackendSourceKind $ _smKind sourceMeta
anySourceMetadataCodec :: (HasTag b) => JSONCodec (SourceMetadata b) -> JSONCodec BackendSourceMetadata
anySourceMetadataCodec = dimapCodec dec enc
where
dec :: HasTag b => SourceMetadata b -> BackendSourceMetadata
dec = AB.mkAnyBackend
dec = BackendSourceMetadata . AB.mkAnyBackend
-- This encoding function is partial, but that should be ok.
enc :: HasTag b => BackendSourceMetadata -> SourceMetadata b
enc input = fromJust $ AB.unpackAnyBackend input
enc input = fromJust $ AB.unpackAnyBackend $ unBackendSourceMetadata input
instance Backend b => HasCodec (SourceMetadata b) where
codec =
@ -492,24 +493,27 @@ mkSourceMetadata ::
SourceCustomization ->
BackendSourceMetadata
mkSourceMetadata name backendSourceKind config customization =
AB.mkAnyBackend $
SourceMetadata
@b
name
backendSourceKind
mempty
mempty
config
Nothing
customization
BackendSourceMetadata $
AB.mkAnyBackend $
SourceMetadata
@b
name
backendSourceKind
mempty
mempty
config
Nothing
customization
type BackendSourceMetadata = AB.AnyBackend SourceMetadata
-- | Source configuration as stored in the Metadata DB for some existentialized backend.
newtype BackendSourceMetadata = BackendSourceMetadata {unBackendSourceMetadata :: AB.AnyBackend SourceMetadata}
deriving newtype (Eq, Show)
toSourceMetadata :: forall b. (Backend b) => Prism' BackendSourceMetadata (SourceMetadata b)
toSourceMetadata = prism' AB.mkAnyBackend AB.unpackAnyBackend
toSourceMetadata = prism' (BackendSourceMetadata . AB.mkAnyBackend) (AB.unpackAnyBackend . unBackendSourceMetadata)
getSourceName :: BackendSourceMetadata -> SourceName
getSourceName e = AB.dispatchAnyBackend @Backend e _smName
getSourceName e = AB.dispatchAnyBackend @Backend (unBackendSourceMetadata e) _smName
type Sources = InsOrdHashMap SourceName BackendSourceMetadata

View File

@ -56,7 +56,7 @@ import Hasura.RQL.Types.GraphqlSchemaIntrospection (SetGraphqlIntrospectionOptio
import Hasura.RQL.Types.Metadata.Common
( Actions,
BackendConfigWrapper (..),
BackendSourceMetadata,
BackendSourceMetadata (..),
ComputedFieldMetadata (..),
CronTriggers,
Endpoints,
@ -103,7 +103,7 @@ sourcesToOrdJSONList sources =
map sourceMetaToOrdJSON $ sortOn getSourceName $ OM.elems sources
where
sourceMetaToOrdJSON :: BackendSourceMetadata -> AO.Value
sourceMetaToOrdJSON exists =
sourceMetaToOrdJSON (BackendSourceMetadata exists) =
AB.dispatchAnyBackend @Backend exists $ \(SourceMetadata {..} :: SourceMetadata b) ->
let sourceNamePair = ("name", AO.toOrdered _smName)
sourceKindPair = ("kind", AO.toOrdered _smKind)

View File

@ -159,7 +159,7 @@ migrateCatalog maybeDefaultSourceConfig extensionsSchema maintenanceMode migrati
defaultSourceConfig
Nothing
emptySourceCustomization
sources = OMap.singleton defaultSource defaultSourceMetadata
sources = OMap.singleton defaultSource $ BackendSourceMetadata defaultSourceMetadata
in emptyMetadata {_metaSources = sources}
liftTx $ insertMetadataInCatalog emptyMetadata'
@ -326,8 +326,9 @@ migrations maybeDefaultSourceConfig dryRun maintenanceMode =
let metadataV3 =
let MetadataNoSources {..} = metadataV2
defaultSourceMetadata =
AB.mkAnyBackend $
SourceMetadata defaultSource PostgresVanillaKind _mnsTables _mnsFunctions defaultSourceConfig Nothing emptySourceCustomization
BackendSourceMetadata $
AB.mkAnyBackend $
SourceMetadata defaultSource PostgresVanillaKind _mnsTables _mnsFunctions defaultSourceConfig Nothing emptySourceCustomization
in Metadata
(OMap.singleton defaultSource defaultSourceMetadata)
_mnsRemoteSchemas
@ -356,7 +357,7 @@ migrations maybeDefaultSourceConfig dryRun maintenanceMode =
MetadataNoSources mempty mempty mempty mempty mempty emptyCustomTypes mempty mempty
metadataV2 <- case OMap.toList _metaSources of
[] -> pure emptyMetadataNoSources
[(_, exists)] ->
[(_, BackendSourceMetadata exists)] ->
pure $ case AB.unpackAnyBackend exists of
Nothing -> emptyMetadataNoSources
Just SourceMetadata {..} ->