server: allow replacing configuration in add source metadata API (#956)

GitOrigin-RevId: a797d6f1f1e568eeab6acd752d3404a30b890461
This commit is contained in:
Rakesh Emmadi 2021-03-30 15:39:29 +05:30 committed by hasura-bot
parent 99cb846911
commit 7798b9c870
4 changed files with 25 additions and 11 deletions

View File

@ -3,6 +3,7 @@
## Next release
(Add entries here in the order of: server, console, cli, docs, others)
- server: add 'replace_configuration' option (default: false) in the add source API payload
- server: add a comment field for actions (#231)
- server: accept GeoJSON for MSSQL geometry and geography operators (#787)
- console: add a comment field for actions (#231)

View File

@ -76,6 +76,11 @@ Args syntax
- true
- :ref:`PGConfiguration <PGConfiguration>`
- Database connection configuration
* - replace_configuration
- false
- Boolean
- If set to ``true`` the configuration will be replaced if the source with
given name already exists (default: ``false``)
.. _pg_drop_source:
@ -115,4 +120,3 @@ Args syntax
- true
- :ref:`SourceName <SourceName>`
- Name of the Postgres database

View File

@ -7,7 +7,7 @@ import qualified Data.HashMap.Strict as HM
import qualified Data.HashMap.Strict.InsOrd as OMap
import qualified Database.PG.Query as Q
import Control.Lens (at, (^.))
import Control.Lens (at, (.~), (^.))
import Control.Monad.Trans.Control (MonadBaseControl)
import Data.Text.Extended
@ -40,13 +40,17 @@ runAddSource
:: forall m b
. (MonadError QErr m, CacheRWM m, MetadataM m, BackendMetadata b)
=> AddSource b -> m EncJSON
runAddSource (AddSource name sourceConfig) = do
runAddSource (AddSource name sourceConfig replaceConfiguration) = do
sources <- scSources <$> askSchemaCache
onJust (HM.lookup name sources) $ const $
throw400 AlreadyExists $ "source with name " <> name <<> " already exists"
buildSchemaCacheFor (MOSource name)
$ MetadataModifier
$ metaSources %~ OMap.insert name (mkSourceMetadata @b name sourceConfig)
let sourceMetadata = mkSourceMetadata @b name sourceConfig
metadataModifier <- MetadataModifier <$>
if HM.member name sources then
if replaceConfiguration then pure $ metaSources.ix name .~ sourceMetadata
else throw400 AlreadyExists $ "source with name " <> name <<> " already exists"
else pure $ metaSources %~ OMap.insert name sourceMetadata
buildSchemaCacheFor (MOSource name) metadataModifier
pure successMsg
runDropSource

View File

@ -105,8 +105,9 @@ instance (MonadResolveSource m) => MonadResolveSource (LazyTxT QErr m) where
-- Metadata API related types
data AddSource b
= AddSource
{ _asName :: !SourceName
, _asConfiguration :: !(SourceConnConfiguration b)
{ _asName :: !SourceName
, _asConfiguration :: !(SourceConnConfiguration b)
, _asReplaceConfiguration :: !Bool
} deriving (Generic)
deriving instance (Backend b) => Show (AddSource b)
deriving instance (Backend b) => Eq (AddSource b)
@ -115,7 +116,11 @@ instance (Backend b) => ToJSON (AddSource b) where
toJSON = genericToJSON hasuraJSON
instance (Backend b) => FromJSON (AddSource b) where
parseJSON = genericParseJSON hasuraJSON
parseJSON = withObject "Object" $ \o ->
AddSource
<$> o .: "name"
<*> o .: "configuration"
<*> o .:? "replace_configuration" .!= False
data DropSource
= DropSource