Skip to content

Commit 8dad454

Browse files
authored
Merge pull request #3 from itering/depend/bump-v1.21.15
Bump gorm to v1.21.15
2 parents 8771f3c + e62a53b commit 8dad454

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+2014
-837
lines changed

.github/dependabot.yml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
version: 2
3+
updates:
4+
- package-ecosystem: gomod
5+
directory: /
6+
schedule:
7+
interval: weekly
8+
- package-ecosystem: github-actions
9+
directory: /
10+
schedule:
11+
interval: weekly
12+
- package-ecosystem: gomod
13+
directory: /tests
14+
schedule:
15+
interval: weekly

.github/workflows/invalid_question.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
ACTIONS_STEP_DEBUG: true
1111
steps:
1212
- name: Close Stale Issues
13-
uses: actions/stale@v3.0.7
13+
uses: actions/stale@v4
1414
with:
1515
repo-token: ${{ secrets.GITHUB_TOKEN }}
1616
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) ✨"

.github/workflows/missing_playground.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
ACTIONS_STEP_DEBUG: true
1111
steps:
1212
- name: Close Stale Issues
13-
uses: actions/stale@v3.0.7
13+
uses: actions/stale@v4
1414
with:
1515
repo-token: ${{ secrets.GITHUB_TOKEN }}
1616
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) ✨"

.github/workflows/reviewdog.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ jobs:
88
- name: Check out code into the Go module directory
99
uses: actions/checkout@v1
1010
- name: golangci-lint
11-
uses: reviewdog/action-golangci-lint@v1
11+
uses: reviewdog/action-golangci-lint@v2

.github/workflows/stale.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
ACTIONS_STEP_DEBUG: true
1111
steps:
1212
- name: Close Stale Issues
13-
uses: actions/stale@v3.0.7
13+
uses: actions/stale@v4
1414
with:
1515
repo-token: ${{ secrets.GITHUB_TOKEN }}
1616
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"

.github/workflows/tests.yml

+8-33
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ jobs:
1313
sqlite:
1414
strategy:
1515
matrix:
16-
go: ['1.15', '1.14', '1.13']
17-
platform: [ubuntu-latest, macos-latest] # can not run in windows OS
16+
go: ['1.17', '1.16', '1.15']
17+
platform: [ubuntu-latest] # can not run in windows OS
1818
runs-on: ${{ matrix.platform }}
1919

2020
steps:
@@ -35,36 +35,11 @@ jobs:
3535
- name: Tests
3636
run: GORM_DIALECT=sqlite ./tests/tests_all.sh
3737

38-
sqlite_windows:
39-
strategy:
40-
matrix:
41-
go: ['1.15', '1.14', '1.13']
42-
platform: [windows-latest]
43-
runs-on: ${{ matrix.platform }}
44-
45-
steps:
46-
- name: Set up Go 1.x
47-
uses: actions/setup-go@v2
48-
with:
49-
go-version: ${{ matrix.go }}
50-
51-
- name: Check out code into the Go module directory
52-
uses: actions/checkout@v2
53-
54-
- name: go mod package cache
55-
uses: actions/cache@v2
56-
with:
57-
path: ~/go/pkg/mod
58-
key: ${{ runner.os }}-go-${{ matrix.go }}-${{ hashFiles('tests/go.mod') }}
59-
60-
- name: Tests
61-
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
62-
6338
mysql:
6439
strategy:
6540
matrix:
66-
dbversion: ['mysql:latest', 'mysql:5.7', 'mysql:5.6', 'mariadb:latest']
67-
go: ['1.15', '1.14', '1.13']
41+
dbversion: ['mysql:latest', 'mysql:5.7', 'mariadb:latest']
42+
go: ['1.17', '1.16', '1.15']
6843
platform: [ubuntu-latest]
6944
runs-on: ${{ matrix.platform }}
7045

@@ -107,9 +82,9 @@ jobs:
10782
postgres:
10883
strategy:
10984
matrix:
110-
dbversion: ['postgres:latest', 'postgres:11', 'postgres:10']
111-
go: ['1.15', '1.14', '1.13']
112-
platform: [ubuntu-latest] # can not run in macOS and widnowsOS
85+
dbversion: ['postgres:latest', 'postgres:12', 'postgres:11', 'postgres:10']
86+
go: ['1.17', '1.16', '1.15']
87+
platform: [ubuntu-latest] # can not run in macOS and Windows
11388
runs-on: ${{ matrix.platform }}
11489

11590
services:
@@ -150,7 +125,7 @@ jobs:
150125
sqlserver:
151126
strategy:
152127
matrix:
153-
go: ['1.15', '1.14', '1.13']
128+
go: ['1.17', '1.16', '1.15']
154129
platform: [ubuntu-latest] # can not run test in macOS and windows
155130
runs-on: ${{ matrix.platform }}
156131

association.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package gorm
22

33
import (
4-
"errors"
54
"fmt"
65
"reflect"
76
"strings"
@@ -27,7 +26,7 @@ func (db *DB) Association(column string) *Association {
2726
association.Relationship = db.Statement.Schema.Relationships.Relations[column]
2827

2928
if association.Relationship == nil {
30-
association.Error = fmt.Errorf("%w: %v", ErrUnsupportedRelation, column)
29+
association.Error = fmt.Errorf("%w: %s", ErrUnsupportedRelation, column)
3130
}
3231

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

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

444-
association.Error = errors.New("invalid association values, length doesn't match")
443+
association.Error = ErrInvalidValueOfLength
445444
return
446445
}
447446

callbacks.go

+39-13
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type callbacks struct {
3232

3333
type processor struct {
3434
db *DB
35+
Clauses []string
3536
fns []func(*DB)
3637
callbacks []*callback
3738
}
@@ -71,38 +72,57 @@ func (cs *callbacks) Raw() *processor {
7172
return cs.processors["raw"]
7273
}
7374

74-
func (p *processor) Execute(db *DB) {
75-
curTime := time.Now()
76-
stmt := db.Statement
75+
func (p *processor) Execute(db *DB) *DB {
76+
// call scopes
77+
for len(db.Statement.scopes) > 0 {
78+
scopes := db.Statement.scopes
79+
db.Statement.scopes = nil
80+
for _, scope := range scopes {
81+
db = scope(db)
82+
}
83+
}
84+
85+
var (
86+
curTime = time.Now()
87+
stmt = db.Statement
88+
resetBuildClauses bool
89+
)
7790

91+
if len(stmt.BuildClauses) == 0 {
92+
stmt.BuildClauses = p.Clauses
93+
resetBuildClauses = true
94+
}
95+
96+
// assign model values
7897
if stmt.Model == nil {
7998
stmt.Model = stmt.Dest
8099
} else if stmt.Dest == nil {
81100
stmt.Dest = stmt.Model
82101
}
83102

103+
// parse model values
84104
if stmt.Model != nil {
85-
if err := stmt.Parse(stmt.Model); err != nil && (!errors.Is(err, schema.ErrUnsupportedDataType) || (stmt.Table == "" && stmt.SQL.Len() == 0)) {
86-
if errors.Is(err, schema.ErrUnsupportedDataType) && stmt.Table == "" {
105+
if err := stmt.Parse(stmt.Model); err != nil && (!errors.Is(err, schema.ErrUnsupportedDataType) || (stmt.Table == "" && stmt.TableExpr == nil && stmt.SQL.Len() == 0)) {
106+
if errors.Is(err, schema.ErrUnsupportedDataType) && stmt.Table == "" && stmt.TableExpr == nil {
87107
db.AddError(fmt.Errorf("%w: Table not set, please set it like: db.Model(&user) or db.Table(\"users\")", err))
88108
} else {
89109
db.AddError(err)
90110
}
91111
}
92112
}
93113

114+
// assign stmt.ReflectValue
94115
if stmt.Dest != nil {
95116
stmt.ReflectValue = reflect.ValueOf(stmt.Dest)
96117
for stmt.ReflectValue.Kind() == reflect.Ptr {
97-
if stmt.ReflectValue.IsNil() {
118+
if stmt.ReflectValue.IsNil() && stmt.ReflectValue.CanAddr() {
98119
stmt.ReflectValue.Set(reflect.New(stmt.ReflectValue.Type().Elem()))
99-
break
100120
}
101121

102122
stmt.ReflectValue = stmt.ReflectValue.Elem()
103123
}
104124
if !stmt.ReflectValue.IsValid() {
105-
db.AddError(fmt.Errorf("invalid value"))
125+
db.AddError(ErrInvalidValue)
106126
}
107127
}
108128

@@ -118,6 +138,12 @@ func (p *processor) Execute(db *DB) {
118138
stmt.SQL.Reset()
119139
stmt.Vars = nil
120140
}
141+
142+
if resetBuildClauses {
143+
stmt.BuildClauses = nil
144+
}
145+
146+
return db
121147
}
122148

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

188214
func (c *callback) Remove(name string) error {
189-
c.processor.db.Logger.Warn(context.Background(), "removing callback `%v` from %v\n", name, utils.FileWithLineNum())
215+
c.processor.db.Logger.Warn(context.Background(), "removing callback `%s` from %s\n", name, utils.FileWithLineNum())
190216
c.name = name
191217
c.remove = true
192218
c.processor.callbacks = append(c.processor.callbacks, c)
193219
return c.processor.compile()
194220
}
195221

196222
func (c *callback) Replace(name string, fn func(*DB)) error {
197-
c.processor.db.Logger.Info(context.Background(), "replacing callback `%v` from %v\n", name, utils.FileWithLineNum())
223+
c.processor.db.Logger.Info(context.Background(), "replacing callback `%s` from %s\n", name, utils.FileWithLineNum())
198224
c.name = name
199225
c.handler = fn
200226
c.replace = true
@@ -224,7 +250,7 @@ func sortCallbacks(cs []*callback) (fns []func(*DB), err error) {
224250
for _, c := range cs {
225251
// show warning message the callback name already exists
226252
if idx := getRIndex(names, c.name); idx > -1 && !c.replace && !c.remove && !cs[idx].remove {
227-
c.processor.db.Logger.Warn(context.Background(), "duplicated callback `%v` from %v\n", c.name, utils.FileWithLineNum())
253+
c.processor.db.Logger.Warn(context.Background(), "duplicated callback `%s` from %s\n", c.name, utils.FileWithLineNum())
228254
}
229255
names = append(names, c.name)
230256
}
@@ -240,7 +266,7 @@ func sortCallbacks(cs []*callback) (fns []func(*DB), err error) {
240266
// if before callback already sorted, append current callback just after it
241267
sorted = append(sorted[:sortedIdx], append([]string{c.name}, sorted[sortedIdx:]...)...)
242268
} else if curIdx > sortedIdx {
243-
return fmt.Errorf("conflicting callback %v with before %v", c.name, c.before)
269+
return fmt.Errorf("conflicting callback %s with before %s", c.name, c.before)
244270
}
245271
} else if idx := getRIndex(names, c.before); idx != -1 {
246272
// if before callback exists
@@ -258,7 +284,7 @@ func sortCallbacks(cs []*callback) (fns []func(*DB), err error) {
258284
// if after callback sorted, append current callback to last
259285
sorted = append(sorted, c.name)
260286
} else if curIdx < sortedIdx {
261-
return fmt.Errorf("conflicting callback %v with before %v", c.name, c.after)
287+
return fmt.Errorf("conflicting callback %s with before %s", c.name, c.after)
262288
}
263289
} else if idx := getRIndex(names, c.after); idx != -1 {
264290
// if after callback exists but haven't sorted

0 commit comments

Comments
 (0)