Skip to content

Commit

Permalink
fix: don't append zero time as NULL without nullzero tag
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Sep 15, 2021
1 parent 4e3f5d7 commit 3b8d9cb
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 22 deletions.
3 changes: 0 additions & 3 deletions dialect/append.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,6 @@ func AppendBytes(b []byte, bytes []byte) []byte {
}

func AppendTime(b []byte, tm time.Time) []byte {
if tm.IsZero() {
return AppendNull(b)
}
b = append(b, '\'')
b = tm.UTC().AppendFormat(b, "2006-01-02 15:04:05.999999-07:00")
b = append(b, '\'')
Expand Down
3 changes: 0 additions & 3 deletions dialect/mysqldialect/append.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ func appendTimeValue(fmter schema.Formatter, b []byte, v reflect.Value) []byte {
}

func appendTime(b []byte, tm time.Time) []byte {
if tm.IsZero() {
return dialect.AppendNull(b)
}
b = append(b, '\'')
b = tm.UTC().AppendFormat(b, "2006-01-02 15:04:05.999999")
b = append(b, '\'')
Expand Down
4 changes: 3 additions & 1 deletion internal/dbtest/orm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ func testRelationColumn(t *testing.T, db *bun.DB) {
book := new(Book)
err := db.NewSelect().
Model(book).
ExcludeColumn("created_at").
Relation("Author", func(q *bun.SelectQuery) *bun.SelectQuery {
return q.Column("name")
}).
Expand All @@ -286,6 +287,7 @@ func testRelationExcludeAll(t *testing.T, db *bun.DB) {
book := new(Book)
err := db.NewSelect().
Model(book).
ExcludeColumn("created_at").
Relation("Author", func(q *bun.SelectQuery) *bun.SelectQuery {
return q.ExcludeColumn("*")
}).
Expand Down Expand Up @@ -420,7 +422,7 @@ type Book struct {
Author Author `bun:"rel:belongs-to"`
EditorID int
Editor *Author `bun:"rel:belongs-to"`
CreatedAt time.Time `bun:"default:current_timestamp"`
CreatedAt time.Time `bun:",nullzero,default:current_timestamp"`
UpdatedAt time.Time `bun:",nullzero"`

Genres []Genre `bun:"m2m:book_genres"` // many to many relation
Expand Down
6 changes: 6 additions & 0 deletions internal/dbtest/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,12 @@ func TestQuery(t *testing.T) {
}
return db.NewCreateTable().Model((*Model)(nil))
},
func(db *bun.DB) schema.QueryAppender {
type Model struct {
Time time.Time `bun:",notnull"`
}
return db.NewInsert().Model(new(Model))
},
}

timeRE := regexp.MustCompile(`'\d{4}-\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-mysql5-84
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INSERT INTO `models` (`time`) VALUES ('0001-01-01 00:00:00')
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql8-84
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INSERT INTO `models` (`time`) VALUES ('0001-01-01 00:00:00')
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pg-84
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INSERT INTO "models" ("time") VALUES ('0001-01-01 00:00:00+00:00')
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pgx-84
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INSERT INTO "models" ("time") VALUES ('0001-01-01 00:00:00+00:00')
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-sqlite-84
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INSERT INTO "models" ("time") VALUES ('0001-01-01 00:00:00+00:00')
15 changes: 0 additions & 15 deletions schema/append.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,14 @@ package schema
import (
"reflect"
"strconv"
"strings"
"time"

"github.com/vmihailenco/msgpack/v5"

"github.com/uptrace/bun/dialect"
"github.com/uptrace/bun/dialect/sqltype"
"github.com/uptrace/bun/internal"
)

func FieldAppender(dialect Dialect, field *Field) AppenderFunc {
if field.Tag.HasOption("msgpack") {
return appendMsgpack
}

switch strings.ToUpper(field.UserSQLType) {
case sqltype.JSON, sqltype.JSONB:
return AppendJSONValue
}

return dialect.Appender(field.StructField.Type)
}

func Append(fmter Formatter, b []byte, v interface{}, custom CustomAppender) []byte {
switch v := v.(type) {
case nil:
Expand Down
15 changes: 15 additions & 0 deletions schema/append_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import (
"net"
"reflect"
"strconv"
"strings"
"time"

"github.com/uptrace/bun/dialect"
"github.com/uptrace/bun/dialect/sqltype"
"github.com/uptrace/bun/extra/bunjson"
"github.com/uptrace/bun/internal"
)
Expand Down Expand Up @@ -47,6 +49,19 @@ var appenders = []AppenderFunc{
reflect.UnsafePointer: nil,
}

func FieldAppender(dialect Dialect, field *Field) AppenderFunc {
if field.Tag.HasOption("msgpack") {
return appendMsgpack
}

switch strings.ToUpper(field.UserSQLType) {
case sqltype.JSON, sqltype.JSONB:
return AppendJSONValue
}

return dialect.Appender(field.StructField.Type)
}

func Appender(typ reflect.Type, custom CustomAppender) AppenderFunc {
switch typ {
case bytesType:
Expand Down

0 comments on commit 3b8d9cb

Please sign in to comment.