Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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