Skip to content

Commit c615bd2

Browse files
authored
Merge pull request #262 from input-output-hk/erikd/pool-owner-validation
db-tool: Add validation that all pools have owners
2 parents 5889e9d + 482a6d4 commit c615bd2

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module Cardano.Db.App.Validate.PoolOwner
2+
( validateAllPoolsHaveOwners
3+
) where
4+
5+
import Cardano.Db.App.Validate.Util
6+
7+
import Cardano.Db
8+
9+
import Control.Monad.IO.Class (MonadIO)
10+
import Control.Monad.Trans.Reader (ReaderT)
11+
12+
import Data.Maybe (fromMaybe)
13+
14+
import Database.Esqueleto (Value (..), (^.), (==.),
15+
countRows, from, notExists, select, unValue, where_)
16+
17+
import Database.Persist.Sql (SqlBackend)
18+
19+
20+
validateAllPoolsHaveOwners :: IO ()
21+
validateAllPoolsHaveOwners = do
22+
putStrF $ "All pools have owners :"
23+
count <- runDbNoLogging queryPoolsWithoutOwners
24+
if count == 0
25+
then putStrLn $ greenText "ok"
26+
else putStrLn $ redText ("Failed, " ++ show count ++ " pools are without owners.")
27+
28+
-- -----------------------------------------------------------------------------
29+
30+
-- select * from pool_hash
31+
-- where not exists (select * from pool_owner where pool_owner.pool_id = pool_hash.id) ;
32+
33+
queryPoolsWithoutOwners :: MonadIO m => ReaderT SqlBackend m Int
34+
queryPoolsWithoutOwners = do
35+
res <- select . from $ \ phash -> do
36+
where_ . notExists . from $ \ powner -> do
37+
where_ (phash ^. PoolHashId ==. powner ^. PoolOwnerPoolHashId)
38+
pure countRows
39+
pure $ fromMaybe 0 (unValue <$> listToMaybe res)

cardano-db/app/Cardano/Db/App/Validation.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ module Cardano.Db.App.Validation
22
( runValidation
33
) where
44

5+
import Cardano.Db.App.Validate.BlockTxs (validateEpochBlockTxs)
56
import Cardano.Db.App.Validate.EpochTable (validateEpochTable)
67
import Cardano.Db.App.Validate.TotalSupply (validateTotalSupplyDecreasing)
7-
import Cardano.Db.App.Validate.BlockTxs (validateEpochBlockTxs)
8+
import Cardano.Db.App.Validate.PoolOwner (validateAllPoolsHaveOwners)
89

910
runValidation :: IO ()
1011
runValidation = do
1112
validateTotalSupplyDecreasing
1213
validateEpochTable
1314
validateEpochBlockTxs
15+
validateAllPoolsHaveOwners

cardano-db/cardano-db.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ executable cardano-db-tool
9898
Cardano.Db.App.Validation
9999
Cardano.Db.App.Validate.BlockTxs
100100
Cardano.Db.App.Validate.EpochTable
101+
Cardano.Db.App.Validate.PoolOwner
101102
Cardano.Db.App.Validate.TotalSupply
102103
Cardano.Db.App.Validate.Util
103104

0 commit comments

Comments
 (0)