Skip to content

Commit

Permalink
fix: don't append soft delete where for insert queries with on confli…
Browse files Browse the repository at this point in the history
…ct clause
  • Loading branch information
vmihailenco committed Sep 18, 2021
1 parent ce782c3 commit 27c477c
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 3 deletions.
3 changes: 3 additions & 0 deletions internal/dbtest/query_test.go
Expand Up @@ -551,6 +551,9 @@ func TestQuery(t *testing.T) {
}
return db.NewInsert().Model(new(Model))
},
func(db *bun.DB) schema.QueryAppender {
return db.NewInsert().Model(new(SoftDelete)).On("CONFLICT DO NOTHING")
},
}

timeRE := regexp.MustCompile(`'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+(\+\d{2}:\d{2})?'`)
Expand Down
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql5-86
@@ -0,0 +1 @@
INSERT INTO `soft_deletes` (`id`, `deleted_at`) VALUES (DEFAULT, DEFAULT) ON CONFLICT DO NOTHING
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql8-86
@@ -0,0 +1 @@
INSERT INTO `soft_deletes` (`id`, `deleted_at`) VALUES (DEFAULT, DEFAULT) ON CONFLICT DO NOTHING
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pg-86
@@ -0,0 +1 @@
INSERT INTO "soft_deletes" AS "soft_delete" ("id", "deleted_at") VALUES (DEFAULT, DEFAULT) ON CONFLICT DO NOTHING RETURNING "id", "deleted_at"
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pgx-86
@@ -0,0 +1 @@
INSERT INTO "soft_deletes" AS "soft_delete" ("id", "deleted_at") VALUES (DEFAULT, DEFAULT) ON CONFLICT DO NOTHING RETURNING "id", "deleted_at"
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-sqlite-86
@@ -0,0 +1 @@
INSERT INTO "soft_deletes" AS "soft_delete" ("deleted_at") VALUES (NULL) ON CONFLICT DO NOTHING RETURNING "id", "deleted_at"
10 changes: 7 additions & 3 deletions query_insert.go
Expand Up @@ -431,9 +431,13 @@ func (q *InsertQuery) appendOn(fmter schema.Formatter, b []byte) (_ []byte, err
b = q.appendSetExcluded(b, fields)
}

b, err = q.appendWhere(fmter, b, true)
if err != nil {
return nil, err
if len(q.where) > 0 {
b = append(b, " WHERE "...)

b, err = appendWhere(fmter, b, q.where)
if err != nil {
return nil, err
}
}

return b, nil
Expand Down

0 comments on commit 27c477c

Please sign in to comment.