Skip to content

Commit ac3623b

Browse files
tiger5226nikooo777
authored andcommitted
Added objectID position tracking. When the template is generated the 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.
1 parent 6b4e052 commit ac3623b

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

templates/singleton/boil_queries.tpl

+11-2
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,12 @@ func deleteOneToOneConflictsBeforeMerge(tx boil.Executor, conflict conflictingUn
8282

8383
func deleteOneToManyConflictsBeforeMerge(tx boil.Executor, conflict conflictingUniqueKey, primaryID uint64, secondaryID uint64) error {
8484
conflictingColumns := strmangle.SetComplement(conflict.columns, []string{conflict.objectIdColumn})
85-
85+
var objectIDIndex int
86+
for i, column := range conflict.columns {
87+
if column == conflict.objectIdColumn {
88+
objectIDIndex = i
89+
}
90+
}
8691
query := fmt.Sprintf(
8792
"SELECT %s FROM %s WHERE %s IN (%s) GROUP BY %s HAVING count(distinct %s) > 1",
8893
strings.Join(conflictingColumns, ","), conflict.table, conflict.objectIdColumn,
@@ -137,7 +142,7 @@ func deleteOneToManyConflictsBeforeMerge(tx boil.Executor, conflict conflictingU
137142
//There could be multiple conflicting rows between ObjectIDs. In the SELECT query we grab each row and their column
138143
// keys to be deleted here in a loop.
139144
for _, rowToDelete := range rowsToRemove {
140-
rowToDelete = append(rowToDelete, secondaryID)
145+
rowToDelete = insert(rowToDelete, objectIDIndex, secondaryID)
141146
_, err = tx.Exec(query, rowToDelete...)
142147
if err != nil {
143148
return errors.Err(err)
@@ -146,6 +151,10 @@ func deleteOneToManyConflictsBeforeMerge(tx boil.Executor, conflict conflictingU
146151
return nil
147152
}
148153

154+
func insert(slice []interface{}, index int, value interface{}) []interface{} {
155+
return append(slice[:index], append([]interface{}{value}, slice[index:]...)...)
156+
}
157+
149158
func checkMerge(tx boil.Executor, foreignKeys []foreignKey) error {
150159
uniqueColumns := []interface{}{}
151160
uniqueColumnNames := map[string]bool{}

0 commit comments

Comments
 (0)