Skip to content

Commit 0cb2450

Browse files
authored
Merge pull request #1440 from go-gorm/fix/issue-1115-unique-aware
feat: skip batch insert when unique index exists
2 parents 0780b46 + ad4c750 commit 0cb2450

File tree

8 files changed

+51
-9
lines changed

8 files changed

+51
-9
lines changed

config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ type Config struct {
4848

4949
Mode GenerateMode // generate mode
5050

51+
UnitTestTemplate string
52+
5153
queryPkgName string // generated query code's package name
5254
modelPkgPath string // model pkg path in target project
5355
dbNameOpts []model.SchemaNameOpt

generator.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,15 @@ func (g *Generator) generateQueryUnitTestFile(data *genInfo) (err error) {
475475
return err
476476
}
477477

478-
err = render(tmpl.CRUDMethodTest, &buf, data.QueryStructMeta)
478+
crudTmpl := tmpl.CRUDMethodTest
479+
if g.UnitTestTemplate != "" {
480+
content, readErr := os.ReadFile(g.UnitTestTemplate)
481+
if readErr != nil {
482+
return readErr
483+
}
484+
crudTmpl = string(content)
485+
}
486+
err = render(crudTmpl, &buf, data.QueryStructMeta)
479487
if err != nil {
480488
return err
481489
}

internal/generate/query.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,18 @@ func (b *QueryStructMeta) appendOrUpdateField(f *model.Field) {
137137

138138
func (b *QueryStructMeta) appendField(f *model.Field) { b.Fields = append(b.Fields, f) }
139139

140+
func (b *QueryStructMeta) HasUniqueIndex() bool {
141+
for _, f := range b.Fields {
142+
if f == nil {
143+
continue
144+
}
145+
if len(f.GORMTag[field.TagKeyGormUniqueIndex]) > 0 {
146+
return true
147+
}
148+
}
149+
return false
150+
}
151+
140152
// HasField check if BaseStruct has fields
141153
func (b *QueryStructMeta) HasField() bool { return len(b.Fields) > 0 }
142154

internal/template/method.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,11 +308,12 @@ func Test_{{.QueryStructName}}Query(t *testing.T) {
308308
t.Error("create item in table <{{.TableName}}> fail:", err)
309309
}
310310
311+
{{ if not .HasUniqueIndex }}
311312
err = _do.CreateInBatches([]*{{.StructInfo.Package}}.{{.ModelStructName}}{ {}, {} }, 10)
312313
if err != nil {
313314
t.Error("create item in table <{{.TableName}}> fail:", err)
314315
}
315-
316+
{{ end }}
316317
_, err = _do.Select({{.QueryStructName}}.ALL).Take()
317318
if err != nil {
318319
t.Error("Take() on table <{{.TableName}}> fail:", err)

tools/gentool/README.ZH_CN.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
only generate models (without query file)
4444
-withUnitTest
4545
generate unit test for query code
46+
-unitTestTemplate string
47+
custom unit test template file path for query code
4648
-fieldSignable
4749
detect integer field's unsigned type, adjust generated data type
4850

@@ -126,6 +128,10 @@ eg :
126128

127129
生成单元测试。
128130

131+
#### unitTestTemplate
132+
133+
自定义 query 单测模板文件路径
134+
129135
#### fieldSignable
130136

131137
Value : False / True

tools/gentool/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ Install GEN as a binary tool
4141
only generate models (without query file)
4242
-withUnitTest
4343
generate unit test for query code
44+
-unitTestTemplate string
45+
custom unit test template file path for query code
4446
-fieldSignable
4547
detect integer field's unsigned type, adjust generated data type
4648

@@ -120,6 +122,10 @@ Value : False / True
120122

121123
Generate unit test.
122124

125+
#### unitTestTemplate
126+
127+
custom unit test template file path for query code
128+
123129
#### fieldSignable
124130

125131
Value : False / True

tools/gentool/gen.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ database:
1818
outFile : ""
1919
# generate unit test for query code
2020
withUnitTest : false
21+
unitTestTemplate : ""
2122
# generated model code's package name
2223
modelPkgName : ""
2324
# generate with pointer when field is nullable

tools/gentool/gentool.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@ const (
3434

3535
// CmdParams is command line parameters
3636
type CmdParams struct {
37-
DSN string `yaml:"dsn"` // consult[https://gorm.io/docs/connecting_to_the_database.html]"
38-
DB string `yaml:"db"` // input mysql or postgres or sqlite or sqlserver. consult[https://gorm.io/docs/connecting_to_the_database.html]
39-
Tables []string `yaml:"tables"` // enter the required data table or leave it blank
40-
OnlyModel bool `yaml:"onlyModel"` // only generate model
41-
OutPath string `yaml:"outPath"` // specify a directory for output
42-
OutFile string `yaml:"outFile"` // query code file name, default: gen.go
43-
WithUnitTest bool `yaml:"withUnitTest"` // generate unit test for query code
37+
DSN string `yaml:"dsn"` // consult[https://gorm.io/docs/connecting_to_the_database.html]"
38+
DB string `yaml:"db"` // input mysql or postgres or sqlite or sqlserver. consult[https://gorm.io/docs/connecting_to_the_database.html]
39+
Tables []string `yaml:"tables"` // enter the required data table or leave it blank
40+
OnlyModel bool `yaml:"onlyModel"` // only generate model
41+
OutPath string `yaml:"outPath"` // specify a directory for output
42+
OutFile string `yaml:"outFile"` // query code file name, default: gen.go
43+
WithUnitTest bool `yaml:"withUnitTest"` // generate unit test for query code
44+
UnitTestTemplate string `yaml:"unitTestTemplate"`
4445
ModelPkgName string `yaml:"modelPkgName"` // generated model code's package name
4546
FieldNullable bool `yaml:"fieldNullable"` // generate with pointer when field is nullable
4647
FieldCoverable bool `yaml:"fieldCoverable"` // generate with pointer when field has default value
@@ -149,6 +150,7 @@ func argParse() *CmdParams {
149150
outPath := flag.String("outPath", defaultQueryPath, "specify a directory for output")
150151
outFile := flag.String("outFile", "", "query code file name, default: gen.go")
151152
withUnitTest := flag.Bool("withUnitTest", false, "generate unit test for query code")
153+
unitTestTemplate := flag.String("unitTestTemplate", "", "custom unit test template file path for query code")
152154
modelPkgName := flag.String("modelPkgName", "", "generated model code's package name")
153155
fieldNullable := flag.Bool("fieldNullable", false, "generate with pointer when field is nullable")
154156
fieldCoverable := flag.Bool("fieldCoverable", false, "generate with pointer when field has default value")
@@ -185,6 +187,9 @@ func argParse() *CmdParams {
185187
if *withUnitTest {
186188
cmdParse.WithUnitTest = *withUnitTest
187189
}
190+
if *unitTestTemplate != "" {
191+
cmdParse.UnitTestTemplate = *unitTestTemplate
192+
}
188193
if *modelPkgName != "" {
189194
cmdParse.ModelPkgName = *modelPkgName
190195
}
@@ -226,6 +231,7 @@ func main() {
226231
OutFile: config.OutFile,
227232
ModelPkgPath: config.ModelPkgName,
228233
WithUnitTest: config.WithUnitTest,
234+
UnitTestTemplate: config.UnitTestTemplate,
229235
FieldNullable: config.FieldNullable,
230236
FieldCoverable: config.FieldCoverable,
231237
FieldWithIndexTag: config.FieldWithIndexTag,

0 commit comments

Comments
 (0)