Skip to content

Commit 9eb08ff

Browse files
committed
more functionality
1 parent 38627e3 commit 9eb08ff

File tree

26 files changed

+558
-263
lines changed

26 files changed

+558
-263
lines changed

cardano-chain-gen/test/Test/Cardano/Db/Mock/Config.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ queryDBSync env = DB.runWithConnectionNoLogging (getDBSyncPGPass env)
232232
getPoolLayer :: DBSyncEnv -> IO PoolDataLayer
233233
getPoolLayer env = do
234234
pgconfig <- runOrThrowIO $ DB.readPGPass (enpPGPassSource $ dbSyncParams env)
235-
pool <- runNoLoggingT $ createPostgresqlPool (DB.toConnectionString pgconfig) 1 -- Pool size of 1 for tests
235+
pool <- runNoLoggingT $ createPostgresqlPool (DB.toConnectionSetting pgconfig) 1 -- Pool size of 1 for tests
236236
pure $
237237
postgresqlPoolDataLayer
238238
nullTracer

cardano-db-sync/src/Cardano/DbSync.hs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import Ouroboros.Network.NodeToClient (IOManager, withIOManager)
5757
import Paths_cardano_db_sync (version)
5858
import System.Directory (createDirectoryIfMissing)
5959
import Prelude (id)
60+
import Hasql.Connection as HC
6061

6162
runDbSyncNode :: MetricSetters -> [(Text, Text)] -> SyncNodeParams -> SyncNodeConfig -> IO ()
6263
runDbSyncNode metricsSetters knownMigrations params syncNodeConfigFromFile =
@@ -112,7 +113,7 @@ runDbSync metricsSetters knownMigrations iomgr trce params syncNodeConfigFromFil
112113
then logInfo trce "All user indexes were created"
113114
else logInfo trce "New user indexes were not created. They may be created later if necessary."
114115

115-
let connectionString = Db.toConnectionString pgConfig
116+
let setting = Db.toConnectionSetting pgConfig
116117

117118
-- For testing and debugging.
118119
whenJust (enpMaybeRollback params) $ \slotNo ->
@@ -148,14 +149,14 @@ runSyncNode ::
148149
MetricSetters ->
149150
Trace IO Text ->
150151
IOManager ->
151-
ConnectionString ->
152+
Setting ->
152153
-- | run migration function
153154
RunMigration ->
154155
SyncNodeConfig ->
155156
SyncNodeParams ->
156157
SyncOptions ->
157158
IO ()
158-
runSyncNode metricsSetters trce iomgr dbConnString runMigrationFnc syncNodeConfigFromFile syncNodeParams syncOptions = do
159+
runSyncNode metricsSetters trce iomgr connSetting runMigrationFnc syncNodeConfigFromFile syncNodeParams syncOptions = do
159160
whenJust maybeLedgerDir $
160161
\enpLedgerStateDir -> do
161162
createDirectoryIfMissing True (unLedgerStateDir enpLedgerStateDir)
@@ -164,20 +165,21 @@ runSyncNode metricsSetters trce iomgr dbConnString runMigrationFnc syncNodeConfi
164165
logInfo trce $ "Using alonzo genesis file from: " <> (show . unGenesisFile $ dncAlonzoGenesisFile syncNodeConfigFromFile)
165166

166167
let useLedger = shouldUseLedger (sioLedger $ dncInsertOptions syncNodeConfigFromFile)
167-
cPool <- createPool dbConnString
168+
-- Our main thread
168169
bracket
169-
cPool
170-
Pool.release
171-
(\pool -> do
170+
(runOrThrowIO $ HC.acquire [connSetting])
171+
release
172+
(\connection -> do
172173
runOrThrowIO $ runExceptT $ do
174+
let dbEnv = Db.DbEnv connection (dncEnableDbLogging syncNodeConfigFromFile)
173175
genCfg <- readCardanoGenesisConfig syncNodeConfigFromFile
174-
isJsonbInSchema <- queryIsJsonbInSchema pool
176+
isJsonbInSchema <- queryIsJsonbInSchema dbEnv
175177
logProtocolMagicId trce $ genesisProtocolMagicId genCfg
176178
syncEnv <-
177179
ExceptT $
178180
mkSyncEnvFromConfig
179181
trce
180-
pool
182+
dbEnv
181183
dbConnString
182184
syncOptions
183185
genCfg

cardano-db-sync/src/Cardano/DbSync/Api.hs

Lines changed: 71 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ import Ouroboros.Consensus.Protocol.Abstract (ConsensusProtocol)
9393
import Ouroboros.Network.Block (BlockNo (..), Point (..))
9494
import Ouroboros.Network.Magic (NetworkMagic (..))
9595
import qualified Ouroboros.Network.Point as Point
96+
import qualified Hasql.Connection as HqlC
9697

9798
setConsistentLevel :: SyncEnv -> ConsistentLevel -> IO ()
9899
setConsistentLevel env cst = do
@@ -155,7 +156,7 @@ runExtraMigrationsMaybe syncEnv = do
155156
let pcm = getPruneConsume syncEnv
156157
txOutTableType = getTxOutTableType syncEnv
157158
logInfo (getTrace syncEnv) $ "runExtraMigrationsMaybe: " <> textShow pcm
158-
DB.runDbIohkNoLogging (envBackend syncEnv) $
159+
DB.runDbIohkNoLogging (envDbEnv syncEnv) $
159160
DB.runExtraMigrations
160161
(getTrace syncEnv)
161162
txOutTableType
@@ -164,11 +165,17 @@ runExtraMigrationsMaybe syncEnv = do
164165

165166
runAddJsonbToSchema :: SyncEnv -> IO ()
166167
runAddJsonbToSchema syncEnv =
167-
void $ DB.runDbIohkNoLogging (envBackend syncEnv) DB.enableJsonbInSchema
168-
169-
runRemoveJsonbFromSchema :: SyncEnv -> IO ()
170-
runRemoveJsonbFromSchema syncEnv =
171-
void $ DB.runDbIohkNoLogging (envBackend syncEnv) DB.disableJsonbInSchema
168+
void $ DB.runDbIohkNoLogging (envDbEnv syncEnv) DB.enableJsonbInSchema
169+
170+
runRemoveJsonbFromSchema
171+
:: (MonadIO m, AsDbError e)
172+
=> SyncEnv
173+
-> DbAction e m ()
174+
runRemoveJsonbFromSchema syncEnv = do
175+
DB.runDbTx DB.Write transx
176+
where
177+
dbEnv = envDbEnv syncEnv
178+
transx = mkDbTransaction "runRemoveJsonbFromSchema" mkCallSite (DB.disableJsonbInSchema (dbConnection dbEnv))
172179

173180
getSafeBlockNoDiff :: SyncEnv -> Word64
174181
getSafeBlockNoDiff syncEnv = 2 * getSecurityParam syncEnv
@@ -307,9 +314,61 @@ getCurrentTipBlockNo env = do
307314
Just tip -> pure $ At (bBlockNo tip)
308315
Nothing -> pure Origin
309316

317+
mkSyncEnvFromConfig ::
318+
Trace IO Text ->
319+
Db.DbEnv ->
320+
ConnectionString ->
321+
SyncOptions ->
322+
GenesisConfig ->
323+
SyncNodeConfig ->
324+
SyncNodeParams ->
325+
-- | migrations were ran on startup
326+
Bool ->
327+
-- | run migration function
328+
RunMigration ->
329+
IO (Either SyncNodeError SyncEnv)
330+
mkSyncEnvFromConfig trce dbEnv connectionString syncOptions genCfg syncNodeConfigFromFile syncNodeParams ranMigration runMigrationFnc =
331+
case genCfg of
332+
GenesisCardano _ bCfg sCfg _ _
333+
| unProtocolMagicId (Byron.configProtocolMagicId bCfg) /= Shelley.sgNetworkMagic (scConfig sCfg) ->
334+
pure
335+
. Left
336+
. SNErrCardanoConfig
337+
$ mconcat
338+
[ "ProtocolMagicId "
339+
, textShow (unProtocolMagicId $ Byron.configProtocolMagicId bCfg)
340+
, " /= "
341+
, textShow (Shelley.sgNetworkMagic $ scConfig sCfg)
342+
]
343+
| Byron.gdStartTime (Byron.configGenesisData bCfg) /= Shelley.sgSystemStart (scConfig sCfg) ->
344+
pure
345+
. Left
346+
. SNErrCardanoConfig
347+
$ mconcat
348+
[ "SystemStart "
349+
, textShow (Byron.gdStartTime $ Byron.configGenesisData bCfg)
350+
, " /= "
351+
, textShow (Shelley.sgSystemStart $ scConfig sCfg)
352+
]
353+
| otherwise ->
354+
Right
355+
<$> mkSyncEnv
356+
trce
357+
dbEnv
358+
connectionString
359+
syncOptions
360+
(fst $ mkProtocolInfoCardano genCfg [])
361+
(Shelley.sgNetworkId $ scConfig sCfg)
362+
(NetworkMagic . unProtocolMagicId $ Byron.configProtocolMagicId bCfg)
363+
(SystemStart . Byron.gdStartTime $ Byron.configGenesisData bCfg)
364+
syncNodeConfigFromFile
365+
syncNodeParams
366+
ranMigration
367+
runMigrationFnc
368+
310369
mkSyncEnv ::
311370
Trace IO Text ->
312-
Pool ->
371+
Db.DbEnv ->
313372
ConnectionString ->
314373
SyncOptions ->
315374
ProtocolInfo CardanoBlock ->
@@ -320,7 +379,11 @@ mkSyncEnv ::
320379
SyncNodeParams ->
321380
RunMigration ->
322381
IO SyncEnv
382+
<<<<<<< HEAD
323383
mkSyncEnv trce backend connectionString syncOptions protoInfo nw nwMagic systemStart syncNodeConfigFromFile syncNP runMigrationFnc = do
384+
=======
385+
mkSyncEnv trce dbEnv connectionString syncOptions protoInfo nw nwMagic systemStart syncNodeConfigFromFile syncNP ranMigrations runMigrationFnc = do
386+
>>>>>>> 29841e49 (more functionality)
324387
dbCNamesVar <- newTVarIO =<< dbConstraintNamesExists backend
325388
cache <-
326389
if soptCache syncOptions
@@ -367,7 +430,7 @@ mkSyncEnv trce backend connectionString syncOptions protoInfo nw nwMagic systemS
367430

368431
pure $
369432
SyncEnv
370-
{ envPool = dbPool
433+
{ envDbEnv = dbEnv
371434
, envBootstrap = bootstrapVar
372435
, envCache = cache
373436
, envConnectionString = connectionString
@@ -402,11 +465,7 @@ mkSyncEnvFromConfig ::
402465
-- | run migration function
403466
RunMigration ->
404467
IO (Either SyncNodeError SyncEnv)
405-
<<<<<<< HEAD
406-
mkSyncEnvFromConfig trce backend connectionString syncOptions genCfg syncNodeConfigFromFile syncNodeParams runMigrationFnc =
407-
=======
408468
mkSyncEnvFromConfig trce dbPool connectionString syncOptions genCfg syncNodeConfigFromFile syncNodeParams ranMigration runMigrationFnc =
409-
>>>>>>> 77f1a2c6 (make a start to stetting up hasql with db pools)
410469
case genCfg of
411470
GenesisCardano _ bCfg sCfg _ _
412471
| unProtocolMagicId (Byron.configProtocolMagicId bCfg) /= Shelley.sgNetworkMagic (scConfig sCfg) ->

cardano-db-sync/src/Cardano/DbSync/Api/Types.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ import Database.Persist.Postgresql (ConnectionString)
3535
import Ouroboros.Consensus.BlockchainTime.WallClock.Types (SystemStart (..))
3636
import Ouroboros.Network.Magic (NetworkMagic (..))
3737

38+
3839
data SyncEnv = SyncEnv
39-
{ envPool :: !!Pool.Pool
40+
{ envDbEnv :: !!DB.DbEnv
4041
, envCache :: !CacheStatus
4142
, envConnectionString :: !ConnectionString
4243
, envConsistentLevel :: !(StrictTVar IO ConsistentLevel)

cardano-db-sync/src/Cardano/DbSync/Config.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ coalesceConfig pcfg ncfg adjustGenesisPath = do
6767
, dncProtocol = ncProtocol ncfg
6868
, dncRequiresNetworkMagic = ncRequiresNetworkMagic ncfg
6969
, dncEnableLogging = pcEnableLogging pcfg
70+
, dncEnableDbLogging = pcEnableDbLogging pcfg
7071
, dncEnableMetrics = pcEnableMetrics pcfg
7172
, dncPrometheusPort = pcPrometheusPort pcfg
7273
, dncPBftSignatureThreshold = ncPBftSignatureThreshold ncfg

cardano-db-sync/src/Cardano/DbSync/Config/Types.hs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ data SyncNodeConfig = SyncNodeConfig
123123
, dncProtocol :: !SyncProtocol
124124
, dncRequiresNetworkMagic :: !RequiresNetworkMagic
125125
, dncEnableLogging :: !Bool
126+
, dncEnableDbLogging :: !Bool
126127
, dncEnableMetrics :: !Bool
127128
, dncPrometheusPort :: !Int
128129
, dncPBftSignatureThreshold :: !(Maybe Double)
@@ -151,6 +152,7 @@ data SyncPreConfig = SyncPreConfig
151152
, pcNodeConfigFile :: !NodeConfigFile
152153
, pcEnableFutureGenesis :: !Bool
153154
, pcEnableLogging :: !Bool
155+
, pcEnableDbLogging :: !Bool
154156
, pcEnableMetrics :: !Bool
155157
, pcPrometheusPort :: !Int
156158
, pcInsertConfig :: !SyncInsertConfig
@@ -384,7 +386,7 @@ isPlutusEnabled PlutusDisable = False
384386
isPlutusEnabled PlutusEnable = True
385387
isPlutusEnabled (PlutusScripts _) = True
386388

387-
-- -------------------------------------------------------------------------------------------------
389+
---------------------------------------------------------------------------------------------------
388390

389391
instance FromJSON SyncPreConfig where
390392
parseJSON =
@@ -398,6 +400,7 @@ parseGenSyncNodeConfig o =
398400
<*> fmap NodeConfigFile (o .: "NodeConfigFile")
399401
<*> fmap (fromMaybe True) (o .:? "EnableFutureGenesis")
400402
<*> o .: "EnableLogging"
403+
<*> fmap (fromMaybe False) (o .:? "EnableDbLogging")
401404
<*> o .: "EnableLogMetrics"
402405
<*> fmap (fromMaybe 8080) (o .:? "PrometheusPort")
403406
<*> o .:? "insert_options" .!= def
@@ -451,6 +454,7 @@ parseOverrides obj baseOptions = do
451454
<*> obj .:? "pool_stat" .!= sioPoolStats baseOptions
452455
<*> obj .:? "json_type" .!= sioJsonType baseOptions
453456
<*> obj .:? "remove_jsonb_from_schema" .!= sioRemoveJsonbFromSchema baseOptions
457+
<*> obj .:? "db_debug" .!= sioDbDebug baseOptions
454458

455459
instance ToJSON SyncInsertConfig where
456460
toJSON (SyncInsertConfig preset options) =
@@ -472,6 +476,7 @@ optionsToList SyncInsertOptions {..} =
472476
, toJsonIfSet "pool_stat" sioPoolStats
473477
, toJsonIfSet "json_type" sioJsonType
474478
, toJsonIfSet "remove_jsonb_from_schema" sioRemoveJsonbFromSchema
479+
, toJsonIfSet "db_debug" sioDbDebug
475480
]
476481

477482
toJsonIfSet :: ToJSON a => Text -> a -> Maybe Pair
@@ -493,6 +498,7 @@ instance FromJSON SyncInsertOptions where
493498
<*> obj .:? "pool_stat" .!= sioPoolStats def
494499
<*> obj .:? "json_type" .!= sioJsonType def
495500
<*> obj .:? "remove_jsonb_from_schema" .!= sioRemoveJsonbFromSchema def
501+
<*> obj .:? "db_debug" .!= sioDbDebug def
496502

497503
instance ToJSON SyncInsertOptions where
498504
toJSON SyncInsertOptions {..} =
@@ -509,6 +515,7 @@ instance ToJSON SyncInsertOptions where
509515
, "pool_stat" .= sioPoolStats
510516
, "json_type" .= sioJsonType
511517
, "remove_jsonb_from_schema" .= sioRemoveJsonbFromSchema
518+
, "db_debug" .= sioDbDebug
512519
]
513520

514521
instance ToJSON RewardsConfig where

0 commit comments

Comments
 (0)