Skip to content

Commit

Permalink
Added objectID position tracking. When the template is generated the …
Browse files Browse the repository at this point in the history
…ordering of conflicting unique columns is alphabetical. This means we cannot assume the objectID can always be appended. It might need to be inserted at a specific position. This adds support for tracking the position and inserting it at the correct position so the delete query correctly deletes the conflicts.
  • Loading branch information
tiger5226 authored and nikooo777 committed May 27, 2023
1 parent 6b4e052 commit ac3623b
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions templates/singleton/boil_queries.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,12 @@ func deleteOneToOneConflictsBeforeMerge(tx boil.Executor, conflict conflictingUn

func deleteOneToManyConflictsBeforeMerge(tx boil.Executor, conflict conflictingUniqueKey, primaryID uint64, secondaryID uint64) error {
conflictingColumns := strmangle.SetComplement(conflict.columns, []string{conflict.objectIdColumn})

var objectIDIndex int
for i, column := range conflict.columns {
if column == conflict.objectIdColumn {
objectIDIndex = i
}
}
query := fmt.Sprintf(
"SELECT %s FROM %s WHERE %s IN (%s) GROUP BY %s HAVING count(distinct %s) > 1",
strings.Join(conflictingColumns, ","), conflict.table, conflict.objectIdColumn,
Expand Down Expand Up @@ -137,7 +142,7 @@ func deleteOneToManyConflictsBeforeMerge(tx boil.Executor, conflict conflictingU
//There could be multiple conflicting rows between ObjectIDs. In the SELECT query we grab each row and their column
// keys to be deleted here in a loop.
for _, rowToDelete := range rowsToRemove {
rowToDelete = append(rowToDelete, secondaryID)
rowToDelete = insert(rowToDelete, objectIDIndex, secondaryID)
_, err = tx.Exec(query, rowToDelete...)
if err != nil {
return errors.Err(err)
Expand All @@ -146,6 +151,10 @@ func deleteOneToManyConflictsBeforeMerge(tx boil.Executor, conflict conflictingU
return nil
}

func insert(slice []interface{}, index int, value interface{}) []interface{} {
return append(slice[:index], append([]interface{}{value}, slice[index:]...)...)
}

func checkMerge(tx boil.Executor, foreignKeys []foreignKey) error {
uniqueColumns := []interface{}{}
uniqueColumnNames := map[string]bool{}
Expand Down

0 comments on commit ac3623b

Please sign in to comment.