Skip to content

Commit 7892019

Browse files
authored
Fix panic bug in migrator due to lack of nil check for stmt.Schema (#6932)
1 parent ac59252 commit 7892019

File tree

1 file changed

+47
-19
lines changed

1 file changed

+47
-19
lines changed

migrator/migrator.go

+47-19
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ func (m Migrator) AutoMigrate(values ...interface{}) error {
127127
}
128128
} else {
129129
if err := m.RunWithValue(value, func(stmt *gorm.Statement) error {
130+
131+
if stmt.Schema == nil {
132+
return errors.New("failed to get schema")
133+
}
134+
130135
columnTypes, err := queryTx.Migrator().ColumnTypes(value)
131136
if err != nil {
132137
return err
@@ -211,6 +216,11 @@ func (m Migrator) CreateTable(values ...interface{}) error {
211216
for _, value := range m.ReorderModels(values, false) {
212217
tx := m.DB.Session(&gorm.Session{})
213218
if err := m.RunWithValue(value, func(stmt *gorm.Statement) (err error) {
219+
220+
if stmt.Schema == nil {
221+
return errors.New("failed to get schema")
222+
}
223+
214224
var (
215225
createTableSQL = "CREATE TABLE ? ("
216226
values = []interface{}{m.CurrentTable(stmt)}
@@ -363,6 +373,9 @@ func (m Migrator) RenameTable(oldName, newName interface{}) error {
363373
func (m Migrator) AddColumn(value interface{}, name string) error {
364374
return m.RunWithValue(value, func(stmt *gorm.Statement) error {
365375
// avoid using the same name field
376+
if stmt.Schema == nil {
377+
return errors.New("failed to get schema")
378+
}
366379
f := stmt.Schema.LookUpField(name)
367380
if f == nil {
368381
return fmt.Errorf("failed to look up field with name: %s", name)
@@ -382,8 +395,10 @@ func (m Migrator) AddColumn(value interface{}, name string) error {
382395
// DropColumn drop value's `name` column
383396
func (m Migrator) DropColumn(value interface{}, name string) error {
384397
return m.RunWithValue(value, func(stmt *gorm.Statement) error {
385-
if field := stmt.Schema.LookUpField(name); field != nil {
386-
name = field.DBName
398+
if stmt.Schema != nil {
399+
if field := stmt.Schema.LookUpField(name); field != nil {
400+
name = field.DBName
401+
}
387402
}
388403

389404
return m.DB.Exec(
@@ -395,13 +410,15 @@ func (m Migrator) DropColumn(value interface{}, name string) error {
395410
// AlterColumn alter value's `field` column' type based on schema definition
396411
func (m Migrator) AlterColumn(value interface{}, field string) error {
397412
return m.RunWithValue(value, func(stmt *gorm.Statement) error {
398-
if field := stmt.Schema.LookUpField(field); field != nil {
399-
fileType := m.FullDataTypeOf(field)
400-
return m.DB.Exec(
401-
"ALTER TABLE ? ALTER COLUMN ? TYPE ?",
402-
m.CurrentTable(stmt), clause.Column{Name: field.DBName}, fileType,
403-
).Error
413+
if stmt.Schema != nil {
414+
if field := stmt.Schema.LookUpField(field); field != nil {
415+
fileType := m.FullDataTypeOf(field)
416+
return m.DB.Exec(
417+
"ALTER TABLE ? ALTER COLUMN ? TYPE ?",
418+
m.CurrentTable(stmt), clause.Column{Name: field.DBName}, fileType,
419+
).Error
404420

421+
}
405422
}
406423
return fmt.Errorf("failed to look up field with name: %s", field)
407424
})
@@ -413,8 +430,10 @@ func (m Migrator) HasColumn(value interface{}, field string) bool {
413430
m.RunWithValue(value, func(stmt *gorm.Statement) error {
414431
currentDatabase := m.DB.Migrator().CurrentDatabase()
415432
name := field
416-
if field := stmt.Schema.LookUpField(field); field != nil {
417-
name = field.DBName
433+
if stmt.Schema != nil {
434+
if field := stmt.Schema.LookUpField(field); field != nil {
435+
name = field.DBName
436+
}
418437
}
419438

420439
return m.DB.Raw(
@@ -429,12 +448,14 @@ func (m Migrator) HasColumn(value interface{}, field string) bool {
429448
// RenameColumn rename value's field name from oldName to newName
430449
func (m Migrator) RenameColumn(value interface{}, oldName, newName string) error {
431450
return m.RunWithValue(value, func(stmt *gorm.Statement) error {
432-
if field := stmt.Schema.LookUpField(oldName); field != nil {
433-
oldName = field.DBName
434-
}
451+
if stmt.Schema != nil {
452+
if field := stmt.Schema.LookUpField(oldName); field != nil {
453+
oldName = field.DBName
454+
}
435455

436-
if field := stmt.Schema.LookUpField(newName); field != nil {
437-
newName = field.DBName
456+
if field := stmt.Schema.LookUpField(newName); field != nil {
457+
newName = field.DBName
458+
}
438459
}
439460

440461
return m.DB.Exec(
@@ -794,6 +815,9 @@ type BuildIndexOptionsInterface interface {
794815
// CreateIndex create index `name`
795816
func (m Migrator) CreateIndex(value interface{}, name string) error {
796817
return m.RunWithValue(value, func(stmt *gorm.Statement) error {
818+
if stmt.Schema == nil {
819+
return errors.New("failed to get schema")
820+
}
797821
if idx := stmt.Schema.LookIndex(name); idx != nil {
798822
opts := m.DB.Migrator().(BuildIndexOptionsInterface).BuildIndexOptions(idx.Fields, stmt)
799823
values := []interface{}{clause.Column{Name: idx.Name}, m.CurrentTable(stmt), opts}
@@ -826,8 +850,10 @@ func (m Migrator) CreateIndex(value interface{}, name string) error {
826850
// DropIndex drop index `name`
827851
func (m Migrator) DropIndex(value interface{}, name string) error {
828852
return m.RunWithValue(value, func(stmt *gorm.Statement) error {
829-
if idx := stmt.Schema.LookIndex(name); idx != nil {
830-
name = idx.Name
853+
if stmt.Schema != nil {
854+
if idx := stmt.Schema.LookIndex(name); idx != nil {
855+
name = idx.Name
856+
}
831857
}
832858

833859
return m.DB.Exec("DROP INDEX ? ON ?", clause.Column{Name: name}, m.CurrentTable(stmt)).Error
@@ -839,8 +865,10 @@ func (m Migrator) HasIndex(value interface{}, name string) bool {
839865
var count int64
840866
m.RunWithValue(value, func(stmt *gorm.Statement) error {
841867
currentDatabase := m.DB.Migrator().CurrentDatabase()
842-
if idx := stmt.Schema.LookIndex(name); idx != nil {
843-
name = idx.Name
868+
if stmt.Schema != nil {
869+
if idx := stmt.Schema.LookIndex(name); idx != nil {
870+
name = idx.Name
871+
}
844872
}
845873

846874
return m.DB.Raw(

0 commit comments

Comments
 (0)