@@ -50,6 +50,8 @@ import Cardano.Pool.DB
5050 ( DBLayer (.. ), ErrPointAlreadyExists (.. ), determinePoolLifeCycleStatus )
5151import Cardano.Pool.DB.Log
5252 ( ParseFailure (.. ), PoolDbLog (.. ) )
53+ import Cardano.Pool.DB.Sqlite.TH hiding
54+ ( BlockHeader , blockHeight )
5355import Cardano.Wallet.DB.Sqlite.Types
5456 ( BlockId (.. ) )
5557import Cardano.Wallet.Logging
@@ -60,7 +62,7 @@ import Cardano.Wallet.Primitive.Types
6062 ( BlockHeader (.. )
6163 , CertificatePublicationTime (.. )
6264 , EpochNo (.. )
63- , PoolId
65+ , PoolId ( .. )
6466 , PoolLifeCycleStatus (.. )
6567 , PoolRegistrationCertificate (.. )
6668 , PoolRetirementCertificate (.. )
@@ -96,8 +98,8 @@ import Data.Quantity
9698 ( Percentage (.. ), Quantity (.. ) )
9799import Data.Ratio
98100 ( denominator , numerator , (%) )
99- import Data.String.QQ
100- ( s )
101+ import Data.String.Interpolate
102+ ( i )
101103import Data.Text
102104 ( Text )
103105import Data.Time.Clock
@@ -120,7 +122,9 @@ import Database.Persist.Sql
120122 , selectFirst
121123 , selectList
122124 , toPersistValue
125+ , update
123126 , (<.)
127+ , (=.)
124128 , (==.)
125129 , (>.)
126130 , (>=.)
@@ -134,9 +138,6 @@ import System.FilePath
134138import System.Random
135139 ( newStdGen )
136140
137- import Cardano.Pool.DB.Sqlite.TH hiding
138- ( BlockHeader , blockHeight )
139-
140141import qualified Cardano.Pool.DB.Sqlite.TH as TH
141142import qualified Cardano.Wallet.Primitive.Types as W
142143import qualified Data.Map.Strict as Map
@@ -393,7 +394,8 @@ newDBLayer trace fp timeInterpreter = do
393394 (PoolMetadataFetchAttempts hash url retryAfter $ retryCount + 1 )
394395
395396 putPoolMetadata hash metadata = do
396- let StakePoolMetadata {ticker,name,description,homepage} = metadata
397+ let StakePoolMetadata
398+ {ticker, name, description, homepage} = metadata
397399 repsert
398400 (PoolMetadataKey hash)
399401 (PoolMetadata hash name ticker description homepage)
@@ -489,6 +491,13 @@ newDBLayer trace fp timeInterpreter = do
489491 deleteWhere [ BlockSlot >. point ]
490492 -- TODO: remove dangling metadata no longer attached to a pool
491493
494+ putDelistedPools pools = do
495+ deleteWhere ([] :: [Filter PoolDelistment ])
496+ insertMany_ $ fmap PoolDelistment pools
497+
498+ readDelistedPools =
499+ fmap (delistedPoolId . entityVal) <$> selectList [] []
500+
492501 removePools = mapM_ $ \ pool -> do
493502 liftIO $ traceWith trace $ MsgRemovingPool pool
494503 deleteWhere [ PoolProductionPoolId ==. pool ]
@@ -541,16 +550,33 @@ newDBLayer trace fp timeInterpreter = do
541550 (SettingsKey 1 )
542551 . toSettings
543552
553+ readLastMetadataGC = do
554+ -- only ever read the first row
555+ result <- selectFirst
556+ []
557+ [Asc InternalStateId , LimitTo 1 ]
558+ pure $ (W. lastMetadataGC . fromInternalState . entityVal) =<< result
559+
560+ putLastMetadataGC utc = do
561+ result <- selectFirst
562+ [ InternalStateId ==. (InternalStateKey 1 ) ]
563+ [ ]
564+ case result of
565+ Just _ -> update (InternalStateKey 1 ) [ LastGCMetadata =. Just utc ]
566+ Nothing -> insert_ (InternalState $ Just utc)
567+
544568 cleanDB = do
545569 deleteWhere ([] :: [Filter PoolProduction ])
546570 deleteWhere ([] :: [Filter PoolOwner ])
547571 deleteWhere ([] :: [Filter PoolRegistration ])
548572 deleteWhere ([] :: [Filter PoolRetirement ])
573+ deleteWhere ([] :: [Filter PoolDelistment ])
549574 deleteWhere ([] :: [Filter StakeDistribution ])
550575 deleteWhere ([] :: [Filter PoolMetadata ])
551576 deleteWhere ([] :: [Filter PoolMetadataFetchAttempts ])
552577 deleteWhere ([] :: [Filter TH. BlockHeader ])
553578 deleteWhere ([] :: [Filter Settings ])
579+ deleteWhere ([] :: [Filter InternalState ])
554580
555581 atomically :: forall a . (SqlPersistT IO a -> IO a )
556582 atomically = runQuery
@@ -706,7 +732,7 @@ createView conn (DatabaseView name definition) = do
706732-- This view does NOT exclude pools that have retired.
707733--
708734activePoolLifeCycleData :: DatabaseView
709- activePoolLifeCycleData = DatabaseView " active_pool_lifecycle_data" [s |
735+ activePoolLifeCycleData = DatabaseView " active_pool_lifecycle_data" [i |
710736 SELECT
711737 active_pool_registrations.pool_id as pool_id,
712738 active_pool_retirements.retirement_epoch as retirement_epoch,
@@ -735,7 +761,7 @@ activePoolLifeCycleData = DatabaseView "active_pool_lifecycle_data" [s|
735761-- This view does NOT exclude pools that have retired.
736762--
737763activePoolOwners :: DatabaseView
738- activePoolOwners = DatabaseView " active_pool_owners" [s |
764+ activePoolOwners = DatabaseView " active_pool_owners" [i |
739765 SELECT pool_id, pool_owners FROM (
740766 SELECT row_number() OVER w AS r, *
741767 FROM (
@@ -763,7 +789,7 @@ activePoolOwners = DatabaseView "active_pool_owners" [s|
763789-- This view does NOT exclude pools that have retired.
764790--
765791activePoolRegistrations :: DatabaseView
766- activePoolRegistrations = DatabaseView " active_pool_registrations" [s |
792+ activePoolRegistrations = DatabaseView " active_pool_registrations" [i |
767793 SELECT
768794 pool_id,
769795 cost,
@@ -793,7 +819,7 @@ activePoolRegistrations = DatabaseView "active_pool_registrations" [s|
793819-- certificates revoked by subsequent re-registration certificates.
794820--
795821activePoolRetirements :: DatabaseView
796- activePoolRetirements = DatabaseView " active_pool_retirements" [s |
822+ activePoolRetirements = DatabaseView " active_pool_retirements" [i |
797823 SELECT * FROM (
798824 SELECT
799825 pool_id,
@@ -973,3 +999,7 @@ toSettings
973999 -> Settings
9741000toSettings (W. Settings pms) = Settings pms
9751001
1002+ fromInternalState
1003+ :: InternalState
1004+ -> W. InternalState
1005+ fromInternalState (InternalState utc) = W. InternalState utc
0 commit comments