Skip to content

Commit e4440e7

Browse files
committed
add Entity instance to handle table ids columns
1 parent 1b19c1f commit e4440e7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+5038
-4007
lines changed

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

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ module Cardano.DbSync (
2323
SimplifiedOffChainPoolData (..),
2424
extractSyncOptions,
2525
) where
26+
import Control.Monad.Extra (whenJust)
27+
import qualified Data.Strict.Maybe as Strict
28+
import qualified Data.Text as Text
29+
import Data.Version (showVersion)
30+
import Database.Persist.Postgresql (ConnectionString, withPostgresqlConn)
31+
import qualified Ouroboros.Consensus.HardFork.Simple as HardFork
32+
import Ouroboros.Network.NodeToClient (IOManager, withIOManager)
33+
import Paths_cardano_db_sync (version)
34+
import System.Directory (createDirectoryIfMissing)
35+
import Prelude (id)
36+
import qualified Hasql.Connection as HsqlC
37+
import qualified Hasql.Connection.Setting as HsqlSet
2638

2739
import Cardano.BM.Trace (Trace, logError, logInfo, logWarning)
2840
import qualified Cardano.Crypto as Crypto
@@ -47,17 +59,6 @@ import Cardano.DbSync.Util.Constraint (queryIsJsonbInSchema)
4759
import Cardano.Prelude hiding (Nat, (%))
4860
import Cardano.Slotting.Slot (EpochNo (..))
4961
import Control.Concurrent.Async
50-
import Control.Monad.Extra (whenJust)
51-
import qualified Data.Strict.Maybe as Strict
52-
import qualified Data.Text as Text
53-
import Data.Version (showVersion)
54-
import Database.Persist.Postgresql (ConnectionString, withPostgresqlConn)
55-
import Ouroboros.Consensus.Cardano (CardanoHardForkTrigger (..))
56-
import Ouroboros.Network.NodeToClient (IOManager, withIOManager)
57-
import Paths_cardano_db_sync (version)
58-
import System.Directory (createDirectoryIfMissing)
59-
import Prelude (id)
60-
import Hasql.Connection as HC
6162

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

116-
let setting = Db.toConnectionSetting pgConfig
117-
117+
let dbConnectionSetting = Db.toConnectionSetting pgConfig
118118

119119
-- For testing and debugging.
120120
whenJust (enpMaybeRollback params) $ \slotNo ->
@@ -123,7 +123,7 @@ runDbSync metricsSetters knownMigrations iomgr trce params syncNodeConfigFromFil
123123
metricsSetters
124124
trce
125125
iomgr
126-
connectionString
126+
dbConnectionSetting
127127
(void . runMigration)
128128
syncNodeConfigFromFile
129129
params
@@ -150,14 +150,15 @@ runSyncNode ::
150150
MetricSetters ->
151151
Trace IO Text ->
152152
IOManager ->
153-
Setting ->
153+
-- | Database connection settings
154+
HsqlSet.Setting ->
154155
-- | run migration function
155156
RunMigration ->
156157
SyncNodeConfig ->
157158
SyncNodeParams ->
158159
SyncOptions ->
159160
IO ()
160-
runSyncNode metricsSetters trce iomgr connSetting runMigrationFnc syncNodeConfigFromFile syncNodeParams syncOptions = do
161+
runSyncNode metricsSetters trce iomgr dbConnSetting runMigrationFnc syncNodeConfigFromFile syncNodeParams syncOptions = do
161162
whenJust maybeLedgerDir $
162163
\enpLedgerStateDir -> do
163164
createDirectoryIfMissing True (unLedgerStateDir enpLedgerStateDir)
@@ -168,11 +169,11 @@ runSyncNode metricsSetters trce iomgr connSetting runMigrationFnc syncNodeConfig
168169
let useLedger = shouldUseLedger (sioLedger $ dncInsertOptions syncNodeConfigFromFile)
169170
-- Our main thread
170171
bracket
171-
(runOrThrowIO $ HC.acquire [connSetting])
172+
(runOrThrowIO $ HsqlC.acquire [dbConnSetting])
172173
release
173-
(\connection -> do
174+
(\dbConn -> do
174175
runOrThrowIO $ runExceptT $ do
175-
let dbEnv = Db.DbEnv connection (dncEnableDbLogging syncNodeConfigFromFile)
176+
let dbEnv = Db.DbEnv dbConn (dncEnableDbLogging syncNodeConfigFromFile)
176177
genCfg <- readCardanoGenesisConfig syncNodeConfigFromFile
177178
isJsonbInSchema <- queryIsJsonbInSchema dbEnv
178179
logProtocolMagicId trce $ genesisProtocolMagicId genCfg

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

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ import qualified Data.Strict.Maybe as Strict
8484
import Data.Time.Clock (getCurrentTime)
8585
import Database.Persist.Postgresql (ConnectionString)
8686
import Database.Persist.Sql (SqlBackend)
87+
import qualified Hasql.Connection as HqlC
8788
import Ouroboros.Consensus.Block.Abstract (BlockProtocol, HeaderHash, Point (..), fromRawHash)
8889
import Ouroboros.Consensus.BlockchainTime.WallClock.Types (SystemStart (..))
8990
import Ouroboros.Consensus.Config (SecurityParam (..), TopLevelConfig, configSecurityParam)
@@ -93,7 +94,6 @@ import Ouroboros.Consensus.Protocol.Abstract (ConsensusProtocol)
9394
import Ouroboros.Network.Block (BlockNo (..), Point (..))
9495
import Ouroboros.Network.Magic (NetworkMagic (..))
9596
import qualified Ouroboros.Network.Point as Point
96-
import qualified Hasql.Connection as HqlC
9797

9898
setConsistentLevel :: SyncEnv -> ConsistentLevel -> IO ()
9999
setConsistentLevel env cst = do
@@ -119,7 +119,7 @@ getIsConsumedFixed env =
119119
where
120120
txOutTableType = getTxOutTableType env
121121
pcm = soptPruneConsumeMigration $ envOptions env
122-
backend = envBackend env
122+
backend = envDbEnv env
123123

124124
getDisableInOutState :: SyncEnv -> IO Bool
125125
getDisableInOutState syncEnv = do
@@ -167,12 +167,12 @@ runAddJsonbToSchema :: SyncEnv -> IO ()
167167
runAddJsonbToSchema syncEnv =
168168
void $ DB.runDbIohkNoLogging (envDbEnv syncEnv) DB.enableJsonbInSchema
169169

170-
runRemoveJsonbFromSchema
171-
:: (MonadIO m, AsDbError e)
172-
=> SyncEnv
173-
-> DbAction e m ()
170+
runRemoveJsonbFromSchema ::
171+
(MonadIO m, AsDbError e) =>
172+
SyncEnv ->
173+
DbAction e m ()
174174
runRemoveJsonbFromSchema syncEnv = do
175-
DB.runDbTx DB.Write transx
175+
DB.runDbT DB.Write transx
176176
where
177177
dbEnv = envDbEnv syncEnv
178178
transx = mkDbTransaction "runRemoveJsonbFromSchema" mkCallSite (DB.disableJsonbInSchema (dbConnection dbEnv))
@@ -285,12 +285,12 @@ getDbLatestBlockInfo backend = do
285285

286286
getDbTipBlockNo :: SyncEnv -> IO (Point.WithOrigin BlockNo)
287287
getDbTipBlockNo env = do
288-
mblk <- getDbLatestBlockInfo (envBackend env)
288+
mblk <- getDbLatestBlockInfo (envDbEnv env)
289289
pure $ maybe Point.Origin (Point.At . bBlockNo) mblk
290290

291291
logDbState :: SyncEnv -> IO ()
292292
logDbState env = do
293-
mblk <- getDbLatestBlockInfo (envBackend env)
293+
mblk <- getDbLatestBlockInfo (envDbEnv env)
294294
case mblk of
295295
Nothing -> logInfo tracer "Database is empty"
296296
Just tip -> logInfo tracer $ mconcat ["Database tip is at ", showTip tip]
@@ -309,7 +309,7 @@ logDbState env = do
309309

310310
getCurrentTipBlockNo :: SyncEnv -> IO (WithOrigin BlockNo)
311311
getCurrentTipBlockNo env = do
312-
maybeTip <- getDbLatestBlockInfo (envBackend env)
312+
maybeTip <- getDbLatestBlockInfo (envDbEnv env)
313313
case maybeTip of
314314
Just tip -> pure $ At (bBlockNo tip)
315315
Nothing -> pure Origin
@@ -379,11 +379,7 @@ mkSyncEnv ::
379379
SyncNodeParams ->
380380
RunMigration ->
381381
IO SyncEnv
382-
<<<<<<< HEAD
383-
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)
382+
mkSyncEnv trce dbEnv connectionString syncOptions protoInfo nw nwMagic systemStart syncNodeConfigFromFile syncNP runMigrationFnc = do
387383
dbCNamesVar <- newTVarIO =<< dbConstraintNamesExists backend
388384
cache <-
389385
if soptCache syncOptions
@@ -512,7 +508,7 @@ getLatestPoints env = do
512508
verifySnapshotPoint env snapshotPoints
513509
NoLedger _ -> do
514510
-- Brings the 5 latest.
515-
lastPoints <- DB.runDbIohkNoLogging (envBackend env) DB.queryLatestPoints
511+
lastPoints <- DB.runDbIohkNoLogging (envDbEnv env) DB.queryLatestPoints
516512
pure $ mapMaybe convert lastPoints
517513
where
518514
convert (Nothing, _) = Nothing
@@ -524,7 +520,7 @@ verifySnapshotPoint env snapPoints =
524520
where
525521
validLedgerFileToPoint :: SnapshotPoint -> IO (Maybe (CardanoPoint, Bool))
526522
validLedgerFileToPoint (OnDisk lsf) = do
527-
hashes <- getSlotHash (envBackend env) (lsfSlotNo lsf)
523+
hashes <- getSlotHash (envDbEnv env) (lsfSlotNo lsf)
528524
let valid = find (\(_, h) -> lsfHash lsf == hashToAnnotation h) hashes
529525
case valid of
530526
Just (slot, hash) | slot == lsfSlotNo lsf -> pure $ convertToDiskPoint slot hash
@@ -533,7 +529,7 @@ verifySnapshotPoint env snapPoints =
533529
case pnt of
534530
GenesisPoint -> pure Nothing
535531
BlockPoint slotNo hsh -> do
536-
hashes <- getSlotHash (envBackend env) slotNo
532+
hashes <- getSlotHash (envDbEnv env) slotNo
537533
let valid = find (\(_, dbHash) -> getHeaderHash hsh == dbHash) hashes
538534
case valid of
539535
Just (dbSlotNo, _) | slotNo == dbSlotNo -> pure $ Just (pnt, True)

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ data NextState
3838
| Done
3939
deriving (Eq)
4040

41-
4241
runDbThread ::
4342
SyncEnv ->
4443
MetricSetters ->
@@ -76,8 +75,8 @@ runDbThread syncEnv metricsSetters queue = do
7675
-- Handle the result of running the actions
7776
case result of
7877
Left err -> logError tracer $ "Error: " <> show err
79-
Right Continue -> processQueue -- Continue processing
80-
Right Done -> pure () -- Stop processing
78+
Right Continue -> processQueue -- Continue processing
79+
Right Done -> pure () -- Stop processing
8180

8281
-- Handle the case where the syncing thread has restarted
8382
handleRestart :: TMVar (LatestPoints, CurrentTip) -> IO ()
@@ -87,7 +86,7 @@ runDbThread syncEnv metricsSetters queue = do
8786
currentTip <- getCurrentTipBlockNo syncEnv
8887
logDbState syncEnv
8988
atomically $ putTMVar resultVar (latestPoints, currentTip)
90-
processQueue -- Continue processing
89+
processQueue -- Continue processing
9190

9291
-- Update block and slot height metrics
9392
updateBlockMetrics :: IO ()
@@ -97,7 +96,6 @@ runDbThread syncEnv metricsSetters queue = do
9796
setDbBlockHeight metricsSetters $ bBlockNo block
9897
setDbSlotHeight metricsSetters $ bSlotNo block
9998

100-
10199
-- runDbThread ::
102100
-- SyncEnv ->
103101
-- MetricSetters ->
@@ -138,7 +136,6 @@ runDbThread syncEnv metricsSetters queue = do
138136
-- atomically $ putTMVar resultVar (latestPoints, currentTip)
139137
-- loop
140138

141-
142139
-- | Run the list of 'DbAction's. Block are applied in a single set (as a transaction)
143140
-- and other operations are applied one-by-one.
144141
runActions ::

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ insertListBlocks ::
5151
[CardanoBlock] ->
5252
IO (Either SyncNodeError ())
5353
insertListBlocks synEnv blocks = do
54-
DB.runDbIohkLogging (envBackend synEnv) tracer
54+
DB.runDbIohkLogging (envDbEnv synEnv) tracer
5555
. runExceptT
5656
$ traverse_ (applyAndInsertBlockMaybe synEnv tracer) blocks
5757
where

cardano-db-sync/src/Cardano/DbSync/Era/Universal/Insert/GovAction.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ insertCommitteeHash cred = do
335335
insertDrep :: (MonadBaseControl IO m, MonadIO m) => DRep -> ReaderT SqlBackend m DB.DrepHashId
336336
insertDrep = \case
337337
DRepCredential cred -> insertCredDrepHash cred
338-
DRepAlwaysAbstain -> DB.insertAlwaysAbstainDrep
338+
DRepAlwaysAbstain -> DB.insertDrepHashAlwaysAbstain
339339
DRepAlwaysNoConfidence -> DB.insertAlwaysNoConfidence
340340

341341
insertCredDrepHash :: (MonadBaseControl IO m, MonadIO m) => Ledger.Credential 'DRepRole -> ReaderT SqlBackend m DB.DrepHashId

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ data SyncInvariant
4242

4343
data SyncNodeError
4444
= SNErrDefault !Text
45-
| SNErrDbTransaction !DB.DbError
45+
| SNErrDatabase !DB.DbError
4646
| SNErrInvariant !Text !SyncInvariant
4747
| SNEErrBlockMismatch !Word64 !ByteString !ByteString
4848
| SNErrIgnoreShelleyInitiation
@@ -67,7 +67,7 @@ instance Show SyncNodeError where
6767
show =
6868
\case
6969
SNErrDefault t -> "Error SNErrDefault: " <> show t
70-
SNErrDbTransaction err -> "Error SNErrDbTransaction: " <> show err
70+
SNErrDatabase err -> "Error SNErrDatabase: " <> show err
7171
SNErrInvariant loc i -> "Error SNErrInvariant: " <> Show.show loc <> ": " <> show (renderSyncInvariant i)
7272
SNEErrBlockMismatch blkNo hashDb hashBlk ->
7373
mconcat

cardano-db/cardano-db.cabal

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ library
3030
-Wunused-packages
3131

3232
exposed-modules: Cardano.Db
33-
Cardano.Db.Schema.Core.TxOut
34-
Cardano.Db.Schema.Variant.TxOut
33+
Cardano.Db.Schema.Variants
3534

3635
other-modules: Cardano.Db.Error
3736
Cardano.Db.Git.RevFromGit
@@ -64,6 +63,10 @@ library
6463
Cardano.Db.Schema.Ids
6564
Cardano.Db.Schema.Orphans
6665
Cardano.Db.Schema.Types
66+
Cardano.Db.Schema.Variants.TxOutAddress
67+
Cardano.Db.Schema.Variants.TxOutCore
68+
Cardano.Db.Schema.Variants.TxOutUtxoHd
69+
Cardano.Db.Schema.Variants.TxOutUtxoHdAddress
6770
Cardano.Db.Statement
6871
Cardano.Db.Statement.Function.Core
6972
Cardano.Db.Statement.Function.Query
@@ -93,20 +96,17 @@ library
9396
, contravariant-extras
9497
, cryptonite
9598
, directory
96-
, esqueleto
9799
, extra
98100
, fast-logger
99101
, filepath
100102
, file-embed
101103
, hasql
102-
, hasql-transaction
103104
, iohk-monitoring
104105
, lifted-base
105106
, memory
106107
, monad-control
107108
, monad-logger
108109
, persistent
109-
, persistent-documentation
110110
, persistent-postgresql
111111
, postgresql-simple
112112
, process

0 commit comments

Comments
 (0)