Skip to content

Commit ec0431d

Browse files
dcrpg:remove duplicates for votes, misses and treasury table
Signed-off-by: Philemon Ukane <ukanephilemon@gmail.com>
1 parent 0b6dbac commit ec0431d

4 files changed

Lines changed: 86 additions & 0 deletions

File tree

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
@@ -410,6 +410,35 @@ const (
410410
FROM agenda_votes) t
411411
WHERE t.rnum > 1);`
412412

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

415444
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
@@ -270,6 +270,42 @@ func DeleteDuplicateAgendaVotes(db *sql.DB) (int64, error) {
270270
return sqlExec(db, internal.DeleteAgendaVotesDuplicateRows, execErrPrefix)
271271
}
272272

273+
// DeleteDuplicateVotes deletes rows in votes with duplicate tx_hash and
274+
// block_hash leaving one row with the lowest id.
275+
func DeleteDuplicateVotes(db *sql.DB) (int64, error) {
276+
if isuniq, err := IsUniqueIndex(db, internal.IndexOfVotesTableOnHashes); err != nil && err != sql.ErrNoRows {
277+
return 0, err
278+
} else if isuniq {
279+
return 0, nil
280+
}
281+
execErrPrefix := "failed to delete duplicate votes: "
282+
return sqlExec(db, internal.DeleteVotesDuplicateRows, execErrPrefix)
283+
}
284+
285+
// DeleteDuplicateMisses deletes rows in misses with duplicate ticket_hash and
286+
// block_hash leaving one row with the lowest id.
287+
func DeleteDuplicateMisses(db *sql.DB) (int64, error) {
288+
if isuniq, err := IsUniqueIndex(db, internal.IndexOfMissesTableOnHashes); err != nil && err != sql.ErrNoRows {
289+
return 0, err
290+
} else if isuniq {
291+
return 0, nil
292+
}
293+
execErrPrefix := "failed to delete duplicate misses: "
294+
return sqlExec(db, internal.DeleteMissesDuplicateRows, execErrPrefix)
295+
}
296+
297+
// DeleteDuplicateTreasuryTxs deletes rows in misses with duplicate tx_hash and
298+
// block_hash leaving one row with the lowest id.
299+
func DeleteDuplicateTreasuryTxs(db *sql.DB) (int64, error) {
300+
if isuniq, err := IsUniqueIndex(db, internal.IndexOfTreasuryTableOnTxHash); err != nil && err != sql.ErrNoRows {
301+
return 0, err
302+
} else if isuniq {
303+
return 0, nil
304+
}
305+
execErrPrefix := "failed to delete duplicate treasury txs: "
306+
return sqlExec(db, internal.DeleteTreasuryTxsDuplicateRows, execErrPrefix)
307+
}
308+
273309
// --- stake (votes, tickets, misses, treasury) tables ---
274310

275311
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
@@ -214,6 +214,15 @@ func (pgb *ChainDB) DeleteDuplicates(barLoad chan *dbtypes.ProgressBarLoad) erro
214214

215215
// Remove duplicate agenda_votes
216216
{TableName: "agenda_votes", DropDupsFunc: pgb.DeleteDuplicateAgendaVotes},
217+
218+
// Remove duplicate votes
219+
{TableName: "votes", DropDupsFunc: pgb.DeleteDuplicateVotes},
220+
221+
// Remove duplicate misses
222+
{TableName: "misses", DropDupsFunc: pgb.DeleteDuplicateMisses},
223+
224+
// Remove duplicate treasury txs
225+
{TableName: "treasury", DropDupsFunc: pgb.DeleteDuplicateTreasuryTxs},
217226
}
218227

219228
var err error

0 commit comments

Comments
 (0)