From 345fb2f81d0ab6e9d8d1f41e51ece21ab4af1be6 Mon Sep 17 00:00:00 2001 From: Artem Poltorzhitskiy Date: Sat, 14 May 2022 00:07:04 +0300 Subject: [PATCH] Fix: on conflict save (#31) --- cmd/metadata/models/contract_metadata.go | 15 ++++++++++++++- cmd/metadata/models/token_metadata.go | 17 ++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/cmd/metadata/models/contract_metadata.go b/cmd/metadata/models/contract_metadata.go index 872d726..44e90ba 100644 --- a/cmd/metadata/models/contract_metadata.go +++ b/cmd/metadata/models/contract_metadata.go @@ -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() diff --git a/cmd/metadata/models/token_metadata.go b/cmd/metadata/models/token_metadata.go index 07f754f..01d1667 100644 --- a/cmd/metadata/models/token_metadata.go +++ b/cmd/metadata/models/token_metadata.go @@ -2,6 +2,7 @@ package models import ( "context" + "fmt" "time" "github.com/dipdup-net/go-lib/database" @@ -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()