Skip to content

Commit

Permalink
Merge pull request #3 from itering/depend/bump-v1.21.15
Browse files Browse the repository at this point in the history
Bump gorm to v1.21.15
  • Loading branch information
mittwillson authored Sep 27, 2021
2 parents 8771f3c + e62a53b commit 8dad454
Show file tree
Hide file tree
Showing 71 changed files with 2,014 additions and 837 deletions.
15 changes: 15 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
version: 2
updates:
- package-ecosystem: gomod
directory: /
schedule:
interval: weekly
- package-ecosystem: github-actions
directory: /
schedule:
interval: weekly
- package-ecosystem: gomod
directory: /tests
schedule:
interval: weekly
2 changes: 1 addition & 1 deletion .github/workflows/invalid_question.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
ACTIONS_STEP_DEBUG: true
steps:
- name: Close Stale Issues
uses: actions/stale@v3.0.7
uses: actions/stale@v4
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
stale-issue-message: "This issue has been marked as invalid question, please give more information by following the `Question` template, if you believe there is a bug of GORM, please create a pull request that could reproduce the issue on [https://github.com/go-gorm/playground](https://github.com/go-gorm/playground), the issue will be closed in 2 days if no further activity occurs. most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ [Search Before Asking](https://stackoverflow.com/help/how-to-ask) ✨"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/missing_playground.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
ACTIONS_STEP_DEBUG: true
steps:
- name: Close Stale Issues
uses: actions/stale@v3.0.7
uses: actions/stale@v4
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
stale-issue-message: "The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout [https://github.com/go-gorm/playground](https://github.com/go-gorm/playground) for details. it will be closed in 2 days if no further activity occurs. if you are asking question, please use the `Question` template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ [Search Before Asking](https://stackoverflow.com/help/how-to-ask) ✨"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/reviewdog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ jobs:
- name: Check out code into the Go module directory
uses: actions/checkout@v1
- name: golangci-lint
uses: reviewdog/action-golangci-lint@v1
uses: reviewdog/action-golangci-lint@v2
2 changes: 1 addition & 1 deletion .github/workflows/stale.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
ACTIONS_STEP_DEBUG: true
steps:
- name: Close Stale Issues
uses: actions/stale@v3.0.7
uses: actions/stale@v4
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
stale-issue-message: "This issue has been automatically marked as stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 30 days"
Expand Down
41 changes: 8 additions & 33 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ jobs:
sqlite:
strategy:
matrix:
go: ['1.15', '1.14', '1.13']
platform: [ubuntu-latest, macos-latest] # can not run in windows OS
go: ['1.17', '1.16', '1.15']
platform: [ubuntu-latest] # can not run in windows OS
runs-on: ${{ matrix.platform }}

steps:
Expand All @@ -35,36 +35,11 @@ jobs:
- name: Tests
run: GORM_DIALECT=sqlite ./tests/tests_all.sh

sqlite_windows:
strategy:
matrix:
go: ['1.15', '1.14', '1.13']
platform: [windows-latest]
runs-on: ${{ matrix.platform }}

steps:
- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go }}

- name: Check out code into the Go module directory
uses: actions/checkout@v2

- name: go mod package cache
uses: actions/cache@v2
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ matrix.go }}-${{ hashFiles('tests/go.mod') }}

- name: Tests
run: cd tests && set GORM_DIALECT=sqlite && go test $race -count=1 -v ./... #run the line in widnows's CMD, default GORM_DIALECT is sqlite

mysql:
strategy:
matrix:
dbversion: ['mysql:latest', 'mysql:5.7', 'mysql:5.6', 'mariadb:latest']
go: ['1.15', '1.14', '1.13']
dbversion: ['mysql:latest', 'mysql:5.7', 'mariadb:latest']
go: ['1.17', '1.16', '1.15']
platform: [ubuntu-latest]
runs-on: ${{ matrix.platform }}

Expand Down Expand Up @@ -107,9 +82,9 @@ jobs:
postgres:
strategy:
matrix:
dbversion: ['postgres:latest', 'postgres:11', 'postgres:10']
go: ['1.15', '1.14', '1.13']
platform: [ubuntu-latest] # can not run in macOS and widnowsOS
dbversion: ['postgres:latest', 'postgres:12', 'postgres:11', 'postgres:10']
go: ['1.17', '1.16', '1.15']
platform: [ubuntu-latest] # can not run in macOS and Windows
runs-on: ${{ matrix.platform }}

services:
Expand Down Expand Up @@ -150,7 +125,7 @@ jobs:
sqlserver:
strategy:
matrix:
go: ['1.15', '1.14', '1.13']
go: ['1.17', '1.16', '1.15']
platform: [ubuntu-latest] # can not run test in macOS and windows
runs-on: ${{ matrix.platform }}

Expand Down
7 changes: 3 additions & 4 deletions association.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package gorm

import (
"errors"
"fmt"
"reflect"
"strings"
Expand All @@ -27,7 +26,7 @@ func (db *DB) Association(column string) *Association {
association.Relationship = db.Statement.Schema.Relationships.Relations[column]

if association.Relationship == nil {
association.Error = fmt.Errorf("%w: %v", ErrUnsupportedRelation, column)
association.Error = fmt.Errorf("%w: %s", ErrUnsupportedRelation, column)
}

db.Statement.ReflectValue = reflect.ValueOf(db.Statement.Model)
Expand Down Expand Up @@ -356,7 +355,7 @@ func (association *Association) saveAssociation(clear bool, values ...interface{
} else if ev.Type().Elem().AssignableTo(elemType) {
fieldValue = reflect.Append(fieldValue, ev.Elem())
} else {
association.Error = fmt.Errorf("unsupported data type: %v for relation %v", ev.Type(), association.Relationship.Name)
association.Error = fmt.Errorf("unsupported data type: %v for relation %s", ev.Type(), association.Relationship.Name)
}

if elemType.Kind() == reflect.Struct {
Expand Down Expand Up @@ -441,7 +440,7 @@ func (association *Association) saveAssociation(clear bool, values ...interface{
break
}

association.Error = errors.New("invalid association values, length doesn't match")
association.Error = ErrInvalidValueOfLength
return
}

Expand Down
52 changes: 39 additions & 13 deletions callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type callbacks struct {

type processor struct {
db *DB
Clauses []string
fns []func(*DB)
callbacks []*callback
}
Expand Down Expand Up @@ -71,38 +72,57 @@ func (cs *callbacks) Raw() *processor {
return cs.processors["raw"]
}

func (p *processor) Execute(db *DB) {
curTime := time.Now()
stmt := db.Statement
func (p *processor) Execute(db *DB) *DB {
// call scopes
for len(db.Statement.scopes) > 0 {
scopes := db.Statement.scopes
db.Statement.scopes = nil
for _, scope := range scopes {
db = scope(db)
}
}

var (
curTime = time.Now()
stmt = db.Statement
resetBuildClauses bool
)

if len(stmt.BuildClauses) == 0 {
stmt.BuildClauses = p.Clauses
resetBuildClauses = true
}

// assign model values
if stmt.Model == nil {
stmt.Model = stmt.Dest
} else if stmt.Dest == nil {
stmt.Dest = stmt.Model
}

// parse model values
if stmt.Model != nil {
if err := stmt.Parse(stmt.Model); err != nil && (!errors.Is(err, schema.ErrUnsupportedDataType) || (stmt.Table == "" && stmt.SQL.Len() == 0)) {
if errors.Is(err, schema.ErrUnsupportedDataType) && stmt.Table == "" {
if err := stmt.Parse(stmt.Model); err != nil && (!errors.Is(err, schema.ErrUnsupportedDataType) || (stmt.Table == "" && stmt.TableExpr == nil && stmt.SQL.Len() == 0)) {
if errors.Is(err, schema.ErrUnsupportedDataType) && stmt.Table == "" && stmt.TableExpr == nil {
db.AddError(fmt.Errorf("%w: Table not set, please set it like: db.Model(&user) or db.Table(\"users\")", err))
} else {
db.AddError(err)
}
}
}

// assign stmt.ReflectValue
if stmt.Dest != nil {
stmt.ReflectValue = reflect.ValueOf(stmt.Dest)
for stmt.ReflectValue.Kind() == reflect.Ptr {
if stmt.ReflectValue.IsNil() {
if stmt.ReflectValue.IsNil() && stmt.ReflectValue.CanAddr() {
stmt.ReflectValue.Set(reflect.New(stmt.ReflectValue.Type().Elem()))
break
}

stmt.ReflectValue = stmt.ReflectValue.Elem()
}
if !stmt.ReflectValue.IsValid() {
db.AddError(fmt.Errorf("invalid value"))
db.AddError(ErrInvalidValue)
}
}

Expand All @@ -118,6 +138,12 @@ func (p *processor) Execute(db *DB) {
stmt.SQL.Reset()
stmt.Vars = nil
}

if resetBuildClauses {
stmt.BuildClauses = nil
}

return db
}

func (p *processor) Get(name string) func(*DB) {
Expand Down Expand Up @@ -186,15 +212,15 @@ func (c *callback) Register(name string, fn func(*DB)) error {
}

func (c *callback) Remove(name string) error {
c.processor.db.Logger.Warn(context.Background(), "removing callback `%v` from %v\n", name, utils.FileWithLineNum())
c.processor.db.Logger.Warn(context.Background(), "removing callback `%s` from %s\n", name, utils.FileWithLineNum())
c.name = name
c.remove = true
c.processor.callbacks = append(c.processor.callbacks, c)
return c.processor.compile()
}

func (c *callback) Replace(name string, fn func(*DB)) error {
c.processor.db.Logger.Info(context.Background(), "replacing callback `%v` from %v\n", name, utils.FileWithLineNum())
c.processor.db.Logger.Info(context.Background(), "replacing callback `%s` from %s\n", name, utils.FileWithLineNum())
c.name = name
c.handler = fn
c.replace = true
Expand Down Expand Up @@ -224,7 +250,7 @@ func sortCallbacks(cs []*callback) (fns []func(*DB), err error) {
for _, c := range cs {
// show warning message the callback name already exists
if idx := getRIndex(names, c.name); idx > -1 && !c.replace && !c.remove && !cs[idx].remove {
c.processor.db.Logger.Warn(context.Background(), "duplicated callback `%v` from %v\n", c.name, utils.FileWithLineNum())
c.processor.db.Logger.Warn(context.Background(), "duplicated callback `%s` from %s\n", c.name, utils.FileWithLineNum())
}
names = append(names, c.name)
}
Expand All @@ -240,7 +266,7 @@ func sortCallbacks(cs []*callback) (fns []func(*DB), err error) {
// if before callback already sorted, append current callback just after it
sorted = append(sorted[:sortedIdx], append([]string{c.name}, sorted[sortedIdx:]...)...)
} else if curIdx > sortedIdx {
return fmt.Errorf("conflicting callback %v with before %v", c.name, c.before)
return fmt.Errorf("conflicting callback %s with before %s", c.name, c.before)
}
} else if idx := getRIndex(names, c.before); idx != -1 {
// if before callback exists
Expand All @@ -258,7 +284,7 @@ func sortCallbacks(cs []*callback) (fns []func(*DB), err error) {
// if after callback sorted, append current callback to last
sorted = append(sorted, c.name)
} else if curIdx < sortedIdx {
return fmt.Errorf("conflicting callback %v with before %v", c.name, c.after)
return fmt.Errorf("conflicting callback %s with before %s", c.name, c.after)
}
} else if idx := getRIndex(names, c.after); idx != -1 {
// if after callback exists but haven't sorted
Expand Down
Loading

0 comments on commit 8dad454

Please sign in to comment.