Fix panic bug in migrator due to lack of nil check for stmt.Schema (#6932)
This commit is contained in:
		
							parent
							
								
									ac59252327
								
							
						
					
					
						commit
						78920199f0
					
				| @ -127,6 +127,11 @@ func (m Migrator) AutoMigrate(values ...interface{}) error { | |||||||
| 			} | 			} | ||||||
| 		} else { | 		} else { | ||||||
| 			if err := m.RunWithValue(value, func(stmt *gorm.Statement) error { | 			if err := m.RunWithValue(value, func(stmt *gorm.Statement) error { | ||||||
|  | 
 | ||||||
|  | 				if stmt.Schema == nil { | ||||||
|  | 					return errors.New("failed to get schema") | ||||||
|  | 				} | ||||||
|  | 
 | ||||||
| 				columnTypes, err := queryTx.Migrator().ColumnTypes(value) | 				columnTypes, err := queryTx.Migrator().ColumnTypes(value) | ||||||
| 				if err != nil { | 				if err != nil { | ||||||
| 					return err | 					return err | ||||||
| @ -211,6 +216,11 @@ func (m Migrator) CreateTable(values ...interface{}) error { | |||||||
| 	for _, value := range m.ReorderModels(values, false) { | 	for _, value := range m.ReorderModels(values, false) { | ||||||
| 		tx := m.DB.Session(&gorm.Session{}) | 		tx := m.DB.Session(&gorm.Session{}) | ||||||
| 		if err := m.RunWithValue(value, func(stmt *gorm.Statement) (err error) { | 		if err := m.RunWithValue(value, func(stmt *gorm.Statement) (err error) { | ||||||
|  | 
 | ||||||
|  | 			if stmt.Schema == nil { | ||||||
|  | 				return errors.New("failed to get schema") | ||||||
|  | 			} | ||||||
|  | 
 | ||||||
| 			var ( | 			var ( | ||||||
| 				createTableSQL          = "CREATE TABLE ? (" | 				createTableSQL          = "CREATE TABLE ? (" | ||||||
| 				values                  = []interface{}{m.CurrentTable(stmt)} | 				values                  = []interface{}{m.CurrentTable(stmt)} | ||||||
| @ -363,6 +373,9 @@ func (m Migrator) RenameTable(oldName, newName interface{}) error { | |||||||
| func (m Migrator) AddColumn(value interface{}, name string) error { | func (m Migrator) AddColumn(value interface{}, name string) error { | ||||||
| 	return m.RunWithValue(value, func(stmt *gorm.Statement) error { | 	return m.RunWithValue(value, func(stmt *gorm.Statement) error { | ||||||
| 		// avoid using the same name field
 | 		// avoid using the same name field
 | ||||||
|  | 		if stmt.Schema == nil { | ||||||
|  | 			return errors.New("failed to get schema") | ||||||
|  | 		} | ||||||
| 		f := stmt.Schema.LookUpField(name) | 		f := stmt.Schema.LookUpField(name) | ||||||
| 		if f == nil { | 		if f == nil { | ||||||
| 			return fmt.Errorf("failed to look up field with name: %s", name) | 			return fmt.Errorf("failed to look up field with name: %s", name) | ||||||
| @ -382,9 +395,11 @@ func (m Migrator) AddColumn(value interface{}, name string) error { | |||||||
| // DropColumn drop value's `name` column
 | // DropColumn drop value's `name` column
 | ||||||
| func (m Migrator) DropColumn(value interface{}, name string) error { | func (m Migrator) DropColumn(value interface{}, name string) error { | ||||||
| 	return m.RunWithValue(value, func(stmt *gorm.Statement) error { | 	return m.RunWithValue(value, func(stmt *gorm.Statement) error { | ||||||
|  | 		if stmt.Schema != nil { | ||||||
| 			if field := stmt.Schema.LookUpField(name); field != nil { | 			if field := stmt.Schema.LookUpField(name); field != nil { | ||||||
| 				name = field.DBName | 				name = field.DBName | ||||||
| 			} | 			} | ||||||
|  | 		} | ||||||
| 
 | 
 | ||||||
| 		return m.DB.Exec( | 		return m.DB.Exec( | ||||||
| 			"ALTER TABLE ? DROP COLUMN ?", m.CurrentTable(stmt), clause.Column{Name: name}, | 			"ALTER TABLE ? DROP COLUMN ?", m.CurrentTable(stmt), clause.Column{Name: name}, | ||||||
| @ -395,6 +410,7 @@ func (m Migrator) DropColumn(value interface{}, name string) error { | |||||||
| // AlterColumn alter value's `field` column' type based on schema definition
 | // AlterColumn alter value's `field` column' type based on schema definition
 | ||||||
| func (m Migrator) AlterColumn(value interface{}, field string) error { | func (m Migrator) AlterColumn(value interface{}, field string) error { | ||||||
| 	return m.RunWithValue(value, func(stmt *gorm.Statement) error { | 	return m.RunWithValue(value, func(stmt *gorm.Statement) error { | ||||||
|  | 		if stmt.Schema != nil { | ||||||
| 			if field := stmt.Schema.LookUpField(field); field != nil { | 			if field := stmt.Schema.LookUpField(field); field != nil { | ||||||
| 				fileType := m.FullDataTypeOf(field) | 				fileType := m.FullDataTypeOf(field) | ||||||
| 				return m.DB.Exec( | 				return m.DB.Exec( | ||||||
| @ -403,6 +419,7 @@ func (m Migrator) AlterColumn(value interface{}, field string) error { | |||||||
| 				).Error | 				).Error | ||||||
| 
 | 
 | ||||||
| 			} | 			} | ||||||
|  | 		} | ||||||
| 		return fmt.Errorf("failed to look up field with name: %s", field) | 		return fmt.Errorf("failed to look up field with name: %s", field) | ||||||
| 	}) | 	}) | ||||||
| } | } | ||||||
| @ -413,9 +430,11 @@ func (m Migrator) HasColumn(value interface{}, field string) bool { | |||||||
| 	m.RunWithValue(value, func(stmt *gorm.Statement) error { | 	m.RunWithValue(value, func(stmt *gorm.Statement) error { | ||||||
| 		currentDatabase := m.DB.Migrator().CurrentDatabase() | 		currentDatabase := m.DB.Migrator().CurrentDatabase() | ||||||
| 		name := field | 		name := field | ||||||
|  | 		if stmt.Schema != nil { | ||||||
| 			if field := stmt.Schema.LookUpField(field); field != nil { | 			if field := stmt.Schema.LookUpField(field); field != nil { | ||||||
| 				name = field.DBName | 				name = field.DBName | ||||||
| 			} | 			} | ||||||
|  | 		} | ||||||
| 
 | 
 | ||||||
| 		return m.DB.Raw( | 		return m.DB.Raw( | ||||||
| 			"SELECT count(*) FROM INFORMATION_SCHEMA.columns WHERE table_schema = ? AND table_name = ? AND column_name = ?", | 			"SELECT count(*) FROM INFORMATION_SCHEMA.columns WHERE table_schema = ? AND table_name = ? AND column_name = ?", | ||||||
| @ -429,6 +448,7 @@ func (m Migrator) HasColumn(value interface{}, field string) bool { | |||||||
| // RenameColumn rename value's field name from oldName to newName
 | // RenameColumn rename value's field name from oldName to newName
 | ||||||
| func (m Migrator) RenameColumn(value interface{}, oldName, newName string) error { | func (m Migrator) RenameColumn(value interface{}, oldName, newName string) error { | ||||||
| 	return m.RunWithValue(value, func(stmt *gorm.Statement) error { | 	return m.RunWithValue(value, func(stmt *gorm.Statement) error { | ||||||
|  | 		if stmt.Schema != nil { | ||||||
| 			if field := stmt.Schema.LookUpField(oldName); field != nil { | 			if field := stmt.Schema.LookUpField(oldName); field != nil { | ||||||
| 				oldName = field.DBName | 				oldName = field.DBName | ||||||
| 			} | 			} | ||||||
| @ -436,6 +456,7 @@ func (m Migrator) RenameColumn(value interface{}, oldName, newName string) error | |||||||
| 			if field := stmt.Schema.LookUpField(newName); field != nil { | 			if field := stmt.Schema.LookUpField(newName); field != nil { | ||||||
| 				newName = field.DBName | 				newName = field.DBName | ||||||
| 			} | 			} | ||||||
|  | 		} | ||||||
| 
 | 
 | ||||||
| 		return m.DB.Exec( | 		return m.DB.Exec( | ||||||
| 			"ALTER TABLE ? RENAME COLUMN ? TO ?", | 			"ALTER TABLE ? RENAME COLUMN ? TO ?", | ||||||
| @ -794,6 +815,9 @@ type BuildIndexOptionsInterface interface { | |||||||
| // CreateIndex create index `name`
 | // CreateIndex create index `name`
 | ||||||
| func (m Migrator) CreateIndex(value interface{}, name string) error { | func (m Migrator) CreateIndex(value interface{}, name string) error { | ||||||
| 	return m.RunWithValue(value, func(stmt *gorm.Statement) error { | 	return m.RunWithValue(value, func(stmt *gorm.Statement) error { | ||||||
|  | 		if stmt.Schema == nil { | ||||||
|  | 			return errors.New("failed to get schema") | ||||||
|  | 		} | ||||||
| 		if idx := stmt.Schema.LookIndex(name); idx != nil { | 		if idx := stmt.Schema.LookIndex(name); idx != nil { | ||||||
| 			opts := m.DB.Migrator().(BuildIndexOptionsInterface).BuildIndexOptions(idx.Fields, stmt) | 			opts := m.DB.Migrator().(BuildIndexOptionsInterface).BuildIndexOptions(idx.Fields, stmt) | ||||||
| 			values := []interface{}{clause.Column{Name: idx.Name}, m.CurrentTable(stmt), opts} | 			values := []interface{}{clause.Column{Name: idx.Name}, m.CurrentTable(stmt), opts} | ||||||
| @ -826,9 +850,11 @@ func (m Migrator) CreateIndex(value interface{}, name string) error { | |||||||
| // DropIndex drop index `name`
 | // DropIndex drop index `name`
 | ||||||
| func (m Migrator) DropIndex(value interface{}, name string) error { | func (m Migrator) DropIndex(value interface{}, name string) error { | ||||||
| 	return m.RunWithValue(value, func(stmt *gorm.Statement) error { | 	return m.RunWithValue(value, func(stmt *gorm.Statement) error { | ||||||
|  | 		if stmt.Schema != nil { | ||||||
| 			if idx := stmt.Schema.LookIndex(name); idx != nil { | 			if idx := stmt.Schema.LookIndex(name); idx != nil { | ||||||
| 				name = idx.Name | 				name = idx.Name | ||||||
| 			} | 			} | ||||||
|  | 		} | ||||||
| 
 | 
 | ||||||
| 		return m.DB.Exec("DROP INDEX ? ON ?", clause.Column{Name: name}, m.CurrentTable(stmt)).Error | 		return m.DB.Exec("DROP INDEX ? ON ?", clause.Column{Name: name}, m.CurrentTable(stmt)).Error | ||||||
| 	}) | 	}) | ||||||
| @ -839,9 +865,11 @@ func (m Migrator) HasIndex(value interface{}, name string) bool { | |||||||
| 	var count int64 | 	var count int64 | ||||||
| 	m.RunWithValue(value, func(stmt *gorm.Statement) error { | 	m.RunWithValue(value, func(stmt *gorm.Statement) error { | ||||||
| 		currentDatabase := m.DB.Migrator().CurrentDatabase() | 		currentDatabase := m.DB.Migrator().CurrentDatabase() | ||||||
|  | 		if stmt.Schema != nil { | ||||||
| 			if idx := stmt.Schema.LookIndex(name); idx != nil { | 			if idx := stmt.Schema.LookIndex(name); idx != nil { | ||||||
| 				name = idx.Name | 				name = idx.Name | ||||||
| 			} | 			} | ||||||
|  | 		} | ||||||
| 
 | 
 | ||||||
| 		return m.DB.Raw( | 		return m.DB.Raw( | ||||||
| 			"SELECT count(*) FROM information_schema.statistics WHERE table_schema = ? AND table_name = ? AND index_name = ?", | 			"SELECT count(*) FROM information_schema.statistics WHERE table_schema = ? AND table_name = ? AND index_name = ?", | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 PiexlMax(奇淼
						PiexlMax(奇淼