Skip to content

Commit a959c32

Browse files
committed
1 parent 3ae1645 commit a959c32

File tree

5 files changed

+36
-10
lines changed

5 files changed

+36
-10
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ rollbackFromBlockNo syncEnv blkNo = do
5353
DB.setNullTxOut trce mTxId
5454
DB.deleteEpochRows epochNo
5555
DB.deleteDrepDistr epochNo
56+
DB.deleteRewardRest epochNo
5657
DB.setNullEnacted epochNo
5758
DB.setNullRatified epochNo
5859
DB.setNullDropped epochNo

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module Cardano.Db.Delete (
1515
deleteBlock,
1616
deleteEpochRows,
1717
deleteDrepDistr,
18+
deleteRewardRest,
1819
deleteAdaPots,
1920
deleteTxOut,
2021
-- for testing
@@ -226,6 +227,10 @@ deleteDrepDistr :: MonadIO m => Word64 -> ReaderT SqlBackend m ()
226227
deleteDrepDistr epochNum =
227228
deleteWhere [DrepDistrEpochNo >. epochNum]
228229

230+
deleteRewardRest :: MonadIO m => Word64 -> ReaderT SqlBackend m ()
231+
deleteRewardRest epochNum =
232+
deleteWhere [RewardRestSpendableEpoch >. epochNum]
233+
229234
deleteAdaPots :: MonadIO m => BlockId -> ReaderT SqlBackend m ()
230235
deleteAdaPots blkId = do
231236
deleteWhere [AdaPotsBlockId ==. blkId]

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ insertManyRewardRests ::
214214
(MonadBaseControl IO m, MonadIO m) =>
215215
[RewardRest] ->
216216
ReaderT SqlBackend m ()
217-
insertManyRewardRests = insertManyCheckUnique "Many Rewards Rest"
217+
insertManyRewardRests = insertManyUnique "Many Rewards Rest" Nothing
218218

219219
insertManyDrepDistr ::
220220
(MonadBaseControl IO m, MonadIO m) =>
@@ -517,11 +517,10 @@ insertManyUnique ::
517517
) =>
518518
String ->
519519
-- | Does constraint already exists
520-
Bool ->
521-
ConstraintNameDB ->
520+
Maybe ConstraintNameDB ->
522521
[record] ->
523522
ReaderT SqlBackend m ()
524-
insertManyUnique vtype constraintExists constraintName records = do
523+
insertManyUnique vtype mConstraintName records = do
525524
unless (null records) $
526525
handle exceptHandler (rawExecute query values)
527526
where
@@ -546,14 +545,14 @@ insertManyUnique vtype constraintExists constraintName records = do
546545

547546
conflictQuery :: Text
548547
conflictQuery =
549-
if constraintExists
550-
then
548+
case mConstraintName of
549+
Just constraintName ->
551550
Text.concat
552551
[ " ON CONFLICT ON CONSTRAINT "
553552
, unConstraintNameDB constraintName
554553
, " DO NOTHING"
555554
]
556-
else ""
555+
_ -> ""
557556

558557
fieldNames, placeholders :: [Text]
559558
(fieldNames, placeholders) =
@@ -575,7 +574,10 @@ insertManyWithManualUnique ::
575574
ConstraintNameDB ->
576575
[record] ->
577576
ReaderT SqlBackend m ()
578-
insertManyWithManualUnique = insertManyUnique
577+
insertManyWithManualUnique str contraintExists constraintName =
578+
insertManyUnique str mConstraintName
579+
where
580+
mConstraintName = if contraintExists then Just constraintName else Nothing
579581

580582
insertManyCheckUnique ::
581583
forall m record.
@@ -588,7 +590,7 @@ insertManyCheckUnique ::
588590
ReaderT SqlBackend m ()
589591
insertManyCheckUnique vtype records = do
590592
let constraintName = uniqueDBName $ onlyOneUniqueDef (Proxy @record)
591-
insertManyUnique vtype True constraintName records
593+
insertManyUnique vtype (Just constraintName) records
592594

593595
-- Insert, getting PostgreSQL to check the uniqueness constaint. If it is violated,
594596
-- simply returns the Key, without changing anything.

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,6 @@ share
321321
amount DbLovelace sqltype=lovelace
322322
earnedEpoch Word64 generated="(CASE WHEN spendable_epoch >= 1 then spendable_epoch-1 else 0 end)"
323323
spendableEpoch Word64
324-
UniqueRewardRest addrId earnedEpoch type
325324
deriving Show
326325

327326
Withdrawal

schema/migration-2-0040-20240517.sql

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
-- Persistent generated migration.
2+
3+
CREATE FUNCTION migrate() RETURNS void AS $$
4+
DECLARE
5+
next_version int ;
6+
BEGIN
7+
SELECT stage_two + 1 INTO next_version FROM schema_version ;
8+
IF next_version = 40 THEN
9+
EXECUTE 'ALTER TABLE "reward_rest" DROP CONSTRAINT "unique_reward_rest"' ;
10+
-- Hand written SQL statements can be added here.
11+
UPDATE schema_version SET stage_two = next_version ;
12+
RAISE NOTICE 'DB has been migrated to stage_two version %', next_version ;
13+
END IF ;
14+
END ;
15+
$$ LANGUAGE plpgsql ;
16+
17+
SELECT migrate() ;
18+
19+
DROP FUNCTION migrate() ;

0 commit comments

Comments
 (0)