From 1c966e0d25a6cdedbb7d599eae0cd060a0572ddf Mon Sep 17 00:00:00 2001 From: Name <1911860538@qq.com> Date: Wed, 21 May 2025 10:35:23 +0800 Subject: [PATCH 1/5] perf: use strings.Cut to replace strings.SplitN (#7455) Co-authored-by: 1911860538 --- callbacks/preload.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/callbacks/preload.go b/callbacks/preload.go index fd8214bb..a84065ff 100644 --- a/callbacks/preload.go +++ b/callbacks/preload.go @@ -103,11 +103,11 @@ func preloadEntryPoint(db *gorm.DB, joins []string, relationships *schema.Relati joined = true continue } - joinNames := strings.SplitN(join, ".", 2) - if len(joinNames) == 2 { - if _, ok := relationships.Relations[joinNames[0]]; ok && name == joinNames[0] { + join0, join1, cut := strings.Cut(join, ".") + if cut { + if _, ok := relationships.Relations[join0]; ok && name == join0 { joined = true - nestedJoins = append(nestedJoins, joinNames[1]) + nestedJoins = append(nestedJoins, join1) } } } From 9703eb775f203e916759bfa51af6724b05ff04ba Mon Sep 17 00:00:00 2001 From: Name <1911860538@qq.com> Date: Wed, 21 May 2025 10:35:56 +0800 Subject: [PATCH 2/5] perf: use strings.IndexByte to replace strings.Index (#7454) Co-authored-by: 1911860538 --- schema/index.go | 2 +- schema/relationship.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/schema/index.go b/schema/index.go index a1cdc639..2690a0cb 100644 --- a/schema/index.go +++ b/schema/index.go @@ -105,7 +105,7 @@ func parseFieldIndexes(field *Field) (indexes []Index, err error) { var ( name string tag = strings.Join(v[1:], ":") - idx = strings.Index(tag, ",") + idx = strings.IndexByte(tag, ',') tagSetting = strings.Join(strings.Split(tag, ",")[1:], ",") settings = ParseTagSetting(tagSetting, ",") length, _ = strconv.Atoi(settings["LENGTH"]) diff --git a/schema/relationship.go b/schema/relationship.go index def4a595..b600f040 100644 --- a/schema/relationship.go +++ b/schema/relationship.go @@ -675,7 +675,7 @@ func (rel *Relationship) ParseConstraint() *Constraint { var ( name string - idx = strings.Index(str, ",") + idx = strings.IndexByte(str, ',') settings = ParseTagSetting(str, ",") ) From 12043304197c54237d430825e90adc50d63ef383 Mon Sep 17 00:00:00 2001 From: pipipipipip <43127989+pi12138@users.noreply.github.com> Date: Wed, 21 May 2025 11:13:31 +0800 Subject: [PATCH 3/5] feat: error message show field name (#7452) * feat: error message show field name * feat: failed to parse field * feat: failed to parse field --------- Co-authored-by: zyp --- schema/relationship.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/schema/relationship.go b/schema/relationship.go index b600f040..ea2eed62 100644 --- a/schema/relationship.go +++ b/schema/relationship.go @@ -78,7 +78,7 @@ func (schema *Schema) parseRelation(field *Field) *Relationship { cacheStore := schema.cacheStore if relation.FieldSchema, err = getOrParse(fieldValue, cacheStore, schema.namer); err != nil { - schema.err = err + schema.err = fmt.Errorf("failed to parse field: %s, error: %w", field.Name, err) return nil } From e3037e4ef0394faf118cf926fd03283995d6da1f Mon Sep 17 00:00:00 2001 From: Name <1911860538@qq.com> Date: Thu, 22 May 2025 10:49:19 +0800 Subject: [PATCH 4/5] perf: break early on match failure in ParseConstraint (#7402) Co-authored-by: 1911860538 --- schema/relationship.go | 1 + 1 file changed, 1 insertion(+) diff --git a/schema/relationship.go b/schema/relationship.go index ea2eed62..c60ff6d9 100644 --- a/schema/relationship.go +++ b/schema/relationship.go @@ -663,6 +663,7 @@ func (rel *Relationship) ParseConstraint() *Constraint { if !(rel.References[idx].PrimaryKey == ref.PrimaryKey && rel.References[idx].ForeignKey == ref.ForeignKey && rel.References[idx].PrimaryValue == ref.PrimaryValue) { matched = false + break } } From 8e7ab46c1b865386bbb8f5322d64880ee5ce6f40 Mon Sep 17 00:00:00 2001 From: codingplz <449149812@qq.com> Date: Thu, 22 May 2025 10:53:47 +0800 Subject: [PATCH 5/5] fix: return init dialector error (#7379) * fix: return init dialector error * mock defer * fix: skip AfterInitialize --------- Co-authored-by: wenyazhou.13 --- gorm.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/gorm.go b/gorm.go index 63a28b37..8b3cec69 100644 --- a/gorm.go +++ b/gorm.go @@ -135,12 +135,16 @@ func Open(dialector Dialector, opts ...Option) (db *DB, err error) { return isConfig && !isConfig2 }) + var skipAfterInitialize bool for _, opt := range opts { if opt != nil { if applyErr := opt.Apply(config); applyErr != nil { return nil, applyErr } defer func(opt Option) { + if skipAfterInitialize { + return + } if errr := opt.AfterInitialize(db); errr != nil { err = errr } @@ -192,6 +196,10 @@ func Open(dialector Dialector, opts ...Option) (db *DB, err error) { if db, _ := db.DB(); db != nil { _ = db.Close() } + + // DB is not initialized, so we skip AfterInitialize + skipAfterInitialize = true + return } if config.TranslateError {