Skip to content

Commit

Permalink
fix: specify table alias for soft delete where
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Sep 15, 2021
1 parent 1a03dc5 commit 5fff1dc
Show file tree
Hide file tree
Showing 14 changed files with 24 additions and 16 deletions.
10 changes: 10 additions & 0 deletions internal/dbtest/soft_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/feature"
)

func TestSoftDelete(t *testing.T) {
Expand Down Expand Up @@ -125,6 +126,15 @@ func testSoftDeleteBulk(t *testing.T, db *bun.DB) {
_, err = db.NewInsert().Model(&videos).Exec(ctx)
require.NoError(t, err)

if db.Dialect().Features().Has(feature.CTE) {
_, err := db.NewUpdate().
Model(&videos).
Column("name").
Bulk().
Exec(ctx)
require.NoError(t, err)
}

_, err = db.NewDelete().Model(&videos).WherePK().Exec(ctx)
require.NoError(t, err)

Expand Down
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-pg-26
Original file line number Diff line number Diff line change
@@ -1 +1 @@
UPDATE "models" AS "model" SET "str" = '' WHERE ("id" = NULL)
UPDATE "models" AS "model" SET "str" = '' WHERE ("model"."id" = NULL)
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-pg-27
Original file line number Diff line number Diff line change
@@ -1 +1 @@
UPDATE "models" AS "model" SET "str" = 'hello' WHERE ("id" = 42)
UPDATE "models" AS "model" SET "str" = 'hello' WHERE ("model"."id" = 42)
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-pg-63
Original file line number Diff line number Diff line change
@@ -1 +1 @@
UPDATE "models" AS "model" SET "str" = '', foo = 'bar' WHERE ("id" = NULL)
UPDATE "models" AS "model" SET "str" = '', foo = 'bar' WHERE ("model"."id" = NULL)
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-pg-64
Original file line number Diff line number Diff line change
@@ -1 +1 @@
UPDATE "soft_deletes" AS "soft_delete" SET "deleted_at" = [TIME] WHERE "deleted_at" IS NULL AND ("id" = NULL)
UPDATE "soft_deletes" AS "soft_delete" SET "deleted_at" = [TIME] WHERE "soft_delete"."deleted_at" IS NULL AND ("soft_delete"."id" = NULL)
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-pgx-26
Original file line number Diff line number Diff line change
@@ -1 +1 @@
UPDATE "models" AS "model" SET "str" = '' WHERE ("id" = NULL)
UPDATE "models" AS "model" SET "str" = '' WHERE ("model"."id" = NULL)
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-pgx-27
Original file line number Diff line number Diff line change
@@ -1 +1 @@
UPDATE "models" AS "model" SET "str" = 'hello' WHERE ("id" = 42)
UPDATE "models" AS "model" SET "str" = 'hello' WHERE ("model"."id" = 42)
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-pgx-63
Original file line number Diff line number Diff line change
@@ -1 +1 @@
UPDATE "models" AS "model" SET "str" = '', foo = 'bar' WHERE ("id" = NULL)
UPDATE "models" AS "model" SET "str" = '', foo = 'bar' WHERE ("model"."id" = NULL)
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-pgx-64
Original file line number Diff line number Diff line change
@@ -1 +1 @@
UPDATE "soft_deletes" AS "soft_delete" SET "deleted_at" = [TIME] WHERE "deleted_at" IS NULL AND ("id" = NULL)
UPDATE "soft_deletes" AS "soft_delete" SET "deleted_at" = [TIME] WHERE "soft_delete"."deleted_at" IS NULL AND ("soft_delete"."id" = NULL)
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-sqlite-26
Original file line number Diff line number Diff line change
@@ -1 +1 @@
UPDATE "models" AS "model" SET "str" = '' WHERE ("id" = NULL)
UPDATE "models" AS "model" SET "str" = '' WHERE ("model"."id" = NULL)
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-sqlite-27
Original file line number Diff line number Diff line change
@@ -1 +1 @@
UPDATE "models" AS "model" SET "str" = 'hello' WHERE ("id" = 42)
UPDATE "models" AS "model" SET "str" = 'hello' WHERE ("model"."id" = 42)
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-sqlite-63
Original file line number Diff line number Diff line change
@@ -1 +1 @@
UPDATE "models" AS "model" SET "str" = '', foo = 'bar' WHERE ("id" = NULL)
UPDATE "models" AS "model" SET "str" = '', foo = 'bar' WHERE ("model"."id" = NULL)
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-sqlite-64
Original file line number Diff line number Diff line change
@@ -1 +1 @@
UPDATE "soft_deletes" AS "soft_delete" SET "deleted_at" = [TIME] WHERE "deleted_at" IS NULL AND ("id" = NULL)
UPDATE "soft_deletes" AS "soft_delete" SET "deleted_at" = [TIME] WHERE "soft_delete"."deleted_at" IS NULL AND ("soft_delete"."id" = NULL)
6 changes: 2 additions & 4 deletions query_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,16 +170,14 @@ func (q *UpdateQuery) AppendQuery(fmter schema.Formatter, b []byte) (_ []byte, e
}
fmter = formatterWithModel(fmter, q)

withAlias := fmter.HasFeature(feature.UpdateMultiTable)

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

b = append(b, "UPDATE "...)

if withAlias {
if fmter.HasFeature(feature.UpdateMultiTable) {
b, err = q.appendTablesWithAlias(fmter, b)
} else {
b, err = q.appendFirstTableWithAlias(fmter, b)
Expand All @@ -200,7 +198,7 @@ func (q *UpdateQuery) AppendQuery(fmter schema.Formatter, b []byte) (_ []byte, e
}
}

b, err = q.mustAppendWhere(fmter, b, withAlias)
b, err = q.mustAppendWhere(fmter, b, true)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 5fff1dc

Please sign in to comment.