Skip to content

Commit

Permalink
Fix: on conflict save (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky authored May 13, 2022
1 parent ec9de9f commit 345fb2f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
15 changes: 14 additions & 1 deletion cmd/metadata/models/contract_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,20 @@ func (contracts *Contracts) Save(metadata []*ContractMetadata) error {
if len(metadata) == 0 {
return nil
}
_, err := contracts.db.DB().Model(&metadata).

savings := make([]*ContractMetadata, 0)
has := make(map[string]struct{})
for i := len(metadata) - 1; i >= 0; i-- {
if _, ok := has[metadata[i].Contract]; !ok {
has[metadata[i].Contract] = struct{}{}
savings = append(savings, metadata[i])
}
}
if len(savings) == 0 {
return nil
}

_, err := contracts.db.DB().Model(&savings).
OnConflict("(network, contract) DO UPDATE").
Set("metadata = excluded.metadata, link = excluded.link, update_id = excluded.update_id, status = excluded.status").
Insert()
Expand Down
17 changes: 16 additions & 1 deletion cmd/metadata/models/token_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package models

import (
"context"
"fmt"
"time"

"github.com/dipdup-net/go-lib/database"
Expand Down Expand Up @@ -137,7 +138,21 @@ func (tokens *Tokens) Save(metadata []*TokenMetadata) error {
return nil
}

_, err := tokens.db.DB().Model(&metadata).
savings := make([]*TokenMetadata, 0)
has := make(map[string]struct{})
for i := len(metadata) - 1; i >= 0; i-- {
id := fmt.Sprintf("%s_%d", metadata[i].Contract, metadata[i].TokenID)
if _, ok := has[id]; !ok {
has[id] = struct{}{}
savings = append(savings, metadata[i])
}
}

if len(savings) == 0 {
return nil
}

_, err := tokens.db.DB().Model(&savings).
OnConflict("(network, contract, token_id) DO UPDATE").
Set("metadata = excluded.metadata, link = excluded.link, update_id = excluded.update_id, status = excluded.status").
Insert()
Expand Down

0 comments on commit 345fb2f

Please sign in to comment.