Skip to content

Commit 6b2ceba

Browse files
committed
add tests for force-index command line arg
1 parent 1a192a3 commit 6b2ceba

File tree

8 files changed

+83
-23
lines changed

8 files changed

+83
-23
lines changed

cardano-chain-gen/cardano-chain-gen.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ test-suite cardano-chain-gen
167167
Test.Cardano.Db.Mock.Unit.Babbage
168168
Test.Cardano.Db.Mock.Unit.Babbage.CommandLineArg.ConfigFile
169169
Test.Cardano.Db.Mock.Unit.Babbage.CommandLineArg.EpochDisabled
170+
Test.Cardano.Db.Mock.Unit.Babbage.CommandLineArg.ForceIndex
170171
Test.Cardano.Db.Mock.Unit.Babbage.CommandLineArg.MigrateConsumedPruneTxOut
171172
Test.Cardano.Db.Mock.Unit.Babbage.InlineAndReference
172173
Test.Cardano.Db.Mock.Unit.Babbage.Other

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

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ module Test.Cardano.Db.Mock.Unit.Babbage (
99
import Cardano.Mock.ChainSync.Server (IOManager)
1010
import Data.Text (Text)
1111
import Test.Tasty (TestTree, testGroup)
12-
import Test.Tasty.HUnit (Assertion, testCase)
1312
import Test.Tasty.ExpectedFailure (expectFail)
13+
import Test.Tasty.HUnit (Assertion, testCase)
1414

15-
import qualified Test.Cardano.Db.Mock.Unit.Babbage.CommandLineArg.MigrateConsumedPruneTxOut as MigrateConsumedPruneTxOut
16-
import qualified Test.Cardano.Db.Mock.Unit.Babbage.CommandLineArg.EpochDisabled as EpochDisabled
1715
import qualified Test.Cardano.Db.Mock.Unit.Babbage.CommandLineArg.ConfigFile as ConfigFile
16+
import qualified Test.Cardano.Db.Mock.Unit.Babbage.CommandLineArg.EpochDisabled as EpochDisabled
17+
import qualified Test.Cardano.Db.Mock.Unit.Babbage.CommandLineArg.ForceIndex as ForceIndex
18+
import qualified Test.Cardano.Db.Mock.Unit.Babbage.CommandLineArg.MigrateConsumedPruneTxOut as MigrateConsumedPruneTxOut
1819
import qualified Test.Cardano.Db.Mock.Unit.Babbage.InlineAndReference as BabInlineRef
1920
import qualified Test.Cardano.Db.Mock.Unit.Babbage.Other as BabOther
2021
import qualified Test.Cardano.Db.Mock.Unit.Babbage.Plutus as BabPlutus
@@ -40,25 +41,31 @@ unitTests iom knownMigrations =
4041
, testGroup
4142
"Command Line Arguments"
4243
[ testGroup
43-
"MigrateConsumed - PruneTxOut"
44-
[ test "flag check" MigrateConsumedPruneTxOut.commandLineArgCheck
45-
, test "basic prune" MigrateConsumedPruneTxOut.basicPrune
46-
, test "prune with simple rollback" MigrateConsumedPruneTxOut.pruneWithSimpleRollback
47-
, test "prune with full tx rollback" MigrateConsumedPruneTxOut.pruneWithFullTxRollback
48-
, test "pruning should keep some tx" MigrateConsumedPruneTxOut.pruningShouldKeepSomeTx
49-
, test "prune and rollback one block" MigrateConsumedPruneTxOut.pruneAndRollBackOneBlock
50-
, test "no pruning and rollback" MigrateConsumedPruneTxOut.noPruneAndRollBack
51-
, test "prune same block" MigrateConsumedPruneTxOut.pruneSameBlock
52-
, test "no pruning same block" MigrateConsumedPruneTxOut.noPruneSameBlock
53-
]
44+
"consumed-tx-out + prune-tx-out"
45+
[ test "flag check" MigrateConsumedPruneTxOut.commandLineArgCheck
46+
, test "basic prune" MigrateConsumedPruneTxOut.basicPrune
47+
, test "prune with simple rollback" MigrateConsumedPruneTxOut.pruneWithSimpleRollback
48+
, test "prune with full tx rollback" MigrateConsumedPruneTxOut.pruneWithFullTxRollback
49+
, test "pruning should keep some tx" MigrateConsumedPruneTxOut.pruningShouldKeepSomeTx
50+
, test "prune and rollback one block" MigrateConsumedPruneTxOut.pruneAndRollBackOneBlock
51+
, test "no pruning and rollback" MigrateConsumedPruneTxOut.noPruneAndRollBack
52+
, test "prune same block" MigrateConsumedPruneTxOut.pruneSameBlock
53+
, test "no pruning same block" MigrateConsumedPruneTxOut.noPruneSameBlock
54+
]
5455
, testGroup
55-
"ConfigFile"
56-
[ expectFail $ test "fails if incorrect or no config file given" ConfigFile.checkConfigFileArg ]
56+
"config"
57+
[ expectFail $ test "fails if incorrect or no config file given" ConfigFile.checkConfigFileArg
58+
]
5759
, testGroup
58-
"EpochDisabled"
60+
"disable-epoch"
5961
[ test "Epoch doesn't update when disabled" EpochDisabled.checkEpochDisabledArg
6062
, test "Epoch updates when enabled" EpochDisabled.checkEpochEnabled
6163
]
64+
, testGroup
65+
"force-indexes"
66+
[ test "check force-index adds indexes" ForceIndex.checkForceIndexesArg
67+
, test "check no force-index doesn't add indexes" ForceIndex.checkNoForceIndexesArg
68+
]
6269
]
6370
, testGroup
6471
"rollbacks"

cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Babbage/CommandLineArg/ConfigFile.hs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@ import Data.Text (Text)
88
import Test.Cardano.Db.Mock.Config (CommandLineArgs (..), babbageConfigDir, initCommandLineArgs, withCustomConfigAndLogs)
99
import Test.Tasty.HUnit (Assertion)
1010

11-
commandLineArgs :: CommandLineArgs
12-
commandLineArgs = initCommandLineArgs {claHasConfigFile = False}
13-
1411
-- this test fails as incorrect configuration file given
1512
checkConfigFileArg :: IOManager -> [(Text, Text)] -> Assertion
1613
checkConfigFileArg =
17-
withCustomConfigAndLogs commandLineArgs babbageConfigDir testLabel $ \_ _ _ -> do
14+
withCustomConfigAndLogs commandLineConfigArgs babbageConfigDir testLabel $ \_ _ _ -> do
1815
pure ()
1916
where
2017
testLabel = "CLAcheckConfigFileArg"
18+
commandLineConfigArgs = initCommandLineArgs {claHasConfigFile = False}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{-# LANGUAGE NumericUnderscores #-}
2+
3+
module Test.Cardano.Db.Mock.Unit.Babbage.CommandLineArg.ForceIndex (
4+
checkForceIndexesArg,
5+
checkNoForceIndexesArg
6+
)
7+
where
8+
9+
import Cardano.Mock.ChainSync.Server (IOManager)
10+
import Data.Text (Text)
11+
import Test.Cardano.Db.Mock.Config (CommandLineArgs (..), babbageConfigDir, initCommandLineArgs, withCustomConfig, startDBSync)
12+
import Test.Tasty.HUnit (Assertion)
13+
import qualified Cardano.Db as DB
14+
import Test.Cardano.Db.Mock.Validate (assertEqQuery)
15+
import GHC.Conc.IO (threadDelay)
16+
17+
checkForceIndexesArg :: IOManager -> [(Text, Text)] -> Assertion
18+
checkForceIndexesArg =
19+
withCustomConfig commandLineForceIndexArgs babbageConfigDir testLabel $ \_ _ dbSyncEnv -> do
20+
startDBSync dbSyncEnv
21+
-- assertBlockNoBackoff dbSyncEnv 0
22+
threadDelay 3_000_000
23+
assertEqQuery dbSyncEnv DB.queryPgIndexesCount 143 "there wasn't the correct number of indexes"
24+
where
25+
testLabel = "CLAcheckForceIndexesArg"
26+
commandLineForceIndexArgs = initCommandLineArgs {claForceIndexes = True}
27+
28+
checkNoForceIndexesArg :: IOManager -> [(Text, Text)] -> Assertion
29+
checkNoForceIndexesArg =
30+
withCustomConfig commandLineNoForceIndexArgs babbageConfigDir testLabel $ \_ _ dbSyncEnv -> do
31+
startDBSync dbSyncEnv
32+
-- assertBlockNoBackoff dbSyncEnv 0
33+
threadDelay 3_000_000
34+
assertEqQuery dbSyncEnv DB.queryPgIndexesCount 78 "there wasn't the correct number of indexes"
35+
where
36+
testLabel = "CLAcheckNoForceIndexesArg"
37+
commandLineNoForceIndexArgs = initCommandLineArgs {claForceIndexes = False}

cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Babbage/CommandLineArg/MigrateConsumedPruneTxOut.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ pruneWithSimpleRollback = do
103103
assertEqQuery dbSyncEnv DB.queryTxOutCount 14 ""
104104
b1 <- forgeAndSubmitBlocks interpreter mockServer 96
105105
assertBlockNoBackoff dbSyncEnv (fullBlockSize b1)
106-
assertEqQuery dbSyncEnv DB.queryTxOutCount 12 "there wasn't the correct "
106+
assertEqQuery dbSyncEnv DB.queryTxOutCount 12 "the txOut count is incorrect"
107107
assertEqQuery dbSyncEnv DB.queryTxOutConsumedCount 0 "Unexpected TxOutConsumedByTxInId count after prune"
108108
assertUnspentTx dbSyncEnv
109109

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{-# LANGUAGE LambdaCase #-}
22
{-# LANGUAGE OverloadedStrings #-}
3+
{-# LANGUAGE ScopedTypeVariables #-}
34

45
module Cardano.Db.Migration (
56
MigrationDir (..),
@@ -18,6 +19,7 @@ module Cardano.Db.Migration (
1819
hashMigrations,
1920
renderMigrationValidateError,
2021
noLedgerMigrations,
22+
queryPgIndexesCount,
2123
) where
2224

2325
import Cardano.BM.Trace (Trace)
@@ -31,7 +33,7 @@ import Cardano.Db.Schema
3133
import Cardano.Db.Text
3234
import Control.Exception (SomeException, handle)
3335
import Control.Monad.Extra
34-
import Control.Monad.IO.Class (liftIO)
36+
import Control.Monad.IO.Class (MonadIO, liftIO)
3537
import Control.Monad.Logger (NoLoggingT)
3638
import Control.Monad.Trans.Reader (ReaderT)
3739
import Control.Monad.Trans.Resource (runResourceT)
@@ -58,6 +60,7 @@ import Database.Persist.Sql (
5860
rawSql,
5961
selectFirst,
6062
)
63+
import GHC.Word (Word64)
6164
import System.Directory (listDirectory)
6265
import System.Exit (ExitCode (..), exitFailure)
6366
import System.FilePath (takeExtension, takeFileName, (</>))
@@ -378,3 +381,15 @@ noLedgerMigrations backend trce = do
378381
rawExecute "delete from epoch_stake" []
379382
rawExecute "delete from ada_pots" []
380383
rawExecute "delete from epoch_param" []
384+
385+
queryPgIndexesCount :: MonadIO m => ReaderT SqlBackend m Word64
386+
queryPgIndexesCount = do
387+
indexesExists :: [Text] <-
388+
fmap unSingle
389+
<$> rawSql
390+
( mconcat
391+
[ "SELECT indexname FROM pg_indexes WHERE schemaname = 'public'"
392+
]
393+
)
394+
[]
395+
pure $ fromIntegral (length indexesExists)

0 commit comments

Comments
 (0)