mirror of
1
Fork 0

upstep bun version (#243)

This commit is contained in:
tobi 2021-09-23 11:13:28 +02:00 committed by GitHub
parent ddfd83d0fb
commit 142f37f1bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 146 additions and 57 deletions

6
go.mod
View File

@ -28,9 +28,9 @@ require (
github.com/superseriousbusiness/exifremove v0.0.0-20210330092427-6acd27eac203 github.com/superseriousbusiness/exifremove v0.0.0-20210330092427-6acd27eac203
github.com/superseriousbusiness/oauth2/v4 v4.3.2-SSB github.com/superseriousbusiness/oauth2/v4 v4.3.2-SSB
github.com/tdewolff/minify/v2 v2.9.21 github.com/tdewolff/minify/v2 v2.9.21
github.com/uptrace/bun v1.0.6 github.com/uptrace/bun v1.0.9-0.20210922104131-34c982b23581
github.com/uptrace/bun/dialect/pgdialect v1.0.5 github.com/uptrace/bun/dialect/pgdialect v1.0.9-0.20210922104131-34c982b23581
github.com/uptrace/bun/dialect/sqlitedialect v1.0.5 github.com/uptrace/bun/dialect/sqlitedialect v1.0.9-0.20210922104131-34c982b23581
github.com/urfave/cli/v2 v2.3.0 github.com/urfave/cli/v2 v2.3.0
github.com/wagslane/go-password-validator v0.3.0 github.com/wagslane/go-password-validator v0.3.0
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 golang.org/x/crypto v0.0.0-20210817164053-32db794688a5

6
go.sum
View File

@ -472,10 +472,16 @@ github.com/ugorji/go/codec v1.2.6/go.mod h1:V6TCNZ4PHqoHGFZuSG1W8nrCzzdgA2DozYxW
github.com/uptrace/bun v1.0.5/go.mod h1:aL6D9vPw8DXaTQTwGrEPtUderBYXx7ShUmPfnxnqscw= github.com/uptrace/bun v1.0.5/go.mod h1:aL6D9vPw8DXaTQTwGrEPtUderBYXx7ShUmPfnxnqscw=
github.com/uptrace/bun v1.0.6 h1:o9eMq5ePGBXtxbK3SIreOCRr+rIBQzvJH+/s98kYcVM= github.com/uptrace/bun v1.0.6 h1:o9eMq5ePGBXtxbK3SIreOCRr+rIBQzvJH+/s98kYcVM=
github.com/uptrace/bun v1.0.6/go.mod h1:aL6D9vPw8DXaTQTwGrEPtUderBYXx7ShUmPfnxnqscw= github.com/uptrace/bun v1.0.6/go.mod h1:aL6D9vPw8DXaTQTwGrEPtUderBYXx7ShUmPfnxnqscw=
github.com/uptrace/bun v1.0.9-0.20210922104131-34c982b23581 h1:W8SAI7irrKSZ3t9MzFwehUyEd6f8ajOprqSzCxHFcxo=
github.com/uptrace/bun v1.0.9-0.20210922104131-34c982b23581/go.mod h1:aL6D9vPw8DXaTQTwGrEPtUderBYXx7ShUmPfnxnqscw=
github.com/uptrace/bun/dialect/pgdialect v1.0.5 h1:mq694/aMvs7GwuTar9NIlCLQt/2u4xsF0QMP4I24yHA= github.com/uptrace/bun/dialect/pgdialect v1.0.5 h1:mq694/aMvs7GwuTar9NIlCLQt/2u4xsF0QMP4I24yHA=
github.com/uptrace/bun/dialect/pgdialect v1.0.5/go.mod h1:MKWjO0PC20ris2oJ3dd6mI/762x24Cjwh8XmbqUhM8A= github.com/uptrace/bun/dialect/pgdialect v1.0.5/go.mod h1:MKWjO0PC20ris2oJ3dd6mI/762x24Cjwh8XmbqUhM8A=
github.com/uptrace/bun/dialect/pgdialect v1.0.9-0.20210922104131-34c982b23581 h1:3r8Td1Y3I51f9LYcC/3EuQT8zKuCh+OWiQQ8FVHK4Pg=
github.com/uptrace/bun/dialect/pgdialect v1.0.9-0.20210922104131-34c982b23581/go.mod h1:HEGRgyS68SiHcKhFa9LXcDN+KEWo1I4VplvunL0Oi4o=
github.com/uptrace/bun/dialect/sqlitedialect v1.0.5 h1:6cIj31YVJr4vvA15C2v76soXL+7WtjFdV6WraApc3r0= github.com/uptrace/bun/dialect/sqlitedialect v1.0.5 h1:6cIj31YVJr4vvA15C2v76soXL+7WtjFdV6WraApc3r0=
github.com/uptrace/bun/dialect/sqlitedialect v1.0.5/go.mod h1:NW2Gctc9ooQXGSD4kYSac2aiF49lo8YJ3ZAr93lH2p8= github.com/uptrace/bun/dialect/sqlitedialect v1.0.5/go.mod h1:NW2Gctc9ooQXGSD4kYSac2aiF49lo8YJ3ZAr93lH2p8=
github.com/uptrace/bun/dialect/sqlitedialect v1.0.9-0.20210922104131-34c982b23581 h1:Yfbbo8EQffFLL7EEBq2yUirSg3b7NID4sgRGdNlIJa0=
github.com/uptrace/bun/dialect/sqlitedialect v1.0.9-0.20210922104131-34c982b23581/go.mod h1:v1rNdAcJdw8AgD4x4OAJFIRFA9+sANoXK7u21H9Wvkg=
github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M= github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M=
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI= github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=

View File

@ -1,3 +1,31 @@
## [1.0.8](https://github.com/uptrace/bun/compare/v1.0.7...v1.0.8) (2021-09-18)
### Bug Fixes
* don't append soft delete where for insert queries with on conflict clause ([27c477c](https://github.com/uptrace/bun/commit/27c477ce071d4c49c99a2531d638ed9f20e33461))
* improve bun.NullTime to accept string ([73ad6f5](https://github.com/uptrace/bun/commit/73ad6f5640a0a9b09f8df2bc4ab9cb510021c50c))
* make allowzero work with auto-detected primary keys ([82ca87c](https://github.com/uptrace/bun/commit/82ca87c7c49797d507b31fdaacf8343716d4feff))
* support soft deletes on nil model ([0556e3c](https://github.com/uptrace/bun/commit/0556e3c63692a7f4e48659d52b55ffd9cca0202a))
## [1.0.7](https://github.com/uptrace/bun/compare/v1.0.6...v1.0.7) (2021-09-15)
### Bug Fixes
* don't append zero time as NULL without nullzero tag ([3b8d9cb](https://github.com/uptrace/bun/commit/3b8d9cb4e39eb17f79a618396bbbe0adbc66b07b))
* **pgdriver:** return PostgreSQL DATE as a string ([40be0e8](https://github.com/uptrace/bun/commit/40be0e8ea85f8932b7a410a6fc2dd3acd2d18ebc))
* specify table alias for soft delete where ([5fff1dc](https://github.com/uptrace/bun/commit/5fff1dc1dd74fa48623a24fa79e358a544dfac0b))
### Features
* add SelectQuery.Exists helper ([c3e59c1](https://github.com/uptrace/bun/commit/c3e59c1bc58b43c4b8e33e7d170ad33a08fbc3c7))
## [1.0.6](https://github.com/uptrace/bun/compare/v1.0.5...v1.0.6) (2021-09-11) ## [1.0.6](https://github.com/uptrace/bun/compare/v1.0.5...v1.0.6) (2021-09-11)

View File

@ -4,7 +4,8 @@ test:
set -e; for dir in $(ALL_GO_MOD_DIRS); do \ set -e; for dir in $(ALL_GO_MOD_DIRS); do \
echo "go test in $${dir}"; \ echo "go test in $${dir}"; \
(cd "$${dir}" && \ (cd "$${dir}" && \
go test ./... && \ go test && \
env GOOS=linux GOARCH=386 go test && \
go vet); \ go vet); \
done done

View File

@ -30,6 +30,9 @@ Main features are:
Resources: Resources:
- To ask questions, join [Discord](https://discord.gg/rWtp5Aj) or use
[Discussions](https://github.com/uptrace/bun/discussions).
- [Newsletter](https://blog.uptrace.dev/newsletter/) to get latest updates.
- [Examples](https://github.com/uptrace/bun/tree/master/example) - [Examples](https://github.com/uptrace/bun/tree/master/example)
- [Documentation](https://bun.uptrace.dev/) - [Documentation](https://bun.uptrace.dev/)
- [Reference](https://pkg.go.dev/github.com/uptrace/bun) - [Reference](https://pkg.go.dev/github.com/uptrace/bun)

View File

@ -18,8 +18,8 @@ const (
) )
type DBStats struct { type DBStats struct {
Queries uint64 Queries uint32
Errors uint64 Errors uint32
} }
type DBOption func(db *DB) type DBOption func(db *DB)
@ -70,8 +70,8 @@ func (db *DB) String() string {
func (db *DB) DBStats() DBStats { func (db *DB) DBStats() DBStats {
return DBStats{ return DBStats{
Queries: atomic.LoadUint64(&db.stats.Queries), Queries: atomic.LoadUint32(&db.stats.Queries),
Errors: atomic.LoadUint64(&db.stats.Errors), Errors: atomic.LoadUint32(&db.stats.Errors),
} }
} }

View File

@ -95,9 +95,6 @@ func AppendBytes(b []byte, bytes []byte) []byte {
} }
func AppendTime(b []byte, tm time.Time) []byte { func AppendTime(b []byte, tm time.Time) []byte {
if tm.IsZero() {
return AppendNull(b)
}
b = append(b, '\'') b = append(b, '\'')
b = tm.UTC().AppendFormat(b, "2006-01-02 15:04:05.999999-07:00") b = tm.UTC().AppendFormat(b, "2006-01-02 15:04:05.999999-07:00")
b = append(b, '\'') b = append(b, '\'')

View File

@ -53,7 +53,7 @@ func (db *DB) beforeQuery(
query string, query string,
queryArgs []interface{}, queryArgs []interface{},
) (context.Context, *QueryEvent) { ) (context.Context, *QueryEvent) {
atomic.AddUint64(&db.stats.Queries, 1) atomic.AddUint32(&db.stats.Queries, 1)
if len(db.queryHooks) == 0 { if len(db.queryHooks) == 0 {
return ctx, nil return ctx, nil
@ -86,7 +86,7 @@ func (db *DB) afterQuery(
case nil, sql.ErrNoRows: case nil, sql.ErrNoRows:
// nothing // nothing
default: default:
atomic.AddUint64(&db.stats.Errors, 1) atomic.AddUint32(&db.stats.Errors, 1)
} }
if event == nil { if event == nil {

View File

@ -190,7 +190,7 @@ func (ms MigrationSlice) LastGroupID() int64 {
var lastGroupID int64 var lastGroupID int64
for i := range ms { for i := range ms {
groupID := ms[i].GroupID groupID := ms[i].GroupID
if groupID != 0 && groupID > lastGroupID { if groupID > lastGroupID {
lastGroupID = groupID lastGroupID = groupID
} }
} }

View File

@ -216,6 +216,9 @@ func (m *structTableModel) join(bind reflect.Value, name string) *relationJoin {
} }
func (m *structTableModel) updateSoftDeleteField(tm time.Time) error { func (m *structTableModel) updateSoftDeleteField(tm time.Time) error {
if !m.strct.IsValid() {
return nil
}
fv := m.table.SoftDeleteField.Value(m.strct) fv := m.table.SoftDeleteField.Value(m.strct)
return m.table.UpdateSoftDeleteField(fv, tm) return m.table.UpdateSoftDeleteField(fv, tm)
} }

View File

@ -1,6 +1,6 @@
{ {
"name": "bun", "name": "bun",
"version": "1.0.6", "version": "1.0.8",
"main": "index.js", "main": "index.js",
"repository": "git@github.com:uptrace/bun.git", "repository": "git@github.com:uptrace/bun.git",
"author": "Vladimir Mihailenco <vladimir.webdev@gmail.com>", "author": "Vladimir Mihailenco <vladimir.webdev@gmail.com>",

View File

@ -155,7 +155,7 @@ func (q *DeleteQuery) AppendQuery(fmter schema.Formatter, b []byte) (_ []byte, e
return upd.AppendQuery(fmter, b) return upd.AppendQuery(fmter, b)
} }
q = q.WhereAllWithDeleted() q = q.WhereDeleted()
withAlias := q.db.features.Has(feature.DeleteTableAlias) withAlias := q.db.features.Has(feature.DeleteTableAlias)
b, err = q.appendWith(fmter, b) b, err = q.appendWith(fmter, b)

View File

@ -431,10 +431,14 @@ func (q *InsertQuery) appendOn(fmter schema.Formatter, b []byte) (_ []byte, err
b = q.appendSetExcluded(b, fields) b = q.appendSetExcluded(b, fields)
} }
b, err = q.appendWhere(fmter, b, true) if len(q.where) > 0 {
b = append(b, " WHERE "...)
b, err = appendWhere(fmter, b, q.where)
if err != nil { if err != nil {
return nil, err return nil, err
} }
}
return b, nil return b, nil
} }

View File

@ -738,7 +738,7 @@ func (q *SelectQuery) afterSelectHook(ctx context.Context) error {
func (q *SelectQuery) Count(ctx context.Context) (int, error) { func (q *SelectQuery) Count(ctx context.Context) (int, error) {
qq := countQuery{q} qq := countQuery{q}
queryBytes, err := qq.appendQuery(q.db.fmter, nil, true) queryBytes, err := qq.AppendQuery(q.db.fmter, nil)
if err != nil { if err != nil {
return 0, err return 0, err
} }
@ -794,6 +794,25 @@ func (q *SelectQuery) ScanAndCount(ctx context.Context, dest ...interface{}) (in
return count, firstErr return count, firstErr
} }
func (q *SelectQuery) Exists(ctx context.Context) (bool, error) {
qq := existsQuery{q}
queryBytes, err := qq.AppendQuery(q.db.fmter, nil)
if err != nil {
return false, err
}
query := internal.String(queryBytes)
ctx, event := q.db.beforeQuery(ctx, qq, query, nil)
var exists bool
err = q.conn.QueryRowContext(ctx, query).Scan(&exists)
q.db.afterQuery(ctx, event, nil, err)
return exists, err
}
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
type joinQuery struct { type joinQuery struct {
@ -837,3 +856,22 @@ type countQuery struct {
func (q countQuery) AppendQuery(fmter schema.Formatter, b []byte) (_ []byte, err error) { func (q countQuery) AppendQuery(fmter schema.Formatter, b []byte) (_ []byte, err error) {
return q.appendQuery(fmter, b, true) return q.appendQuery(fmter, b, true)
} }
//------------------------------------------------------------------------------
type existsQuery struct {
*SelectQuery
}
func (q existsQuery) AppendQuery(fmter schema.Formatter, b []byte) (_ []byte, err error) {
b = append(b, "SELECT EXISTS ("...)
b, err = q.appendQuery(fmter, b, false)
if err != nil {
return nil, err
}
b = append(b, ")"...)
return b, nil
}

View File

@ -170,8 +170,6 @@ func (q *UpdateQuery) AppendQuery(fmter schema.Formatter, b []byte) (_ []byte, e
} }
fmter = formatterWithModel(fmter, q) fmter = formatterWithModel(fmter, q)
withAlias := fmter.HasFeature(feature.UpdateMultiTable)
b, err = q.appendWith(fmter, b) b, err = q.appendWith(fmter, b)
if err != nil { if err != nil {
return nil, err return nil, err
@ -179,7 +177,7 @@ func (q *UpdateQuery) AppendQuery(fmter schema.Formatter, b []byte) (_ []byte, e
b = append(b, "UPDATE "...) b = append(b, "UPDATE "...)
if withAlias { if fmter.HasFeature(feature.UpdateMultiTable) {
b, err = q.appendTablesWithAlias(fmter, b) b, err = q.appendTablesWithAlias(fmter, b)
} else { } else {
b, err = q.appendFirstTableWithAlias(fmter, b) b, err = q.appendFirstTableWithAlias(fmter, b)
@ -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 { if err != nil {
return nil, err return nil, err
} }

View File

@ -3,29 +3,14 @@ package schema
import ( import (
"reflect" "reflect"
"strconv" "strconv"
"strings"
"time" "time"
"github.com/vmihailenco/msgpack/v5" "github.com/vmihailenco/msgpack/v5"
"github.com/uptrace/bun/dialect" "github.com/uptrace/bun/dialect"
"github.com/uptrace/bun/dialect/sqltype"
"github.com/uptrace/bun/internal" "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 { func Append(fmter Formatter, b []byte, v interface{}, custom CustomAppender) []byte {
switch v := v.(type) { switch v := v.(type) {
case nil: case nil:

View File

@ -6,9 +6,11 @@ import (
"net" "net"
"reflect" "reflect"
"strconv" "strconv"
"strings"
"time" "time"
"github.com/uptrace/bun/dialect" "github.com/uptrace/bun/dialect"
"github.com/uptrace/bun/dialect/sqltype"
"github.com/uptrace/bun/extra/bunjson" "github.com/uptrace/bun/extra/bunjson"
"github.com/uptrace/bun/internal" "github.com/uptrace/bun/internal"
) )
@ -47,6 +49,19 @@ var appenders = []AppenderFunc{
reflect.UnsafePointer: nil, 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 { func Appender(typ reflect.Type, custom CustomAppender) AppenderFunc {
switch typ { switch typ {
case bytesType: case bytesType:

View File

@ -101,7 +101,9 @@ func (f *Field) ScanValue(strct reflect.Value, src interface{}) error {
func (f *Field) markAsPK() { func (f *Field) markAsPK() {
f.IsPK = true f.IsPK = true
f.NotNull = true f.NotNull = true
if !f.Tag.HasOption("allowzero") {
f.NullZero = true f.NullZero = true
}
} }
func indexEqual(ind1, ind2 []int) bool { func indexEqual(ind1, ind2 []int) bool {

View File

@ -207,6 +207,9 @@ func scanString(dest reflect.Value, src interface{}) error {
case []byte: case []byte:
dest.SetString(string(src)) dest.SetString(string(src))
return nil return nil
case time.Time:
dest.SetString(src.Format(time.RFC3339Nano))
return nil
} }
return fmt.Errorf("bun: can't scan %#v into %s", src, dest.Type()) return fmt.Errorf("bun: can't scan %#v into %s", src, dest.Type())
} }

View File

@ -109,17 +109,23 @@ func (tm *NullTime) Scan(src interface{}) error {
} }
switch src := src.(type) { switch src := src.(type) {
case time.Time:
tm.Time = src
return nil
case string:
newtm, err := internal.ParseTime(src)
if err != nil {
return err
}
tm.Time = newtm
return nil
case []byte: case []byte:
newtm, err := internal.ParseTime(internal.String(src)) newtm, err := internal.ParseTime(internal.String(src))
if err != nil { if err != nil {
return err return err
} }
tm.Time = newtm tm.Time = newtm
return nil return nil
case time.Time:
tm.Time = src
return nil
default: default:
return fmt.Errorf("bun: can't scan %#v into NullTime", src) return fmt.Errorf("bun: can't scan %#v into NullTime", src)
} }

View File

@ -181,9 +181,7 @@ func (t *Table) initFields() {
t.FieldMap = make(map[string]*Field, t.Type.NumField()) t.FieldMap = make(map[string]*Field, t.Type.NumField())
t.addFields(t.Type, nil) t.addFields(t.Type, nil)
if len(t.PKs) > 0 { if len(t.PKs) == 0 {
return
}
for _, name := range []string{"id", "uuid", "pk_" + t.ModelName} { for _, name := range []string{"id", "uuid", "pk_" + t.ModelName} {
if field, ok := t.FieldMap[name]; ok { if field, ok := t.FieldMap[name]; ok {
field.markAsPK() field.markAsPK()
@ -192,6 +190,8 @@ func (t *Table) initFields() {
break break
} }
} }
}
if len(t.PKs) == 1 { if len(t.PKs) == 1 {
pk := t.PKs[0] pk := t.PKs[0]
if pk.SQLDefault != "" { if pk.SQLDefault != "" {

View File

@ -2,5 +2,5 @@ package bun
// Version is the current release version. // Version is the current release version.
func Version() string { func Version() string {
return "1.0.6" return "1.0.8"
} }

6
vendor/modules.txt vendored
View File

@ -447,7 +447,7 @@ github.com/tmthrgd/go-hex
# github.com/ugorji/go/codec v1.2.6 # github.com/ugorji/go/codec v1.2.6
## explicit; go 1.11 ## explicit; go 1.11
github.com/ugorji/go/codec github.com/ugorji/go/codec
# github.com/uptrace/bun v1.0.6 # github.com/uptrace/bun v1.0.9-0.20210922104131-34c982b23581
## explicit; go 1.16 ## explicit; go 1.16
github.com/uptrace/bun github.com/uptrace/bun
github.com/uptrace/bun/dialect github.com/uptrace/bun/dialect
@ -459,10 +459,10 @@ github.com/uptrace/bun/internal/parser
github.com/uptrace/bun/internal/tagparser github.com/uptrace/bun/internal/tagparser
github.com/uptrace/bun/migrate github.com/uptrace/bun/migrate
github.com/uptrace/bun/schema github.com/uptrace/bun/schema
# github.com/uptrace/bun/dialect/pgdialect v1.0.5 # github.com/uptrace/bun/dialect/pgdialect v1.0.9-0.20210922104131-34c982b23581
## explicit; go 1.16 ## explicit; go 1.16
github.com/uptrace/bun/dialect/pgdialect github.com/uptrace/bun/dialect/pgdialect
# github.com/uptrace/bun/dialect/sqlitedialect v1.0.5 # github.com/uptrace/bun/dialect/sqlitedialect v1.0.9-0.20210922104131-34c982b23581
## explicit; go 1.16 ## explicit; go 1.16
github.com/uptrace/bun/dialect/sqlitedialect github.com/uptrace/bun/dialect/sqlitedialect
# github.com/urfave/cli/v2 v2.3.0 # github.com/urfave/cli/v2 v2.3.0