Skip to content

Commit 4999c69

Browse files
committed
devide queries and use statements
1 parent 33d40d2 commit 4999c69

39 files changed

+4782
-4928
lines changed

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

Lines changed: 14 additions & 13 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 qualified Hasql.Connection as HsqlC
6162

6263
runDbSyncNode :: MetricSetters -> [(Text, Text)] -> SyncNodeParams -> SyncNodeConfig -> IO ()
6364
runDbSyncNode metricsSetters knownMigrations params syncNodeConfigFromFile =
@@ -150,14 +151,14 @@ runSyncNode ::
150151
Trace IO Text ->
151152
IOManager ->
152153
-- | Database connection settings
153-
Setting ->
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)

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

Lines changed: 6 additions & 10 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
@@ -167,10 +167,10 @@ 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
175175
DB.runDbT DB.Write transx
176176
where
@@ -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

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/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

cardano-db/src/Cardano/Db/Error.hs

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,77 @@
1-
{-# LANGUAGE DeriveGeneric #-}
2-
{-# LANGUAGE LambdaCase #-}
3-
{-# LANGUAGE OverloadedStrings #-}
41
{-# LANGUAGE RankNTypes #-}
52

63
module Cardano.Db.Error (
7-
AsDbError (..),
4+
-- AsDbError (..),
85
CallSite (..),
96
DbError (..),
107
runOrThrowIODb,
118
runOrThrowIO,
129
logAndThrowIO,
13-
base16encode
10+
base16encode,
1411
) where
1512

1613
import Cardano.BM.Trace (Trace, logError)
17-
import Cardano.Db.Schema.Ids
18-
import Cardano.Prelude (throwIO, MonadIO)
14+
import Cardano.Prelude (MonadIO, throwIO)
1915
import Control.Exception (Exception)
16+
import qualified Data.ByteString.Base16 as Base16
2017
import Data.ByteString.Char8 (ByteString)
2118
import Data.Text (Text)
22-
import Data.Word (Word16, Word64)
23-
import GHC.Generics (Generic)
24-
import qualified Data.ByteString.Base16 as Base16
2519
import qualified Data.Text.Encoding as Text
26-
import qualified Hasql.Session as HsqlS
2720

28-
class AsDbError e where
29-
toDbError :: DbError -> e
30-
fromDbError :: e -> Maybe DbError
21+
import qualified Hasql.Session as HsqlSes
3122

32-
data DbError
33-
= DbError !CallSite !Text !HsqlS.SessionError
34-
| DbLookupError !CallSite !Text !LookupContext
23+
data DbError = DbError
24+
{ dbErrorCallSite :: !CallSite
25+
, dbErrorMessage :: !Text
26+
, dbErrorCause :: !(Maybe HsqlSes.SessionError) -- Now a Maybe
27+
}
3528
deriving (Show, Eq)
3629

3730
instance Exception DbError
3831

32+
-- class AsDbError e where
33+
-- toDbError :: DbError -> e
34+
-- fromDbError :: e -> Maybe DbError
35+
36+
-- data DbError
37+
-- = DbError !CallSite !Text !HsqlS.SessionError
38+
-- | DbLookupError !CallSite !Text !LookupContext
39+
-- deriving (Show, Eq)
40+
41+
-- instance Exception DbError
42+
3943
data CallSite = CallSite
4044
{ csModule :: !Text
4145
, csFile :: !Text
4246
, csLine :: !Int
43-
} deriving (Show, Eq)
44-
45-
data LookupContext
46-
= BlockHashContext !ByteString
47-
| BlockIdContext !Word64
48-
| MessageContext !Text
49-
| TxHashContext !ByteString
50-
| TxOutPairContext !ByteString !Word16
51-
| EpochNoContext !Word64
52-
| SlotNoContext !Word64
53-
| GovActionPairContext !TxId !Word64
54-
| MetaEmptyContext
55-
| MetaMultipleRowsContext
56-
| MultipleGenesisContext
57-
| ExtraMigrationContext !String
58-
| PruneConsumedContext !String
59-
| RJsonbInSchemaContext !String
60-
| TxOutVariantContext !String
61-
deriving (Show, Eq, Generic)
62-
63-
instance Exception LookupContext
47+
}
48+
deriving (Show, Eq)
49+
50+
-- data LookupContext
51+
-- = BlockHashContext !ByteString
52+
-- | BlockIdContext !Word64
53+
-- | MessageContext !Text
54+
-- | TxHashContext !ByteString
55+
-- | TxOutPairContext !ByteString !Word16
56+
-- | EpochNoContext !Word64
57+
-- | SlotNoContext !Word64
58+
-- | GovActionPairContext !TxId !Word64
59+
-- | MetaEmptyContext
60+
-- | MetaMultipleRowsContext
61+
-- | MultipleGenesisContext
62+
-- | ExtraMigrationContext !String
63+
-- | PruneConsumedContext !String
64+
-- | RJsonbInSchemaContext !String
65+
-- | TxOutVariantContext !String
66+
-- deriving (Show, Eq, Generic)
67+
68+
-- instance Exception LookupContext
6469

6570
-- catchDbError :: String -> HsqlT.Transaction a -> HsqlT.Transaction a
6671
-- catchDbError context action =
6772
-- action `catch` \e ->
6873
-- throwError $ DbError $ context ++ ": " ++ show e
6974

70-
7175
-- instance Show LookupFail where
7276
-- show =
7377
-- \case

0 commit comments

Comments
 (0)