Skip to content

Commit

Permalink
Add validation in generating empty SET clause for StructModel (uptrac…
Browse files Browse the repository at this point in the history
…e#871)

* feat: add validation on structTableModel empty result after appending SET

* feat: add test cases
  • Loading branch information
hohobilly authored Sep 10, 2023
1 parent 63de2b1 commit fd7b609
Show file tree
Hide file tree
Showing 37 changed files with 55 additions and 7 deletions.
12 changes: 12 additions & 0 deletions internal/dbtest/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,18 @@ func TestQuery(t *testing.T) {
func(db *bun.DB) schema.QueryAppender {
return db.NewUpdate().TableExpr("xxx").Set("foo = ?", bun.NullZero("")).Where("1")
},
func(db *bun.DB) schema.QueryAppender {
return db.NewUpdate().Model(new(Model)).OmitZero().WherePK()
},
func(db *bun.DB) schema.QueryAppender {
return db.NewUpdate().Model(&Model{Str: ""}).OmitZero().WherePK()
},
func(db *bun.DB) schema.QueryAppender {
return db.NewUpdate().Model(&Model{Str: ""}).WherePK()
},
func(db *bun.DB) schema.QueryAppender {
return db.NewUpdate().Model(&Model{42, ""}).OmitZero()
},
}

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-159
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bun: empty SET clause is not allowed in the UPDATE query
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mariadb-160
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bun: empty SET clause is not allowed in the UPDATE query
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mariadb-161
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE `models` AS `model` SET `str` = '' WHERE (`model`.`id` = NULL)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mariadb-162
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bun: empty SET clause is not allowed in the UPDATE query
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-mariadb-87
Original file line number Diff line number Diff line change
@@ -1 +1 @@
UPDATE `models` AS `model` SET WHERE (`model`.`id` = 42)
bun: empty SET clause is not allowed in the UPDATE query
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mssql2019-159
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bun: empty SET clause is not allowed in the UPDATE query
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mssql2019-160
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bun: empty SET clause is not allowed in the UPDATE query
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mssql2019-161
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE "models" SET "str" = N'' WHERE ("id" = NULL)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mssql2019-162
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bun: empty SET clause is not allowed in the UPDATE query
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-mssql2019-87
Original file line number Diff line number Diff line change
@@ -1 +1 @@
UPDATE "models" SET WHERE ("id" = 42)
bun: empty SET clause is not allowed in the UPDATE query
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql5-159
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bun: empty SET clause is not allowed in the UPDATE query
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql5-160
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bun: empty SET clause is not allowed in the UPDATE query
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql5-161
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE `models` AS `model` SET `str` = '' WHERE (`model`.`id` = NULL)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql5-162
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bun: empty SET clause is not allowed in the UPDATE query
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-mysql5-87
Original file line number Diff line number Diff line change
@@ -1 +1 @@
UPDATE `models` AS `model` SET WHERE (`model`.`id` = 42)
bun: empty SET clause is not allowed in the UPDATE query
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql8-159
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bun: empty SET clause is not allowed in the UPDATE query
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql8-160
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bun: empty SET clause is not allowed in the UPDATE query
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql8-161
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE `models` AS `model` SET `str` = '' WHERE (`model`.`id` = NULL)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql8-162
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bun: empty SET clause is not allowed in the UPDATE query
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-mysql8-87
Original file line number Diff line number Diff line change
@@ -1 +1 @@
UPDATE `models` AS `model` SET WHERE (`model`.`id` = 42)
bun: empty SET clause is not allowed in the UPDATE query
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pg-159
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bun: empty SET clause is not allowed in the UPDATE query
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pg-160
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bun: empty SET clause is not allowed in the UPDATE query
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pg-161
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE "models" AS "model" SET "str" = '' WHERE ("model"."id" = NULL)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pg-162
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bun: empty SET clause is not allowed in the UPDATE query
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-pg-87
Original file line number Diff line number Diff line change
@@ -1 +1 @@
UPDATE "models" AS "model" SET WHERE ("model"."id" = 42)
bun: empty SET clause is not allowed in the UPDATE query
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pgx-159
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bun: empty SET clause is not allowed in the UPDATE query
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pgx-160
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bun: empty SET clause is not allowed in the UPDATE query
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pgx-161
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE "models" AS "model" SET "str" = '' WHERE ("model"."id" = NULL)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pgx-162
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bun: empty SET clause is not allowed in the UPDATE query
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-pgx-87
Original file line number Diff line number Diff line change
@@ -1 +1 @@
UPDATE "models" AS "model" SET WHERE ("model"."id" = 42)
bun: empty SET clause is not allowed in the UPDATE query
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-sqlite-159
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bun: empty SET clause is not allowed in the UPDATE query
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-sqlite-160
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bun: empty SET clause is not allowed in the UPDATE query
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-sqlite-161
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE "models" AS "model" SET "str" = '' WHERE ("model"."id" = NULL)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-sqlite-162
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bun: empty SET clause is not allowed in the UPDATE query
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-sqlite-87
Original file line number Diff line number Diff line change
@@ -1 +1 @@
UPDATE "models" AS "model" SET WHERE ("model"."id" = 42)
bun: empty SET clause is not allowed in the UPDATE query
8 changes: 8 additions & 0 deletions query_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,18 @@ func (q *UpdateQuery) mustAppendSet(fmter schema.Formatter, b []byte) (_ []byte,

switch model := q.tableModel.(type) {
case *structTableModel:
pos := len(b)
b, err = q.appendSetStruct(fmter, b, model)
if err != nil {
return nil, err
}

// Validate if no values were appended after SET clause.
// e.g. UPDATE users SET WHERE id = 1
// See issues858
if len(b) == pos {
return nil, errors.New("bun: empty SET clause is not allowed in the UPDATE query")
}
case *sliceTableModel:
return nil, errors.New("bun: to bulk Update, use CTE and VALUES")
default:
Expand Down

0 comments on commit fd7b609

Please sign in to comment.