Skip to content

Commit

Permalink
Revert "PLIN-4034 Lookups for deletes on upserts (#101)" (#102)
Browse files Browse the repository at this point in the history
This reverts commit 9f0a2e7.
  • Loading branch information
acofer authored Mar 20, 2023
1 parent 9f0a2e7 commit 9c5183d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 153 deletions.
41 changes: 3 additions & 38 deletions picard.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,36 +158,6 @@ func (p PersistenceORM) upsert(data interface{}, deleteFilters interface{}) erro
dataValue := reflect.ValueOf(data)
dataCount := dataValue.Len()
var changeSets []*dbchange.ChangeSet

// the delete queries should be executed first because of posible UNIQUE restrictions on the DB

deleteLookupResults, _, err := p.checkForExisting(data, tableMetadata, nil, true)
if err != nil {
return err
}

deletes := []dbchange.Change{}

for _, value := range deleteLookupResults {
deletes = append(deletes, dbchange.Change{
Changes: value.(map[string]interface{}),
Type: dbchange.Delete,
})
}

if len(deletes) > 0 {
for i := 0; i < len(deletes); i += p.batchSize {
end := i + p.batchSize
if end > len(deletes) {
end = len(deletes)
}
err = p.upsertBatch(&dbchange.ChangeSet{Deletes: deletes[i:end]}, tableMetadata)
if err != nil {
return err
}
}
}

if dataCount > 0 {
for i := 0; i < dataCount; i += p.batchSize {
end := i + p.batchSize
Expand Down Expand Up @@ -450,7 +420,6 @@ func (p PersistenceORM) checkForExisting(
data interface{},
tableMetadata *tags.TableMetadata,
foreignKey *tags.ForeignKey,
nonExisting bool, // negates the query to check which items shouldn't exist anymore in the DB
) (
map[string]interface{},
[]tags.Lookup,
Expand Down Expand Up @@ -488,11 +457,7 @@ func (p PersistenceORM) checkForExisting(
}
}
}
if nonExisting {
query = query.Where("NOT "+strings.Join(wheres, " || '"+separator+"' || ")+" = ANY(?)", pq.Array(lookupObjectKeys))
} else {
query = query.Where(strings.Join(wheres, " || '"+separator+"' || ")+" = ANY(?)", pq.Array(lookupObjectKeys))
}
query = query.Where(strings.Join(wheres, " || '"+separator+"' || ")+" = ANY(?)", pq.Array(lookupObjectKeys))
}

if multitenancyKeyColumnName != "" {
Expand Down Expand Up @@ -825,14 +790,14 @@ func (p PersistenceORM) generateChanges(
foreignKeys := tableMetadata.GetForeignKeys()
insertsHavePrimaryKey := false
primaryKeyColumnName := tableMetadata.GetPrimaryKeyColumnName()
lookupResults, lookups, err := p.checkForExisting(data, tableMetadata, nil, false)
lookupResults, lookups, err := p.checkForExisting(data, tableMetadata, nil)
if err != nil {
return nil, err
}

for index := range foreignKeys {
foreignKey := &foreignKeys[index]
foreignResults, foreignLookupsUsed, err := p.checkForExisting(data, foreignKey.TableMetadata, foreignKey, false)
foreignResults, foreignLookupsUsed, err := p.checkForExisting(data, foreignKey.TableMetadata, foreignKey)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 9c5183d

Please sign in to comment.