Skip to content

Commit 7dd753f

Browse files
committed
add inserts better dbinfo class for uniques
1 parent 5a6ecf4 commit 7dd753f

34 files changed

+6012
-5266
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ runDbSync metricsSetters knownMigrations iomgr trce params syncNodeConfigFromFil
115115

116116
let setting = Db.toConnectionSetting pgConfig
117117

118+
118119
-- For testing and debugging.
119120
whenJust (enpMaybeRollback params) $ \slotNo ->
120121
void $ unsafeRollback trce (txOutConfigToTableType txOutConfig) pgConfig slotNo

cardano-db/cardano-db.cabal

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,17 @@ library
6565
Cardano.Db.Schema.Orphans
6666
Cardano.Db.Schema.Types
6767
Cardano.Db.Statement
68+
Cardano.Db.Statement.Function.Core
69+
Cardano.Db.Statement.Function.Query
70+
Cardano.Db.Statement.Function.Insert
6871
Cardano.Db.Statement.Base
6972
Cardano.Db.Statement.EpochAndProtocol
7073
Cardano.Db.Statement.GovernanceAndVoting
71-
Cardano.Db.Statement.Helpers
7274
Cardano.Db.Statement.MultiAsset
7375
Cardano.Db.Statement.OffChain
7476
Cardano.Db.Statement.Pool
7577
Cardano.Db.Statement.StakeDeligation
78+
Cardano.Db.Statement.Types
7679
Cardano.Db.Types
7780

7881
build-depends: aeson

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

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

cardano-db/src/Cardano/Db/Operations/Other/ConsumedTxOut.hs

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

cardano-db/src/Cardano/Db/Operations/Other/JsonbQuery.hs

Lines changed: 75 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -5,121 +5,87 @@
55

66
module Cardano.Db.Operations.Other.JsonbQuery where
77

8-
import Cardano.Db.Error (LookupFail (..))
9-
import Control.Exception.Lifted (handle, throwIO)
10-
import Control.Monad.IO.Class (MonadIO, liftIO)
11-
import Control.Monad.Trans.Control (MonadBaseControl)
12-
import Control.Monad.Trans.Reader (ReaderT)
8+
import Control.Monad.IO.Class (liftIO)
9+
import Data.ByteString (ByteString)
10+
import Data.Int (Int64)
11+
import qualified Hasql.Connection as HsqlC
12+
import qualified Hasql.Decoders as HsqlD
13+
import qualified Hasql.Encoders as HsqlE
14+
import qualified Hasql.Session as HsqlS
15+
import qualified Hasql.Statement as HsqlS
16+
import qualified Hasql.Transaction as HsqlT
1317

14-
import Database.Esqueleto.Experimental
15-
import Database.PostgreSQL.Simple (SqlError)
18+
import Cardano.Db.Error (DbError (..), AsDbError (..))
19+
import Cardano.Db.Statement.Function.Core (mkCallSite)
20+
import Cardano.Prelude (ExceptT, MonadError (..), forM_)
1621

17-
enableJsonbInSchema ::
18-
forall m.
19-
( MonadBaseControl IO m
20-
, MonadIO m
21-
) =>
22-
ReaderT SqlBackend m ()
22+
enableJsonbInSchema :: HsqlT.Transaction ()
2323
enableJsonbInSchema = do
24-
handle exceptHandler $
25-
rawExecute
26-
"ALTER TABLE tx_metadata ALTER COLUMN json TYPE jsonb USING json::jsonb"
27-
[]
28-
handle exceptHandler $
29-
rawExecute
30-
"ALTER TABLE script ALTER COLUMN json TYPE jsonb USING json::jsonb"
31-
[]
32-
handle exceptHandler $
33-
rawExecute
34-
"ALTER TABLE datum ALTER COLUMN value TYPE jsonb USING value::jsonb"
35-
[]
36-
handle exceptHandler $
37-
rawExecute
38-
"ALTER TABLE redeemer_data ALTER COLUMN value TYPE jsonb USING value::jsonb"
39-
[]
40-
handle exceptHandler $
41-
rawExecute
42-
"ALTER TABLE cost_model ALTER COLUMN costs TYPE jsonb USING costs::jsonb"
43-
[]
44-
handle exceptHandler $
45-
rawExecute
46-
"ALTER TABLE gov_action_proposal ALTER COLUMN description TYPE jsonb USING description::jsonb"
47-
[]
48-
handle exceptHandler $
49-
rawExecute
50-
"ALTER TABLE off_chain_pool_data ALTER COLUMN json TYPE jsonb USING json::jsonb"
51-
[]
52-
handle exceptHandler $
53-
rawExecute
54-
"ALTER TABLE off_chain_vote_data ALTER COLUMN json TYPE jsonb USING json::jsonb"
55-
[]
24+
forM_ stmts $ \stmt -> HsqlT.statement () (enableJsonbInSchemaStmt stmt)
25+
where
26+
enableJsonbInSchemaStmt :: (ByteString, ByteString) -> HsqlS.Statement () ()
27+
enableJsonbInSchemaStmt (t, c) =
28+
HsqlS.Statement
29+
("ALTER TABLE " <> t <> " ALTER COLUMN " <> c <> " TYPE jsonb USING " <> c <> "::jsonb")
30+
HsqlE.noParams
31+
HsqlD.noResult
32+
True
33+
34+
stmts :: [(ByteString, ByteString)]
35+
stmts = [ ("tx_metadata", "json")
36+
, ("script", "json")
37+
, ("datum", "value")
38+
, ("redeemer_data", "value")
39+
, ("cost_model", "costs")
40+
, ("gov_action_proposal", "description")
41+
, ("off_chain_pool_data", "json")
42+
, ("off_chain_vote_data", "json")
43+
]
5644

57-
disableJsonbInSchema ::
58-
forall m.
59-
( MonadBaseControl IO m
60-
, MonadIO m
61-
) =>
62-
ReaderT SqlBackend m ()
45+
disableJsonbInSchema :: HsqlT.Transaction ()
6346
disableJsonbInSchema = do
64-
handle exceptHandler $
65-
rawExecute
66-
"ALTER TABLE tx_metadata ALTER COLUMN json TYPE VARCHAR"
67-
[]
68-
handle exceptHandler $
69-
rawExecute
70-
"ALTER TABLE script ALTER COLUMN json TYPE VARCHAR"
71-
[]
72-
handle exceptHandler $
73-
rawExecute
74-
"ALTER TABLE datum ALTER COLUMN value TYPE VARCHAR"
75-
[]
76-
handle exceptHandler $
77-
rawExecute
78-
"ALTER TABLE redeemer_data ALTER COLUMN value TYPE VARCHAR"
79-
[]
80-
handle exceptHandler $
81-
rawExecute
82-
"ALTER TABLE cost_model ALTER COLUMN costs TYPE VARCHAR"
83-
[]
84-
handle exceptHandler $
85-
rawExecute
86-
"ALTER TABLE gov_action_proposal ALTER COLUMN description TYPE VARCHAR"
87-
[]
88-
handle exceptHandler $
89-
rawExecute
90-
"ALTER TABLE off_chain_pool_data ALTER COLUMN json TYPE VARCHAR"
91-
[]
92-
handle exceptHandler $
93-
rawExecute
94-
"ALTER TABLE off_chain_vote_data ALTER COLUMN json TYPE VARCHAR"
95-
[]
47+
forM_ stmts $ \(t, c) -> HsqlT.statement () (disableJsonbInSchemaStmt t c)
48+
where
49+
disableJsonbInSchemaStmt t c = HsqlS.Statement
50+
("ALTER TABLE " <> t <> " ALTER COLUMN " <> c <> " TYPE VARCHAR")
51+
HsqlE.noParams
52+
HsqlD.noResult
53+
True
9654

97-
queryJsonbInSchemaExists ::
98-
MonadIO m =>
99-
ReaderT SqlBackend m Bool
100-
queryJsonbInSchemaExists = do
101-
isjsonb <- rawSql query []
102-
pure $ case isjsonb of
103-
[Single (1 :: Int)] -> True
104-
_other -> False
55+
stmts :: [(ByteString, ByteString)]
56+
stmts = [ ("tx_metadata", "json")
57+
, ("script", "json")
58+
, ("datum", "value")
59+
, ("redeemer_data", "value")
60+
, ("cost_model", "costs")
61+
, ("gov_action_proposal", "description")
62+
, ("off_chain_pool_data", "json")
63+
, ("off_chain_vote_data", "json")
64+
]
65+
66+
queryJsonbInSchemaExists :: AsDbError e => HsqlC.Connection -> ExceptT e IO Bool
67+
queryJsonbInSchemaExists conn = do
68+
result <- liftIO $ HsqlS.run (HsqlS.statement () jsonbSchemaStatement) conn
69+
case result of
70+
Left err -> throwError $ toDbError $ QueryError "queryJsonbInSchemaExists" mkCallSite err
71+
Right countRes -> pure $ countRes == 1
10572
where
106-
tableName = "'tx_metadata'"
107-
columnName = "'json'"
108-
-- check if the column is of type jsonb
73+
jsonbSchemaStatement :: HsqlS.Statement () Int64
74+
jsonbSchemaStatement =
75+
HsqlS.Statement
76+
query
77+
HsqlE.noParams -- No parameters needed
78+
decoder
79+
True -- Prepared statement
80+
10981
query =
110-
mconcat
111-
[ "SELECT COUNT(*) FROM information_schema.columns "
112-
, "WHERE table_name ="
113-
, tableName
114-
, "AND column_name ="
115-
, columnName
116-
, "AND data_type = 'jsonb';"
117-
]
82+
"SELECT COUNT(*) \
83+
\FROM information_schema.columns \
84+
\WHERE table_name = 'tx_metadata' \
85+
\AND column_name = 'json' \
86+
\AND data_type = 'jsonb';"
11887

119-
exceptHandler ::
120-
forall m a.
121-
MonadIO m =>
122-
SqlError ->
123-
ReaderT SqlBackend m a
124-
exceptHandler e =
125-
liftIO $ throwIO (DBRJsonbInSchema $ show e)
88+
decoder :: HsqlD.Result Int64
89+
decoder = HsqlD.singleRow $
90+
HsqlD.column $
91+
HsqlD.nonNullable HsqlD.int8

0 commit comments

Comments
 (0)