Skip to content

Commit 7e0acdc

Browse files
committed
Persistent to Hasql
1 parent b8e53e3 commit 7e0acdc

File tree

19 files changed

+4199
-1670
lines changed

19 files changed

+4199
-1670
lines changed

cardano-db/cardano-db.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ library
8080
, fast-logger
8181
, filepath
8282
, file-embed
83+
, hasql
84+
, hasql-transaction
8385
, iohk-monitoring
8486
, lifted-base
8587
, memory

cardano-db/src/Cardano/Db/Operations/Insert.hs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ import Database.Persist.Types (
151151
entityKey,
152152
)
153153
import Database.PostgreSQL.Simple (SqlError)
154+
import Hasql.Statement (Statement)
155+
import qualified Hasql.Transaction.Sessions as Transaction
156+
import qualified Hasql.Transaction as Transactio
157+
154158

155159
-- The original naive way of inserting rows into Postgres was:
156160
--
@@ -171,8 +175,25 @@ import Database.PostgreSQL.Simple (SqlError)
171175
insertAdaPots :: (MonadBaseControl IO m, MonadIO m) => AdaPots -> ReaderT SqlBackend m AdaPotsId
172176
insertAdaPots = insertUnchecked "AdaPots"
173177

174-
insertBlock :: (MonadBaseControl IO m, MonadIO m) => Block -> ReaderT SqlBackend m BlockId
175-
insertBlock = insertUnchecked "Block"
178+
-- insertBlock :: (MonadBaseControl IO m, MonadIO m) => Block -> ReaderT SqlBackend m BlockId
179+
-- insertBlock = insertUnchecked "Block"
180+
181+
insertBlock :: Block -> Session BlockId
182+
insertBlock block = Transaction.transaction Transaction.ReadCommitted Transaction.Write insertBlockTransaction
183+
184+
insertBlockStatement :: Statement Block BlockId
185+
insertBlockStatement =
186+
Statement
187+
"INSERT INTO block (id, hash, slot_no, epoch_no) VALUES ($1, $2, $3, $4) RETURNING id"
188+
blockEncoder
189+
(BlockId <$> Decode.int64)
190+
191+
insertBlockTransaction :: Block -> Transaction BlockId
192+
insertBlockTransaction block = do
193+
result <- Transaction.statement block insertBlockStatement
194+
case result of
195+
Right blockId -> pure blockId
196+
Left err -> liftIO $ throwIO (DbInsertException "Block" (fromString $ show err))
176197

177198
insertCollateralTxIn :: (MonadBaseControl IO m, MonadIO m) => CollateralTxIn -> ReaderT SqlBackend m CollateralTxInId
178199
insertCollateralTxIn = insertUnchecked "CollateralTxIn"

cardano-db/src/Cardano/Db/Schema/BaseSchema.hs

Lines changed: 3647 additions & 1363 deletions
Large diffs are not rendered by default.

cardano-db/src/Cardano/Db/Schema/Core/TxOut.hs

Lines changed: 0 additions & 118 deletions
This file was deleted.

cardano-db/src/Cardano/Db/Schema/Orphans.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ import Cardano.Db.Types (
3030
readVoterRole,
3131
renderAnchorType,
3232
renderGovActionType,
33-
renderScriptPurpose,
34-
renderScriptType,
33+
scriptPurposeFromText,
34+
scriptPurposeToText,
3535
renderSyncState,
3636
renderVote,
3737
renderVoterRole,
@@ -109,13 +109,13 @@ instance PersistField SyncState where
109109
Left $ mconcat ["Failed to parse Haskell type SyncState: ", Text.pack (show x)]
110110

111111
instance PersistField ScriptPurpose where
112-
toPersistValue = PersistText . renderScriptPurpose
112+
toPersistValue = PersistText . scriptPurposeFromText
113113
fromPersistValue (PersistLiteral bs) = Right $ readScriptPurpose (BS.unpack bs)
114114
fromPersistValue x =
115115
Left $ mconcat ["Failed to parse Haskell type ScriptPurpose: ", Text.pack (show x)]
116116

117117
instance PersistField ScriptType where
118-
toPersistValue = PersistText . renderScriptType
118+
toPersistValue = PersistText . scriptPurposeToText
119119
fromPersistValue (PersistLiteral bs) = Right $ readScriptType (BS.unpack bs)
120120
fromPersistValue x =
121121
Left $ mconcat ["Failed to parse Haskell type ScriptType: ", Text.pack (show x)]

cardano-db/src/Cardano/Db/Schema/Variant/TxOut.hs

Lines changed: 0 additions & 119 deletions
This file was deleted.

0 commit comments

Comments
 (0)