Skip to content

Commit 94f866e

Browse files
authored
refactor(database): simplify account upsert patterns/use (#1055)
Signed-off-by: Chris Gianelloni <wolf31o2@blinklabs.io>
1 parent b30d405 commit 94f866e

File tree

1 file changed

+7
-30
lines changed

1 file changed

+7
-30
lines changed

database/plugin/metadata/sqlite/transaction.go

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ func (d *MetadataStoreSqlite) getOrCreateAccount(
8686
// saveAccountIfNew saves the account if it doesn't exist in the database yet
8787
func saveAccountIfNew(account *models.Account, txn *gorm.DB) error {
8888
if account.ID == 0 {
89-
result := txn.Create(account)
89+
result := txn.Clauses(clause.OnConflict{
90+
Columns: []clause.Column{{Name: "staking_key"}},
91+
UpdateAll: true,
92+
}).Create(account)
9093
if result.Error != nil {
9194
return result.Error
9295
}
@@ -509,7 +512,7 @@ func (d *MetadataStoreSqlite) SetTransaction(
509512
result := txn.Clauses(clause.OnConflict{
510513
Columns: []clause.Column{{Name: "staking_key"}},
511514
UpdateAll: true,
512-
}).Create(&tmpAccount)
515+
}).Create(tmpAccount)
513516
if result.Error != nil {
514517
return fmt.Errorf("process certificate: %w", result.Error)
515518
}
@@ -530,23 +533,10 @@ func (d *MetadataStoreSqlite) SetTransaction(
530533
}
531534
case *lcommon.StakeDelegationCertificate:
532535
stakeKey := c.StakeCredential.Credential[:]
533-
tmpAccount, err := d.GetAccount(stakeKey, txn)
536+
tmpAccount, err := d.getOrCreateAccount(stakeKey, txn)
534537
if err != nil {
535538
return fmt.Errorf("process certificate: %w", err)
536539
}
537-
if tmpAccount == nil {
538-
d.logger.Warn("delegating non-existent account", "hash", stakeKey)
539-
tmpAccount = &models.Account{
540-
StakingKey: stakeKey,
541-
}
542-
result := txn.Clauses(clause.OnConflict{
543-
Columns: []clause.Column{{Name: "staking_key"}},
544-
UpdateAll: true,
545-
}).Create(&tmpAccount)
546-
if result.Error != nil {
547-
return fmt.Errorf("process certificate: %w", result.Error)
548-
}
549-
}
550540

551541
tmpAccount.Pool = c.PoolKeyHash[:]
552542

@@ -707,23 +697,10 @@ func (d *MetadataStoreSqlite) SetTransaction(
707697
}
708698
case *lcommon.VoteDelegationCertificate:
709699
stakeKey := c.StakeCredential.Credential[:]
710-
tmpAccount, err := d.GetAccount(stakeKey, txn)
700+
tmpAccount, err := d.getOrCreateAccount(stakeKey, txn)
711701
if err != nil {
712702
return fmt.Errorf("process certificate: %w", err)
713703
}
714-
if tmpAccount == nil {
715-
d.logger.Warn("delegating vote for non-existent account", "hash", stakeKey)
716-
tmpAccount = &models.Account{
717-
StakingKey: stakeKey,
718-
}
719-
result := txn.Clauses(clause.OnConflict{
720-
Columns: []clause.Column{{Name: "staking_key"}},
721-
UpdateAll: true,
722-
}).Create(&tmpAccount)
723-
if result.Error != nil {
724-
return fmt.Errorf("process certificate: %w", result.Error)
725-
}
726-
}
727704

728705
tmpAccount.Drep = c.Drep.Credential[:]
729706

0 commit comments

Comments
 (0)