From d0abec71a9a546472a83bd70ed4e6a7357659a9b Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Thu, 14 Oct 2021 15:06:18 +0300 Subject: [PATCH] fix: support allowzero on the soft delete field --- internal/dbtest/query_test.go | 41 +++++++++++++++---- internal/dbtest/soft_delete_test.go | 23 ----------- .../testdata/snapshots/TestQuery-mysql5-90 | 1 + .../testdata/snapshots/TestQuery-mysql5-91 | 1 + .../testdata/snapshots/TestQuery-mysql5-92 | 1 + .../testdata/snapshots/TestQuery-mysql5-93 | 1 + .../testdata/snapshots/TestQuery-mysql5-94 | 1 + .../testdata/snapshots/TestQuery-mysql5-95 | 1 + .../testdata/snapshots/TestQuery-mysql8-90 | 1 + .../testdata/snapshots/TestQuery-mysql8-91 | 1 + .../testdata/snapshots/TestQuery-mysql8-92 | 1 + .../testdata/snapshots/TestQuery-mysql8-93 | 1 + .../testdata/snapshots/TestQuery-mysql8-94 | 1 + .../testdata/snapshots/TestQuery-mysql8-95 | 1 + .../dbtest/testdata/snapshots/TestQuery-pg-90 | 1 + .../dbtest/testdata/snapshots/TestQuery-pg-91 | 1 + .../dbtest/testdata/snapshots/TestQuery-pg-92 | 1 + .../dbtest/testdata/snapshots/TestQuery-pg-93 | 1 + .../dbtest/testdata/snapshots/TestQuery-pg-94 | 1 + .../dbtest/testdata/snapshots/TestQuery-pg-95 | 1 + .../testdata/snapshots/TestQuery-pgx-90 | 1 + .../testdata/snapshots/TestQuery-pgx-91 | 1 + .../testdata/snapshots/TestQuery-pgx-92 | 1 + .../testdata/snapshots/TestQuery-pgx-93 | 1 + .../testdata/snapshots/TestQuery-pgx-94 | 1 + .../testdata/snapshots/TestQuery-pgx-95 | 1 + .../testdata/snapshots/TestQuery-sqlite-90 | 1 + .../testdata/snapshots/TestQuery-sqlite-91 | 1 + .../testdata/snapshots/TestQuery-sqlite-92 | 1 + .../testdata/snapshots/TestQuery-sqlite-93 | 1 + .../testdata/snapshots/TestQuery-sqlite-94 | 1 + .../testdata/snapshots/TestQuery-sqlite-95 | 1 + query_base.go | 21 ++++++++-- schema/table.go | 20 +++++---- 34 files changed, 92 insertions(+), 43 deletions(-) create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-mysql5-90 create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-mysql5-91 create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-mysql5-92 create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-mysql5-93 create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-mysql5-94 create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-mysql5-95 create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-mysql8-90 create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-mysql8-91 create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-mysql8-92 create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-mysql8-93 create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-mysql8-94 create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-mysql8-95 create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-pg-90 create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-pg-91 create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-pg-92 create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-pg-93 create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-pg-94 create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-pg-95 create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-pgx-90 create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-pgx-91 create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-pgx-92 create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-pgx-93 create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-pgx-94 create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-pgx-95 create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-sqlite-90 create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-sqlite-91 create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-sqlite-92 create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-sqlite-93 create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-sqlite-94 create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-sqlite-95 diff --git a/internal/dbtest/query_test.go b/internal/dbtest/query_test.go index 8547b86dc..210ade30d 100644 --- a/internal/dbtest/query_test.go +++ b/internal/dbtest/query_test.go @@ -37,11 +37,20 @@ func TestQuery(t *testing.T) { User *User `bun:"rel:belongs-to"` } - type SoftDelete struct { + type SoftDelete1 struct { + bun.BaseModel `bun:"soft_deletes,alias:soft_delete"` + ID int64 DeletedAt time.Time `bun:",soft_delete"` } + type SoftDelete2 struct { + bun.BaseModel `bun:"soft_deletes,alias:soft_delete"` + + ID int64 + DeletedAt time.Time `bun:",soft_delete,allowzero"` + } + queries := []func(db *bun.DB) schema.QueryAppender{ func(db *bun.DB) schema.QueryAppender { return db.NewValues(&Model{42, "hello"}) @@ -403,19 +412,19 @@ func TestQuery(t *testing.T) { return db.NewUpdate().Model(new(Model)).Value("foo", "?", "bar").WherePK() }, func(db *bun.DB) schema.QueryAppender { - return db.NewDelete().Model(new(SoftDelete)).WherePK() + return db.NewDelete().Model(new(SoftDelete1)).WherePK() }, func(db *bun.DB) schema.QueryAppender { - return db.NewDelete().Model(new(SoftDelete)).WherePK().ForceDelete() + return db.NewDelete().Model(new(SoftDelete1)).WherePK().ForceDelete() }, func(db *bun.DB) schema.QueryAppender { - return db.NewSelect().Model(new(SoftDelete)) + return db.NewSelect().Model(new(SoftDelete1)) }, func(db *bun.DB) schema.QueryAppender { - return db.NewSelect().Model(new(SoftDelete)).WhereDeleted() + return db.NewSelect().Model(new(SoftDelete1)).WhereDeleted() }, func(db *bun.DB) schema.QueryAppender { - return db.NewSelect().Model(new(SoftDelete)).WhereAllWithDeleted() + return db.NewSelect().Model(new(SoftDelete1)).WhereAllWithDeleted() }, func(db *bun.DB) schema.QueryAppender { models := []Model{ @@ -552,7 +561,7 @@ 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") + return db.NewInsert().Model(new(SoftDelete1)).On("CONFLICT DO NOTHING") }, func(db *bun.DB) schema.QueryAppender { return db.NewUpdate().Model(&Model{ID: 42}).OmitZero().WherePK() @@ -567,6 +576,24 @@ func TestQuery(t *testing.T) { func(db *bun.DB) schema.QueryAppender { return db.NewInsert().ColumnExpr("id, name").Table("dest").Table("src") }, + func(db *bun.DB) schema.QueryAppender { + return db.NewDelete().Model(new(SoftDelete2)).WherePK() + }, + func(db *bun.DB) schema.QueryAppender { + return db.NewDelete().Model(new(SoftDelete2)).WherePK().ForceDelete() + }, + func(db *bun.DB) schema.QueryAppender { + return db.NewSelect().Model(new(SoftDelete2)) + }, + func(db *bun.DB) schema.QueryAppender { + return db.NewSelect().Model(new(SoftDelete2)).WhereDeleted() + }, + func(db *bun.DB) schema.QueryAppender { + return db.NewSelect().Model(new(SoftDelete2)).WhereAllWithDeleted() + }, + func(db *bun.DB) schema.QueryAppender { + return db.NewInsert().Model(new(SoftDelete2)).On("CONFLICT DO NOTHING") + }, } timeRE := regexp.MustCompile(`'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+(\+\d{2}:\d{2})?'`) diff --git a/internal/dbtest/soft_delete_test.go b/internal/dbtest/soft_delete_test.go index ed4235a30..5c8bb0781 100644 --- a/internal/dbtest/soft_delete_test.go +++ b/internal/dbtest/soft_delete_test.go @@ -2,8 +2,6 @@ package dbtest_test import ( "context" - "database/sql" - "database/sql/driver" "testing" "time" @@ -32,27 +30,6 @@ func TestSoftDelete(t *testing.T) { }) } -type CustomTime struct { - Time time.Time -} - -var _ driver.Valuer = (*CustomTime)(nil) - -func (tm *CustomTime) Value() (driver.Value, error) { - return tm.Time, nil -} - -var _ sql.Scanner = (*CustomTime)(nil) - -func (tm *CustomTime) Scan(src interface{}) error { - tm.Time, _ = src.(time.Time) - return nil -} - -func (tm *CustomTime) IsZero() bool { - return tm.Time.IsZero() -} - type Video struct { ID int64 Name string diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql5-90 b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-90 new file mode 100644 index 000000000..02439a53f --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-90 @@ -0,0 +1 @@ +UPDATE `soft_deletes` AS `soft_delete` SET `soft_delete`.`deleted_at` = [TIME] WHERE `soft_delete`.`deleted_at` = '0001-01-01 00:00:00' AND (`soft_delete`.`id` = NULL) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql5-91 b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-91 new file mode 100644 index 000000000..b72364358 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-91 @@ -0,0 +1 @@ +DELETE FROM `soft_deletes` WHERE `deleted_at` != '0001-01-01 00:00:00' AND (`id` = NULL) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql5-92 b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-92 new file mode 100644 index 000000000..9b26505e8 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-92 @@ -0,0 +1 @@ +SELECT `soft_delete`.`id`, `soft_delete`.`deleted_at` FROM `soft_deletes` AS `soft_delete` WHERE `soft_delete`.`deleted_at` = '0001-01-01 00:00:00' diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql5-93 b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-93 new file mode 100644 index 000000000..dcf2142b3 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-93 @@ -0,0 +1 @@ +SELECT `soft_delete`.`id`, `soft_delete`.`deleted_at` FROM `soft_deletes` AS `soft_delete` WHERE `soft_delete`.`deleted_at` != '0001-01-01 00:00:00' diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql5-94 b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-94 new file mode 100644 index 000000000..6e95603ae --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-94 @@ -0,0 +1 @@ +SELECT `soft_delete`.`id`, `soft_delete`.`deleted_at` FROM `soft_deletes` AS `soft_delete` diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql5-95 b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-95 new file mode 100644 index 000000000..c08c6d206 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-95 @@ -0,0 +1 @@ +INSERT INTO `soft_deletes` (`id`, `deleted_at`) VALUES (DEFAULT, '0001-01-01 00:00:00') ON CONFLICT DO NOTHING diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql8-90 b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-90 new file mode 100644 index 000000000..02439a53f --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-90 @@ -0,0 +1 @@ +UPDATE `soft_deletes` AS `soft_delete` SET `soft_delete`.`deleted_at` = [TIME] WHERE `soft_delete`.`deleted_at` = '0001-01-01 00:00:00' AND (`soft_delete`.`id` = NULL) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql8-91 b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-91 new file mode 100644 index 000000000..bc8802ca5 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-91 @@ -0,0 +1 @@ +DELETE FROM `soft_deletes` AS `soft_delete` WHERE `soft_delete`.`deleted_at` != '0001-01-01 00:00:00' AND (`soft_delete`.`id` = NULL) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql8-92 b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-92 new file mode 100644 index 000000000..9b26505e8 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-92 @@ -0,0 +1 @@ +SELECT `soft_delete`.`id`, `soft_delete`.`deleted_at` FROM `soft_deletes` AS `soft_delete` WHERE `soft_delete`.`deleted_at` = '0001-01-01 00:00:00' diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql8-93 b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-93 new file mode 100644 index 000000000..dcf2142b3 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-93 @@ -0,0 +1 @@ +SELECT `soft_delete`.`id`, `soft_delete`.`deleted_at` FROM `soft_deletes` AS `soft_delete` WHERE `soft_delete`.`deleted_at` != '0001-01-01 00:00:00' diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql8-94 b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-94 new file mode 100644 index 000000000..6e95603ae --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-94 @@ -0,0 +1 @@ +SELECT `soft_delete`.`id`, `soft_delete`.`deleted_at` FROM `soft_deletes` AS `soft_delete` diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql8-95 b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-95 new file mode 100644 index 000000000..c08c6d206 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-95 @@ -0,0 +1 @@ +INSERT INTO `soft_deletes` (`id`, `deleted_at`) VALUES (DEFAULT, '0001-01-01 00:00:00') ON CONFLICT DO NOTHING diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pg-90 b/internal/dbtest/testdata/snapshots/TestQuery-pg-90 new file mode 100644 index 000000000..995ff27df --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pg-90 @@ -0,0 +1 @@ +UPDATE "soft_deletes" AS "soft_delete" SET "deleted_at" = [TIME] WHERE "soft_delete"."deleted_at" = '0001-01-01 00:00:00+00:00' AND ("soft_delete"."id" = NULL) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pg-91 b/internal/dbtest/testdata/snapshots/TestQuery-pg-91 new file mode 100644 index 000000000..af5d5591a --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pg-91 @@ -0,0 +1 @@ +DELETE FROM "soft_deletes" AS "soft_delete" WHERE "soft_delete"."deleted_at" != '0001-01-01 00:00:00+00:00' AND ("soft_delete"."id" = NULL) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pg-92 b/internal/dbtest/testdata/snapshots/TestQuery-pg-92 new file mode 100644 index 000000000..1c4201125 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pg-92 @@ -0,0 +1 @@ +SELECT "soft_delete"."id", "soft_delete"."deleted_at" FROM "soft_deletes" AS "soft_delete" WHERE "soft_delete"."deleted_at" = '0001-01-01 00:00:00+00:00' diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pg-93 b/internal/dbtest/testdata/snapshots/TestQuery-pg-93 new file mode 100644 index 000000000..6991876ea --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pg-93 @@ -0,0 +1 @@ +SELECT "soft_delete"."id", "soft_delete"."deleted_at" FROM "soft_deletes" AS "soft_delete" WHERE "soft_delete"."deleted_at" != '0001-01-01 00:00:00+00:00' diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pg-94 b/internal/dbtest/testdata/snapshots/TestQuery-pg-94 new file mode 100644 index 000000000..e6be84a61 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pg-94 @@ -0,0 +1 @@ +SELECT "soft_delete"."id", "soft_delete"."deleted_at" FROM "soft_deletes" AS "soft_delete" diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pg-95 b/internal/dbtest/testdata/snapshots/TestQuery-pg-95 new file mode 100644 index 000000000..be12f8e1e --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pg-95 @@ -0,0 +1 @@ +INSERT INTO "soft_deletes" AS "soft_delete" ("id", "deleted_at") VALUES (DEFAULT, '0001-01-01 00:00:00+00:00') ON CONFLICT DO NOTHING RETURNING "id" diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pgx-90 b/internal/dbtest/testdata/snapshots/TestQuery-pgx-90 new file mode 100644 index 000000000..995ff27df --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pgx-90 @@ -0,0 +1 @@ +UPDATE "soft_deletes" AS "soft_delete" SET "deleted_at" = [TIME] WHERE "soft_delete"."deleted_at" = '0001-01-01 00:00:00+00:00' AND ("soft_delete"."id" = NULL) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pgx-91 b/internal/dbtest/testdata/snapshots/TestQuery-pgx-91 new file mode 100644 index 000000000..af5d5591a --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pgx-91 @@ -0,0 +1 @@ +DELETE FROM "soft_deletes" AS "soft_delete" WHERE "soft_delete"."deleted_at" != '0001-01-01 00:00:00+00:00' AND ("soft_delete"."id" = NULL) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pgx-92 b/internal/dbtest/testdata/snapshots/TestQuery-pgx-92 new file mode 100644 index 000000000..1c4201125 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pgx-92 @@ -0,0 +1 @@ +SELECT "soft_delete"."id", "soft_delete"."deleted_at" FROM "soft_deletes" AS "soft_delete" WHERE "soft_delete"."deleted_at" = '0001-01-01 00:00:00+00:00' diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pgx-93 b/internal/dbtest/testdata/snapshots/TestQuery-pgx-93 new file mode 100644 index 000000000..6991876ea --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pgx-93 @@ -0,0 +1 @@ +SELECT "soft_delete"."id", "soft_delete"."deleted_at" FROM "soft_deletes" AS "soft_delete" WHERE "soft_delete"."deleted_at" != '0001-01-01 00:00:00+00:00' diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pgx-94 b/internal/dbtest/testdata/snapshots/TestQuery-pgx-94 new file mode 100644 index 000000000..e6be84a61 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pgx-94 @@ -0,0 +1 @@ +SELECT "soft_delete"."id", "soft_delete"."deleted_at" FROM "soft_deletes" AS "soft_delete" diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pgx-95 b/internal/dbtest/testdata/snapshots/TestQuery-pgx-95 new file mode 100644 index 000000000..be12f8e1e --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pgx-95 @@ -0,0 +1 @@ +INSERT INTO "soft_deletes" AS "soft_delete" ("id", "deleted_at") VALUES (DEFAULT, '0001-01-01 00:00:00+00:00') ON CONFLICT DO NOTHING RETURNING "id" diff --git a/internal/dbtest/testdata/snapshots/TestQuery-sqlite-90 b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-90 new file mode 100644 index 000000000..995ff27df --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-90 @@ -0,0 +1 @@ +UPDATE "soft_deletes" AS "soft_delete" SET "deleted_at" = [TIME] WHERE "soft_delete"."deleted_at" = '0001-01-01 00:00:00+00:00' AND ("soft_delete"."id" = NULL) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-sqlite-91 b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-91 new file mode 100644 index 000000000..af5d5591a --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-91 @@ -0,0 +1 @@ +DELETE FROM "soft_deletes" AS "soft_delete" WHERE "soft_delete"."deleted_at" != '0001-01-01 00:00:00+00:00' AND ("soft_delete"."id" = NULL) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-sqlite-92 b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-92 new file mode 100644 index 000000000..1c4201125 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-92 @@ -0,0 +1 @@ +SELECT "soft_delete"."id", "soft_delete"."deleted_at" FROM "soft_deletes" AS "soft_delete" WHERE "soft_delete"."deleted_at" = '0001-01-01 00:00:00+00:00' diff --git a/internal/dbtest/testdata/snapshots/TestQuery-sqlite-93 b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-93 new file mode 100644 index 000000000..6991876ea --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-93 @@ -0,0 +1 @@ +SELECT "soft_delete"."id", "soft_delete"."deleted_at" FROM "soft_deletes" AS "soft_delete" WHERE "soft_delete"."deleted_at" != '0001-01-01 00:00:00+00:00' diff --git a/internal/dbtest/testdata/snapshots/TestQuery-sqlite-94 b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-94 new file mode 100644 index 000000000..e6be84a61 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-94 @@ -0,0 +1 @@ +SELECT "soft_delete"."id", "soft_delete"."deleted_at" FROM "soft_deletes" AS "soft_delete" diff --git a/internal/dbtest/testdata/snapshots/TestQuery-sqlite-95 b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-95 new file mode 100644 index 000000000..5e0586a0a --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-95 @@ -0,0 +1 @@ +INSERT INTO "soft_deletes" AS "soft_delete" ("deleted_at") VALUES ('0001-01-01 00:00:00+00:00') ON CONFLICT DO NOTHING RETURNING "id" diff --git a/query_base.go b/query_base.go index 8b11c6cef..199c7df13 100644 --- a/query_base.go +++ b/query_base.go @@ -6,6 +6,7 @@ import ( "database/sql/driver" "errors" "fmt" + "time" "github.com/uptrace/bun/dialect/feature" "github.com/uptrace/bun/internal" @@ -628,11 +629,23 @@ func (q *whereBaseQuery) appendWhere( b = append(b, q.tableModel.Table().SQLAlias...) b = append(b, '.') } - b = append(b, q.tableModel.Table().SoftDeleteField.SQLName...) - if q.flags.Has(deletedFlag) { - b = append(b, " IS NOT NULL"...) + + field := q.tableModel.Table().SoftDeleteField + b = append(b, field.SQLName...) + + if field.NullZero { + if q.flags.Has(deletedFlag) { + b = append(b, " IS NOT NULL"...) + } else { + b = append(b, " IS NULL"...) + } } else { - b = append(b, " IS NULL"...) + if q.flags.Has(deletedFlag) { + b = append(b, " != "...) + } else { + b = append(b, " = "...) + } + b = fmter.Dialect().AppendTime(b, time.Time{}) } } diff --git a/schema/table.go b/schema/table.go index 213f821ab..827c12f69 100644 --- a/schema/table.go +++ b/schema/table.go @@ -334,15 +334,6 @@ func (t *Table) newField(f reflect.StructField, index []int) *Field { if tag.HasOption("pk") { field.markAsPK() } - if tag.HasOption("allowzero") { - if tag.HasOption("nullzero") { - internal.Warn.Printf( - "%s.%s: nullzero and allowzero options are mutually exclusive", - t.TypeName, f.Name, - ) - } - field.NullZero = false - } if v, ok := tag.Options["unique"]; ok { // Split the value by comma, this will allow multiple names to be specified. @@ -385,6 +376,17 @@ func (t *Table) newField(f reflect.StructField, index []int) *Field { t.UpdateSoftDeleteField = softDeleteFieldUpdater(field) } + // Check this in the end to undo NullZero. + if tag.HasOption("allowzero") { + if tag.HasOption("nullzero") { + internal.Warn.Printf( + "%s.%s: nullzero and allowzero options are mutually exclusive", + t.TypeName, f.Name, + ) + } + field.NullZero = false + } + return field }