fix: avoid losing cached community collectibles data

This commit is contained in:
Dario Gabriel Lipicar 2024-01-11 12:05:40 -03:00 committed by dlipicar
parent 0c474bb42c
commit 3a00c3fb9d
7 changed files with 79 additions and 37 deletions

View File

@ -106,9 +106,9 @@ func upsertCollectibleTraits(creator sqlite.StatementCreator, id thirdparty.Coll
return nil
}
func upsertCollectiblesData(creator sqlite.StatementCreator, collectibles []thirdparty.CollectibleData) error {
insertCollectible, err := creator.Prepare(fmt.Sprintf(`INSERT OR REPLACE INTO collectible_data_cache (%s)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, collectibleDataColumns))
func setCollectiblesData(creator sqlite.StatementCreator, collectibles []thirdparty.CollectibleData, allowUpdate bool) error {
insertCollectible, err := creator.Prepare(fmt.Sprintf(`%s INTO collectible_data_cache (%s)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, insertStatement(allowUpdate), collectibleDataColumns))
if err != nil {
return err
}
@ -134,16 +134,18 @@ func upsertCollectiblesData(creator sqlite.StatementCreator, collectibles []thir
return err
}
err = upsertCollectibleTraits(creator, c.ID, c.Traits)
if err != nil {
return err
if allowUpdate {
err = upsertCollectibleTraits(creator, c.ID, c.Traits)
if err != nil {
return err
}
}
}
return nil
}
func (o *CollectibleDataDB) SetData(collectibles []thirdparty.CollectibleData) (err error) {
func (o *CollectibleDataDB) SetData(collectibles []thirdparty.CollectibleData, allowUpdate bool) (err error) {
tx, err := o.db.Begin()
if err != nil {
return err
@ -157,7 +159,7 @@ func (o *CollectibleDataDB) SetData(collectibles []thirdparty.CollectibleData) (
}()
// Insert new collectibles data
err = upsertCollectiblesData(tx, collectibles)
err = setCollectiblesData(tx, collectibles, allowUpdate)
if err != nil {
return err
}

View File

@ -31,7 +31,7 @@ func TestUpdateCollectiblesData(t *testing.T) {
var err error
err = db.SetData(data)
err = db.SetData(data, true)
require.NoError(t, err)
ids := make([]thirdparty.CollectibleUniqueID, 0, len(data))
@ -79,15 +79,29 @@ func TestUpdateCollectiblesData(t *testing.T) {
}
// update some collectibles, changing the provider
c0 := data[0]
c0Orig := data[0]
c0 := c0Orig
c0.Name = "new collectible name 0"
c0.Provider = "new collectible provider 0"
c1 := data[1]
c1Orig := data[1]
c1 := c1Orig
c1.Name = "new collectible name 1"
c1.Provider = "new collectible provider 1"
err = db.SetData([]thirdparty.CollectibleData{c0, c1})
// Test allowUpdate = false
err = db.SetData([]thirdparty.CollectibleData{c0, c1}, false)
require.NoError(t, err)
loadedMap, err = db.GetData([]thirdparty.CollectibleUniqueID{c0.ID, c1.ID})
require.NoError(t, err)
require.Equal(t, 2, len(loadedMap))
require.Equal(t, c0Orig, loadedMap[c0.ID.HashKey()])
require.Equal(t, c1Orig, loadedMap[c1.ID.HashKey()])
// Test allowUpdate = true
err = db.SetData([]thirdparty.CollectibleData{c0, c1}, true)
require.NoError(t, err)
loadedMap, err = db.GetData([]thirdparty.CollectibleUniqueID{c0.ID, c1.ID})
@ -108,7 +122,7 @@ func TestUpdateCommunityData(t *testing.T) {
var err error
err = db.SetData(data)
err = db.SetData(data, true)
require.NoError(t, err)
for i := 0; i < nData; i++ {

View File

@ -98,9 +98,9 @@ func upsertCollectionTraits(creator sqlite.StatementCreator, id thirdparty.Contr
return nil
}
func upsertCollectionsData(creator sqlite.StatementCreator, collections []thirdparty.CollectionData) error {
insertCollection, err := creator.Prepare(fmt.Sprintf(`INSERT OR REPLACE INTO collection_data_cache (%s)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, collectionDataColumns))
func setCollectionsData(creator sqlite.StatementCreator, collections []thirdparty.CollectionData, allowUpdate bool) error {
insertCollection, err := creator.Prepare(fmt.Sprintf(`%s INTO collection_data_cache (%s)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, insertStatement(allowUpdate), collectionDataColumns))
if err != nil {
return err
}
@ -120,16 +120,18 @@ func upsertCollectionsData(creator sqlite.StatementCreator, collections []thirdp
return err
}
err = upsertCollectionTraits(creator, c.ID, c.Traits)
if err != nil {
return err
if allowUpdate {
err = upsertCollectionTraits(creator, c.ID, c.Traits)
if err != nil {
return err
}
}
}
return nil
}
func (o *CollectionDataDB) SetData(collections []thirdparty.CollectionData) (err error) {
func (o *CollectionDataDB) SetData(collections []thirdparty.CollectionData, allowUpdate bool) (err error) {
tx, err := o.db.Begin()
if err != nil {
return err
@ -143,7 +145,7 @@ func (o *CollectionDataDB) SetData(collections []thirdparty.CollectionData) (err
}()
// Insert new collections data
err = upsertCollectionsData(tx, collections)
err = setCollectionsData(tx, collections, allowUpdate)
if err != nil {
return err
}

View File

@ -30,7 +30,7 @@ func TestUpdateCollectionsData(t *testing.T) {
var err error
err = db.SetData(data)
err = db.SetData(data, true)
require.NoError(t, err)
ids := make([]thirdparty.ContractID, 0, len(data))
@ -72,15 +72,29 @@ func TestUpdateCollectionsData(t *testing.T) {
}
// update some collections, changing the provider
c0 := data[0]
c0Orig := data[0]
c0 := c0Orig
c0.Name = "new collection name 0"
c0.Provider = "new collection provider 0"
c1 := data[1]
c1Orig := data[1]
c1 := c1Orig
c1.Name = "new collection name 1"
c1.Provider = "new collection provider 1"
err = db.SetData([]thirdparty.CollectionData{c0, c1})
// Test allowUpdate = false
err = db.SetData([]thirdparty.CollectionData{c0, c1}, false)
require.NoError(t, err)
loadedMap, err = db.GetData([]thirdparty.ContractID{c0.ID, c1.ID})
require.NoError(t, err)
require.Equal(t, 2, len(loadedMap))
require.Equal(t, c0Orig, loadedMap[c0.ID.HashKey()])
require.Equal(t, c1Orig, loadedMap[c1.ID.HashKey()])
// Test allowUpdate = true
err = db.SetData([]thirdparty.CollectionData{c0, c1}, true)
require.NoError(t, err)
loadedMap, err = db.GetData([]thirdparty.ContractID{c0.ID, c1.ID})

View File

@ -0,0 +1,8 @@
package collectibles
func insertStatement(allowUpdate bool) string {
if allowUpdate {
return `INSERT OR REPLACE`
}
return `INSERT OR IGNORE`
}

View File

@ -86,7 +86,7 @@ func TestFilterOwnedCollectibles(t *testing.T) {
}
}
err = cDB.SetData(data)
err = cDB.SetData(data, true)
require.NoError(t, err)
for i := 0; i < nData; i++ {
err = cDB.SetCommunityInfo(data[i].ID, communityData[i])

View File

@ -595,12 +595,12 @@ func (o *Manager) processFullCollectibleData(ctx context.Context, assets []third
}
}
err := o.collectiblesDataDB.SetData(collectiblesData)
err := o.collectiblesDataDB.SetData(collectiblesData, true)
if err != nil {
return nil, err
}
err = o.collectionsDataDB.SetData(collectionsData)
err = o.collectionsDataDB.SetData(collectionsData, true)
if err != nil {
return nil, err
}
@ -647,12 +647,15 @@ func (o *Manager) fillCommunityID(asset *thirdparty.FullCollectibleData) error {
func (o *Manager) fetchCommunityAssets(communityID string, communityAssets []*thirdparty.FullCollectibleData) error {
communityInfo, err := o.communityManager.FetchCommunityInfo(communityID)
// If the community is found, we update the DB.
// If the community is not found, we only insert new entries to the DB (don't replace what is already there).
allowUpdate := false
if err != nil {
log.Error("fetchCommunityInfo failed", "communityID", communityID, "err", err)
return err
}
if communityInfo != nil {
} else if communityInfo == nil {
log.Warn("fetchCommunityAssets community not found", "communityID", communityID)
} else {
for _, communityAsset := range communityAssets {
err := o.communityManager.FillCollectibleMetadata(communityAsset)
if err != nil {
@ -660,8 +663,7 @@ func (o *Manager) fetchCommunityAssets(communityID string, communityAssets []*th
return err
}
}
} else {
log.Warn("fetchCommunityAssets community not found", "communityID", communityID)
allowUpdate = true
}
collectiblesData := make([]thirdparty.CollectibleData, 0, len(communityAssets))
@ -674,13 +676,13 @@ func (o *Manager) fetchCommunityAssets(communityID string, communityAssets []*th
}
}
err = o.collectiblesDataDB.SetData(collectiblesData)
err = o.collectiblesDataDB.SetData(collectiblesData, allowUpdate)
if err != nil {
log.Error("collectiblesDataDB SetData failed", "communityID", communityID, "err", err)
return err
}
err = o.collectionsDataDB.SetData(collectionsData)
err = o.collectionsDataDB.SetData(collectionsData, allowUpdate)
if err != nil {
log.Error("collectionsDataDB SetData failed", "communityID", communityID, "err", err)
return err
@ -732,7 +734,7 @@ func (o *Manager) fillAnimationMediatype(ctx context.Context, asset *thirdparty.
}
func (o *Manager) processCollectionData(ctx context.Context, collections []thirdparty.CollectionData) error {
return o.collectionsDataDB.SetData(collections)
return o.collectionsDataDB.SetData(collections, true)
}
func (o *Manager) getCacheFullCollectibleData(uniqueIDs []thirdparty.CollectibleUniqueID) ([]thirdparty.FullCollectibleData, error) {