From ac3623bb528b82432d2aaffcdc47ed5155470145 Mon Sep 17 00:00:00 2001 From: Mark Beamer Jr Date: Tue, 11 Jun 2019 22:37:55 -0400 Subject: [PATCH 1/2] 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. --- templates/singleton/boil_queries.tpl | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/templates/singleton/boil_queries.tpl b/templates/singleton/boil_queries.tpl index 3e9ebb8..d04683f 100644 --- a/templates/singleton/boil_queries.tpl +++ b/templates/singleton/boil_queries.tpl @@ -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, @@ -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) @@ -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{} -- 2.45.2 From e2e6d7ace5553ddff56a2f9f31c3a5e2f0f170ce Mon Sep 17 00:00:00 2001 From: Niko Storni Date: Tue, 30 May 2023 19:28:55 +0200 Subject: [PATCH 2/2] address review --- templates/singleton/boil_queries.tpl | 1 + 1 file changed, 1 insertion(+) diff --git a/templates/singleton/boil_queries.tpl b/templates/singleton/boil_queries.tpl index d04683f..416de3d 100644 --- a/templates/singleton/boil_queries.tpl +++ b/templates/singleton/boil_queries.tpl @@ -86,6 +86,7 @@ func deleteOneToManyConflictsBeforeMerge(tx boil.Executor, conflict conflictingU for i, column := range conflict.columns { if column == conflict.objectIdColumn { objectIDIndex = i + break } } query := fmt.Sprintf( -- 2.45.2