Skip to content

Commit

Permalink
Merge pull request #517 from uptrace/fix/value-override-omitzero
Browse files Browse the repository at this point in the history
fix: fix OmitZero and value overriding
  • Loading branch information
vmihailenco authored Apr 20, 2022
2 parents f010b1d + 087ea07 commit d4c3bf9
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 3 deletions.
12 changes: 12 additions & 0 deletions internal/dbtest/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,18 @@ func TestQuery(t *testing.T) {
WherePK().
WhereAllWithDeleted()
},
func(db *bun.DB) schema.QueryAppender {
type Model struct {
ID int64 `bun:",pk"`
UpdatedAt time.Time
}
return db.NewUpdate().
Model(&Model{}).
OmitZero().
WherePK().
Value("updated_at", "NOW()").
Returning("*")
},
}

timeRE := regexp.MustCompile(`'2\d{3}-\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-mariadb-130
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE `models` AS `model` SET `updated_at` = NOW() WHERE (`model`.`id` = 0)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mssql2019-130
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE "models" SET "updated_at" = NOW() WHERE ("id" = 0)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql5-130
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE `models` AS `model` SET `updated_at` = NOW() WHERE (`model`.`id` = 0)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql8-130
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE `models` AS `model` SET `updated_at` = NOW() WHERE (`model`.`id` = 0)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pg-130
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE "models" AS "model" SET "updated_at" = NOW() WHERE ("model"."id" = 0) RETURNING *
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pgx-130
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE "models" AS "model" SET "updated_at" = NOW() WHERE ("model"."id" = 0) RETURNING *
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-sqlite-130
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE "models" AS "model" SET "updated_at" = NOW() WHERE ("model"."id" = 0) RETURNING *
7 changes: 4 additions & 3 deletions query_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,9 @@ func (q *UpdateQuery) appendSetStruct(
isTemplate := fmter.IsNop()
pos := len(b)
for _, f := range fields {
if q.omitZero && f.HasZeroValue(model.strct) {
app, hasValue := q.modelValues[f.Name]

if !hasValue && q.omitZero && f.HasZeroValue(model.strct) {
continue
}

Expand All @@ -290,8 +292,7 @@ func (q *UpdateQuery) appendSetStruct(
continue
}

app, ok := q.modelValues[f.Name]
if ok {
if hasValue {
b, err = app.AppendQuery(fmter, b)
if err != nil {
return nil, err
Expand Down

0 comments on commit d4c3bf9

Please sign in to comment.