Skip to content

Commit 236a470

Browse files
dcrpg:remove duplicates for votes, misses and treasury table
Signed-off-by: Philemon Ukane <[email protected]>
1 parent 789b090 commit 236a470

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed

db/dcrpg/indexing.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,18 @@ func (pgb *ChainDB) deleteDuplicateAgendaVotes() (int64, error) {
394394
return deleteDuplicateAgendaVotes(pgb.db)
395395
}
396396

397+
func (pgb *ChainDB) DeleteDuplicateVotes() (int64, error) {
398+
return DeleteDuplicateVotes(pgb.db)
399+
}
400+
401+
func (pgb *ChainDB) DeleteDuplicateMisses() (int64, error) {
402+
return DeleteDuplicateMisses(pgb.db)
403+
}
404+
405+
func (pgb *ChainDB) DeleteDuplicateTreasuryTxs() (int64, error) {
406+
return DeleteDuplicateTreasuryTxs(pgb.db)
407+
}
408+
397409
// Indexes checks
398410

399411
// MissingIndexes lists missing table indexes and their descriptions.

db/dcrpg/internal/stakestmts.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,35 @@ const (
414414
FROM agenda_votes) t
415415
WHERE t.rnum > 1);`
416416

417+
// DeleteVotesDuplicateRows removes rows that would violate the unique
418+
// index uix_votes_hashes_index. This should be run prior to creating the index.
419+
DeleteVotesDuplicateRows = `DELETE FROM votes
420+
WHERE id IN (SELECT id FROM (
421+
SELECT id,
422+
row_number() OVER (PARTITION BY tx_hash, block_hash ORDER BY id DESC) AS rnum
423+
FROM votes) t
424+
WHERE t.rnum > 1);`
425+
426+
// DeleteMissesDuplicateRows removes rows that would violate the unique
427+
// index uix_misses_hashes_index. This should be run prior to creating the index.
428+
DeleteMissesDuplicateRows = `DELETE FROM misses
429+
WHERE id IN (SELECT id FROM (
430+
SELECT id,
431+
row_number() OVER (PARTITION BY ticket_hash, block_hash ORDER BY id DESC) AS rnum
432+
FROM misses) t
433+
WHERE t.rnum > 1);`
434+
435+
// DeleteTreasuryTxsDuplicateRows removes rows that would violate the unique
436+
// index uix_treasury_tx_hash. This should be run prior to creating the index.
437+
DeleteTreasuryTxsDuplicateRows = `DELETE FROM treasury
438+
WHERE (tx_hash, block_hash) IN (SELECT tx_hash, block_hash FROM (
439+
SELECT
440+
tx_hash,
441+
block_hash,
442+
row_number() OVER (PARTITION BY tx_hash, block_hash) AS rnum
443+
FROM treasury) t
444+
WHERE t.rnum > 1);`
445+
417446
// Select
418447

419448
SelectAgendasVotesByTime = `SELECT votes.block_time AS timestamp,` +

db/dcrpg/queries.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,42 @@ func deleteDuplicateAgendaVotes(db *sql.DB) (int64, error) {
257257
return sqlExec(db, internal.DeleteAgendaVotesDuplicateRows, execErrPrefix)
258258
}
259259

260+
// DeleteDuplicateVotes deletes rows in votes with duplicate tx_hash and
261+
// block_hash leaving one row with the lowest id.
262+
func DeleteDuplicateVotes(db *sql.DB) (int64, error) {
263+
if isuniq, err := IsUniqueIndex(db, internal.IndexOfVotesTableOnHashes); err != nil && err != sql.ErrNoRows {
264+
return 0, err
265+
} else if isuniq {
266+
return 0, nil
267+
}
268+
execErrPrefix := "failed to delete duplicate votes: "
269+
return sqlExec(db, internal.DeleteVotesDuplicateRows, execErrPrefix)
270+
}
271+
272+
// DeleteDuplicateMisses deletes rows in misses with duplicate ticket_hash and
273+
// block_hash leaving one row with the lowest id.
274+
func DeleteDuplicateMisses(db *sql.DB) (int64, error) {
275+
if isuniq, err := IsUniqueIndex(db, internal.IndexOfMissesTableOnHashes); err != nil && err != sql.ErrNoRows {
276+
return 0, err
277+
} else if isuniq {
278+
return 0, nil
279+
}
280+
execErrPrefix := "failed to delete duplicate misses: "
281+
return sqlExec(db, internal.DeleteMissesDuplicateRows, execErrPrefix)
282+
}
283+
284+
// DeleteDuplicateTreasuryTxs deletes rows in misses with duplicate tx_hash and
285+
// block_hash leaving one row with the lowest id.
286+
func DeleteDuplicateTreasuryTxs(db *sql.DB) (int64, error) {
287+
if isuniq, err := IsUniqueIndex(db, internal.IndexOfTreasuryTableOnTxHash); err != nil && err != sql.ErrNoRows {
288+
return 0, err
289+
} else if isuniq {
290+
return 0, nil
291+
}
292+
execErrPrefix := "failed to delete duplicate treasury txs: "
293+
return sqlExec(db, internal.DeleteTreasuryTxsDuplicateRows, execErrPrefix)
294+
}
295+
260296
// --- stake (votes, tickets, misses, treasury) tables ---
261297

262298
func insertTreasuryTxns(db *sql.DB, dbTxns []*dbtypes.Tx, checked, updateExistingRecords bool) error {

db/dcrpg/tables.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,15 @@ func (pgb *ChainDB) deleteDuplicates(barLoad chan *dbtypes.ProgressBarLoad) erro
218218

219219
// Remove duplicate agenda_votes
220220
{TableName: "agenda_votes", DropDupsFunc: pgb.deleteDuplicateAgendaVotes},
221+
222+
// Remove duplicate votes
223+
{TableName: "votes", DropDupsFunc: pgb.DeleteDuplicateVotes},
224+
225+
// Remove duplicate misses
226+
{TableName: "misses", DropDupsFunc: pgb.DeleteDuplicateMisses},
227+
228+
// Remove duplicate treasury txs
229+
{TableName: "treasury", DropDupsFunc: pgb.DeleteDuplicateTreasuryTxs},
221230
}
222231

223232
var err error

0 commit comments

Comments
 (0)