Skip to content

Commit

Permalink
fix: relation join soft delete SQL generate
Browse files Browse the repository at this point in the history
  • Loading branch information
danclive committed Sep 12, 2022
1 parent 28915f8 commit a98f4e9
Show file tree
Hide file tree
Showing 16 changed files with 50 additions and 6 deletions.
16 changes: 16 additions & 0 deletions internal/dbtest/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,22 @@ func TestQuery(t *testing.T) {
}
return db.NewCreateTable().Model(new(User))
},
func(db *bun.DB) schema.QueryAppender {
type Model struct {
ID int64 `bun:",pk,autoincrement"`
SoftDeleteID int64
SoftDelete *SoftDelete1 `bun:"rel:belongs-to"`
}
return db.NewSelect().Model(new(Model)).Relation("SoftDelete")
},
func(db *bun.DB) schema.QueryAppender {
type Model struct {
ID int64 `bun:",pk,autoincrement"`
SoftDeleteID int64
SoftDelete *SoftDelete2 `bun:"rel:belongs-to"`
}
return db.NewSelect().Model(new(Model)).Relation("SoftDelete")
},
}

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-152
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT `model`.`id`, `model`.`soft_delete_id`, `soft_delete`.`id` AS `soft_delete__id`, `soft_delete`.`deleted_at` AS `soft_delete__deleted_at` FROM `models` AS `model` LEFT JOIN `soft_deletes` AS `soft_delete` ON (`soft_delete`.`id` = `model`.`soft_delete_id`) AND `soft_delete`.`deleted_at` IS NULL
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mariadb-153
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT `model`.`id`, `model`.`soft_delete_id`, `soft_delete`.`id` AS `soft_delete__id`, `soft_delete`.`deleted_at` AS `soft_delete__deleted_at` FROM `models` AS `model` LEFT JOIN `soft_deletes` AS `soft_delete` ON (`soft_delete`.`id` = `model`.`soft_delete_id`) AND `soft_delete`.`deleted_at` = '0001-01-01 00:00:00'
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mssql2019-152
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT "model"."id", "model"."soft_delete_id", "soft_delete"."id" AS "soft_delete__id", "soft_delete"."deleted_at" AS "soft_delete__deleted_at" FROM "models" AS "model" LEFT JOIN "soft_deletes" AS "soft_delete" ON ("soft_delete"."id" = "model"."soft_delete_id") AND "soft_delete"."deleted_at" IS NULL
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mssql2019-153
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT "model"."id", "model"."soft_delete_id", "soft_delete"."id" AS "soft_delete__id", "soft_delete"."deleted_at" AS "soft_delete__deleted_at" FROM "models" AS "model" LEFT JOIN "soft_deletes" AS "soft_delete" ON ("soft_delete"."id" = "model"."soft_delete_id") AND "soft_delete"."deleted_at" = '0001-01-01 00:00:00'
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql5-152
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT `model`.`id`, `model`.`soft_delete_id`, `soft_delete`.`id` AS `soft_delete__id`, `soft_delete`.`deleted_at` AS `soft_delete__deleted_at` FROM `models` AS `model` LEFT JOIN `soft_deletes` AS `soft_delete` ON (`soft_delete`.`id` = `model`.`soft_delete_id`) AND `soft_delete`.`deleted_at` IS NULL
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql5-153
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT `model`.`id`, `model`.`soft_delete_id`, `soft_delete`.`id` AS `soft_delete__id`, `soft_delete`.`deleted_at` AS `soft_delete__deleted_at` FROM `models` AS `model` LEFT JOIN `soft_deletes` AS `soft_delete` ON (`soft_delete`.`id` = `model`.`soft_delete_id`) AND `soft_delete`.`deleted_at` = '0001-01-01 00:00:00'
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql8-152
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT `model`.`id`, `model`.`soft_delete_id`, `soft_delete`.`id` AS `soft_delete__id`, `soft_delete`.`deleted_at` AS `soft_delete__deleted_at` FROM `models` AS `model` LEFT JOIN `soft_deletes` AS `soft_delete` ON (`soft_delete`.`id` = `model`.`soft_delete_id`) AND `soft_delete`.`deleted_at` IS NULL
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql8-153
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT `model`.`id`, `model`.`soft_delete_id`, `soft_delete`.`id` AS `soft_delete__id`, `soft_delete`.`deleted_at` AS `soft_delete__deleted_at` FROM `models` AS `model` LEFT JOIN `soft_deletes` AS `soft_delete` ON (`soft_delete`.`id` = `model`.`soft_delete_id`) AND `soft_delete`.`deleted_at` = '0001-01-01 00:00:00'
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pg-152
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT "model"."id", "model"."soft_delete_id", "soft_delete"."id" AS "soft_delete__id", "soft_delete"."deleted_at" AS "soft_delete__deleted_at" FROM "models" AS "model" LEFT JOIN "soft_deletes" AS "soft_delete" ON ("soft_delete"."id" = "model"."soft_delete_id") AND "soft_delete"."deleted_at" IS NULL
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pg-153
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT "model"."id", "model"."soft_delete_id", "soft_delete"."id" AS "soft_delete__id", "soft_delete"."deleted_at" AS "soft_delete__deleted_at" FROM "models" AS "model" LEFT JOIN "soft_deletes" AS "soft_delete" ON ("soft_delete"."id" = "model"."soft_delete_id") AND "soft_delete"."deleted_at" = '0001-01-01 00:00:00+00:00'
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pgx-152
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT "model"."id", "model"."soft_delete_id", "soft_delete"."id" AS "soft_delete__id", "soft_delete"."deleted_at" AS "soft_delete__deleted_at" FROM "models" AS "model" LEFT JOIN "soft_deletes" AS "soft_delete" ON ("soft_delete"."id" = "model"."soft_delete_id") AND "soft_delete"."deleted_at" IS NULL
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pgx-153
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT "model"."id", "model"."soft_delete_id", "soft_delete"."id" AS "soft_delete__id", "soft_delete"."deleted_at" AS "soft_delete__deleted_at" FROM "models" AS "model" LEFT JOIN "soft_deletes" AS "soft_delete" ON ("soft_delete"."id" = "model"."soft_delete_id") AND "soft_delete"."deleted_at" = '0001-01-01 00:00:00+00:00'
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-sqlite-152
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT "model"."id", "model"."soft_delete_id", "soft_delete"."id" AS "soft_delete__id", "soft_delete"."deleted_at" AS "soft_delete__deleted_at" FROM "models" AS "model" LEFT JOIN "soft_deletes" AS "soft_delete" ON ("soft_delete"."id" = "model"."soft_delete_id") AND "soft_delete"."deleted_at" IS NULL
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-sqlite-153
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT "model"."id", "model"."soft_delete_id", "soft_delete"."id" AS "soft_delete__id", "soft_delete"."deleted_at" AS "soft_delete__deleted_at" FROM "models" AS "model" LEFT JOIN "soft_deletes" AS "soft_delete" ON ("soft_delete"."id" = "model"."soft_delete_id") AND "soft_delete"."deleted_at" = '0001-01-01 00:00:00+00:00'
26 changes: 20 additions & 6 deletions relation_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package bun
import (
"context"
"reflect"
"time"

"github.com/uptrace/bun/dialect/feature"
"github.com/uptrace/bun/internal"
Expand Down Expand Up @@ -256,14 +257,27 @@ func (j *relationJoin) appendBaseAlias(fmter schema.Formatter, b []byte) []byte
return append(b, j.BaseModel.Table().SQLAlias...)
}

func (j *relationJoin) appendSoftDelete(b []byte, flags internal.Flag) []byte {
func (j *relationJoin) appendSoftDelete(fmter schema.Formatter, b []byte, flags internal.Flag) []byte {
b = append(b, '.')
b = append(b, j.JoinModel.Table().SoftDeleteField.SQLName...)
if flags.Has(deletedFlag) {
b = append(b, " IS NOT NULL"...)

field := j.JoinModel.Table().SoftDeleteField
b = append(b, field.SQLName...)

if field.IsPtr || field.NullZero {
if flags.Has(deletedFlag) {
b = append(b, " IS NOT NULL"...)
} else {
b = append(b, " IS NULL"...)
}
} else {
b = append(b, " IS NULL"...)
if flags.Has(deletedFlag) {
b = append(b, " != "...)
} else {
b = append(b, " = "...)
}
b = fmter.Dialect().AppendTime(b, time.Time{})
}

return b
}

Expand Down Expand Up @@ -306,7 +320,7 @@ func (j *relationJoin) appendHasOneJoin(
if isSoftDelete {
b = append(b, " AND "...)
b = j.appendAlias(fmter, b)
b = j.appendSoftDelete(b, q.flags)
b = j.appendSoftDelete(fmter, b, q.flags)
}

return b, nil
Expand Down

0 comments on commit a98f4e9

Please sign in to comment.