@@ -22,6 +22,7 @@ module Cardano.DbSync.Config.Types (
2222 SyncNodeConfig (.. ),
2323 SyncPreConfig (.. ),
2424 SyncInsertConfig (.. ),
25+ SyncInsertPreset (.. ),
2526 SyncInsertOptions (.. ),
2627 TxCBORConfig (.. ),
2728 PoolStatsConfig (.. ),
@@ -55,6 +56,10 @@ module Cardano.DbSync.Config.Types (
5556 isTxOutConsumed ,
5657 isTxOutPrune ,
5758 forceTxIn ,
59+ fullInsertOptions ,
60+ onlyUTxOInsertOptions ,
61+ onlyGovInsertOptions ,
62+ disableAllInsertOptions ,
5863) where
5964
6065import qualified Cardano.BM.Configuration as Logging
@@ -68,7 +73,8 @@ import Cardano.Slotting.Slot (SlotNo (..))
6873import Control.Monad (fail )
6974import Data.Aeson (FromJSON (.. ), ToJSON (.. ), Value (.. ), (.!=) , (.:) , (.:?) , (.=) )
7075import qualified Data.Aeson as Aeson
71- import Data.Aeson.Types (Parser , typeMismatch )
76+ import Data.Aeson.Key (fromText )
77+ import Data.Aeson.Types (Pair , Parser , typeMismatch )
7278import Data.ByteString.Short (ShortByteString (), fromShort , toShort )
7379import Data.Default.Class (Default (.. ))
7480import Ouroboros.Consensus.Cardano.CanHardFork (TriggerHardFork (.. ))
@@ -150,12 +156,17 @@ data SyncPreConfig = SyncPreConfig
150156 }
151157 deriving (Show )
152158
153- data SyncInsertConfig
154- = FullInsertOptions
155- | OnlyUTxOInsertOptions
156- | OnlyGovInsertOptions
157- | DisableAllInsertOptions
158- | SyncInsertConfig SyncInsertOptions
159+ data SyncInsertConfig = SyncInsertConfig
160+ { sicPreset :: Maybe SyncInsertPreset
161+ , sicOptions :: SyncInsertOptions
162+ }
163+ deriving (Eq , Show )
164+
165+ data SyncInsertPreset
166+ = FullInsertPreset
167+ | OnlyUTxOInsertPreset
168+ | OnlyGovInsertPreset
169+ | DisableAllInsertPreset
159170 deriving (Eq , Show )
160171
161172data SyncInsertOptions = SyncInsertOptions
@@ -386,23 +397,73 @@ instance FromJSON SyncProtocol where
386397 String " Cardano" -> pure SyncProtocolCardano
387398 x -> typeMismatch " Protocol" x
388399
400+ instance FromJSON SyncInsertPreset where
401+ parseJSON = Aeson. withText " SyncInsertPreset" $ \ case
402+ " full" -> pure FullInsertPreset
403+ " only_utxo" -> pure OnlyUTxOInsertPreset
404+ " only_governance" -> pure OnlyGovInsertPreset
405+ " disable_all" -> pure DisableAllInsertPreset
406+ other -> fail $ " unexpected preset: " <> show other
407+
408+ instance ToJSON SyncInsertPreset where
409+ toJSON FullInsertPreset = " full"
410+ toJSON OnlyUTxOInsertPreset = " only_utxo"
411+ toJSON OnlyGovInsertPreset = " only_governance"
412+ toJSON DisableAllInsertPreset = " disable_all"
413+
389414instance FromJSON SyncInsertConfig where
390415 parseJSON = Aeson. withObject " SyncInsertConfig" $ \ obj -> do
391416 preset <- obj .:? " preset"
392- case preset :: Maybe Text of
393- Nothing -> SyncInsertConfig <$> parseJSON (Aeson. Object obj)
394- Just " full" -> pure FullInsertOptions
395- Just " only_utxo" -> pure OnlyUTxOInsertOptions
396- Just " only_gov" -> pure OnlyGovInsertOptions
397- Just " disable_all" -> pure DisableAllInsertOptions
398- Just other -> fail $ " unexpected preset: " <> show other
417+ baseOptions <- case preset of
418+ Just FullInsertPreset -> pure fullInsertOptions
419+ Just OnlyUTxOInsertPreset -> pure onlyUTxOInsertOptions
420+ Just OnlyGovInsertPreset -> pure onlyGovInsertOptions
421+ Just DisableAllInsertPreset -> pure disableAllInsertOptions
422+ Nothing -> pure def -- Default options
423+ options <- parseOverrides obj baseOptions
424+ pure $ SyncInsertConfig preset options
425+
426+ parseOverrides :: Aeson. Object -> SyncInsertOptions -> Parser SyncInsertOptions
427+ parseOverrides obj baseOptions = do
428+ SyncInsertOptions
429+ <$> obj .:? " tx_cbor" .!= sioTxCBOR baseOptions
430+ <*> obj .:? " tx_out" .!= sioTxOut baseOptions
431+ <*> obj .:? " ledger" .!= sioLedger baseOptions
432+ <*> obj .:? " shelley" .!= sioShelley baseOptions
433+ <*> pure (sioRewards baseOptions)
434+ <*> obj .:? " multi_asset" .!= sioMultiAsset baseOptions
435+ <*> obj .:? " metadata" .!= sioMetadata baseOptions
436+ <*> obj .:? " plutus" .!= sioPlutus baseOptions
437+ <*> obj .:? " governance" .!= sioGovernance baseOptions
438+ <*> obj .:? " offchain_pool_data" .!= sioOffchainPoolData baseOptions
439+ <*> obj .:? " pool_stats" .!= sioPoolStats baseOptions
440+ <*> obj .:? " json_type" .!= sioJsonType baseOptions
441+ <*> obj .:? " remove_jsonb_from_schema" .!= sioRemoveJsonbFromSchema baseOptions
399442
400443instance ToJSON SyncInsertConfig where
401- toJSON (SyncInsertConfig opts) = toJSON opts
402- toJSON FullInsertOptions = Aeson. object [" preset" .= (" full" :: Text )]
403- toJSON OnlyUTxOInsertOptions = Aeson. object [" preset" .= (" only_utxo" :: Text )]
404- toJSON OnlyGovInsertOptions = Aeson. object [" preset" .= (" only_gov" :: Text )]
405- toJSON DisableAllInsertOptions = Aeson. object [" preset" .= (" disable_all" :: Text )]
444+ toJSON (SyncInsertConfig preset options) =
445+ Aeson. object $ maybe [] (\ p -> [fromText " preset" .= p]) preset ++ optionsToList options
446+
447+ optionsToList :: SyncInsertOptions -> [Pair ]
448+ optionsToList SyncInsertOptions {.. } =
449+ catMaybes
450+ [ toJsonIfSet " tx_cbor" sioTxCBOR
451+ , toJsonIfSet " tx_out" sioTxOut
452+ , toJsonIfSet " ledger" sioLedger
453+ , toJsonIfSet " shelley" sioShelley
454+ , toJsonIfSet " rewards" sioRewards
455+ , toJsonIfSet " multi_asset" sioMultiAsset
456+ , toJsonIfSet " metadata" sioMetadata
457+ , toJsonIfSet " plutus" sioPlutus
458+ , toJsonIfSet " governance" sioGovernance
459+ , toJsonIfSet " offchain_pool_data" sioOffchainPoolData
460+ , toJsonIfSet " pool_stats" sioPoolStats
461+ , toJsonIfSet " json_type" sioJsonType
462+ , toJsonIfSet " remove_jsonb_from_schema" sioRemoveJsonbFromSchema
463+ ]
464+
465+ toJsonIfSet :: ToJSON a => Text -> a -> Maybe Pair
466+ toJsonIfSet key value = Just $ fromText key .= value
406467
407468instance FromJSON SyncInsertOptions where
408469 parseJSON = Aeson. withObject " SyncInsertOptions" $ \ obj ->
@@ -433,11 +494,14 @@ instance ToJSON SyncInsertOptions where
433494 , " plutus" .= sioPlutus
434495 , " governance" .= sioGovernance
435496 , " offchain_pool_data" .= sioOffchainPoolData
436- , " pool_stat " .= sioPoolStats
497+ , " pool_stats " .= sioPoolStats
437498 , " json_type" .= sioJsonType
438499 , " remove_jsonb_from_schema" .= sioRemoveJsonbFromSchema
439500 ]
440501
502+ instance ToJSON RewardsConfig where
503+ toJSON (RewardsConfig enabled) = Aeson. Bool enabled
504+
441505instance ToJSON TxCBORConfig where
442506 toJSON = boolToEnableDisable . isTxCBOREnabled
443507
@@ -626,7 +690,7 @@ instance FromJSON JsonTypeConfig where
626690 other -> fail $ " unexpected json_type: " <> show other
627691
628692instance Default SyncInsertConfig where
629- def = SyncInsertConfig def
693+ def = SyncInsertConfig Nothing def
630694
631695instance Default SyncInsertOptions where
632696 def =
@@ -646,6 +710,68 @@ instance Default SyncInsertOptions where
646710 , sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
647711 }
648712
713+ fullInsertOptions :: SyncInsertOptions
714+ fullInsertOptions =
715+ SyncInsertOptions
716+ { sioTxCBOR = TxCBORConfig False
717+ , sioTxOut = TxOutEnable
718+ , sioLedger = LedgerEnable
719+ , sioShelley = ShelleyEnable
720+ , sioRewards = RewardsConfig True
721+ , sioMultiAsset = MultiAssetEnable
722+ , sioMetadata = MetadataEnable
723+ , sioPlutus = PlutusEnable
724+ , sioGovernance = GovernanceConfig True
725+ , sioOffchainPoolData = OffchainPoolDataConfig True
726+ , sioPoolStats = PoolStatsConfig True
727+ , sioJsonType = JsonTypeText
728+ , sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
729+ }
730+
731+ onlyUTxOInsertOptions :: SyncInsertOptions
732+ onlyUTxOInsertOptions =
733+ SyncInsertOptions
734+ { sioTxCBOR = TxCBORConfig False
735+ , sioTxOut = TxOutBootstrap (ForceTxIn False )
736+ , sioLedger = LedgerIgnore
737+ , sioShelley = ShelleyDisable
738+ , sioRewards = RewardsConfig True
739+ , sioMultiAsset = MultiAssetDisable
740+ , sioMetadata = MetadataDisable
741+ , sioPlutus = PlutusDisable
742+ , sioGovernance = GovernanceConfig False
743+ , sioOffchainPoolData = OffchainPoolDataConfig False
744+ , sioPoolStats = PoolStatsConfig False
745+ , sioJsonType = JsonTypeText
746+ , sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
747+ }
748+
749+ onlyGovInsertOptions :: SyncInsertOptions
750+ onlyGovInsertOptions =
751+ disableAllInsertOptions
752+ { sioLedger = LedgerEnable
753+ , sioGovernance = GovernanceConfig True
754+ , sioPoolStats = PoolStatsConfig True
755+ }
756+
757+ disableAllInsertOptions :: SyncInsertOptions
758+ disableAllInsertOptions =
759+ SyncInsertOptions
760+ { sioTxCBOR = TxCBORConfig False
761+ , sioTxOut = TxOutDisable
762+ , sioLedger = LedgerDisable
763+ , sioShelley = ShelleyDisable
764+ , sioRewards = RewardsConfig False
765+ , sioMultiAsset = MultiAssetDisable
766+ , sioMetadata = MetadataDisable
767+ , sioPlutus = PlutusDisable
768+ , sioOffchainPoolData = OffchainPoolDataConfig False
769+ , sioPoolStats = PoolStatsConfig False
770+ , sioGovernance = GovernanceConfig False
771+ , sioJsonType = JsonTypeText
772+ , sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
773+ }
774+
649775boolToEnableDisable :: IsString s => Bool -> s
650776boolToEnableDisable True = " enable"
651777boolToEnableDisable False = " disable"
0 commit comments