Skip to content

Commit e0824aa

Browse files
author
wenyazhou.13
committed
fix: skip AfterInitialize
1 parent d7064f1 commit e0824aa

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

gorm.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,20 @@ func Open(dialector Dialector, opts ...Option) (db *DB, err error) {
130130
return isConfig && !isConfig2
131131
})
132132

133+
var skipAfterInitialize bool
133134
for _, opt := range opts {
134135
if opt != nil {
135136
if applyErr := opt.Apply(config); applyErr != nil {
136137
return nil, applyErr
137138
}
138-
//defer func(opt Option) {
139-
// if errr := opt.AfterInitialize(db); errr != nil {
140-
// err = errr
141-
// }
142-
//}(opt)
139+
defer func(opt Option) {
140+
if skipAfterInitialize {
141+
return
142+
}
143+
if errr := opt.AfterInitialize(db); errr != nil {
144+
err = errr
145+
}
146+
}(opt)
143147
}
144148
}
145149

@@ -187,6 +191,9 @@ func Open(dialector Dialector, opts ...Option) (db *DB, err error) {
187191
if db, _ := db.DB(); db != nil {
188192
_ = db.Close()
189193
}
194+
195+
// DB is not initialized, so we skip AfterInitialize
196+
skipAfterInitialize = true
190197
return
191198
}
192199

0 commit comments

Comments
 (0)