Optimize migrator.go MigrateColumn and ColumnTypes func. (#4532)
This commit is contained in:
		
							parent
							
								
									7a49629fd1
								
							
						
					
					
						commit
						413fe587c6
					
				| @ -2,6 +2,7 @@ package migrator | |||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"context" | 	"context" | ||||||
|  | 	"database/sql" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"reflect" | 	"reflect" | ||||||
| 	"regexp" | 	"regexp" | ||||||
| @ -386,11 +387,11 @@ func (m Migrator) MigrateColumn(value interface{}, field *schema.Field, columnTy | |||||||
| 			alterColumn = true | 			alterColumn = true | ||||||
| 		} else { | 		} else { | ||||||
| 			// has size in data type and not equal
 | 			// has size in data type and not equal
 | ||||||
| 
 |  | ||||||
| 			// Since the following code is frequently called in the for loop, reg optimization is needed here
 | 			// Since the following code is frequently called in the for loop, reg optimization is needed here
 | ||||||
| 			matches := regRealDataType.FindAllStringSubmatch(realDataType, -1) | 			matches := regRealDataType.FindAllStringSubmatch(realDataType, -1) | ||||||
| 			matches2 := regFullDataType.FindAllStringSubmatch(fullDataType, -1) | 			matches2 := regFullDataType.FindAllStringSubmatch(fullDataType, -1) | ||||||
| 			if (len(matches) == 1 && matches[0][1] != fmt.Sprint(field.Size) || !field.PrimaryKey) && (len(matches2) == 1 && matches2[0][1] != fmt.Sprint(length)) { | 			if (len(matches) == 1 && matches[0][1] != fmt.Sprint(field.Size) || !field.PrimaryKey) && | ||||||
|  | 				(len(matches2) == 1 && matches2[0][1] != fmt.Sprint(length)) { | ||||||
| 				alterColumn = true | 				alterColumn = true | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| @ -418,22 +419,31 @@ func (m Migrator) MigrateColumn(value interface{}, field *schema.Field, columnTy | |||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (m Migrator) ColumnTypes(value interface{}) (columnTypes []gorm.ColumnType, err error) { | // ColumnTypes return columnTypes []gorm.ColumnType and execErr error
 | ||||||
| 	columnTypes = make([]gorm.ColumnType, 0) | func (m Migrator) ColumnTypes(value interface{}) ([]gorm.ColumnType, error) { | ||||||
| 	err = m.RunWithValue(value, func(stmt *gorm.Statement) error { | 	columnTypes := make([]gorm.ColumnType, 0) | ||||||
|  | 	execErr := m.RunWithValue(value, func(stmt *gorm.Statement) error { | ||||||
| 		rows, err := m.DB.Session(&gorm.Session{}).Table(stmt.Table).Limit(1).Rows() | 		rows, err := m.DB.Session(&gorm.Session{}).Table(stmt.Table).Limit(1).Rows() | ||||||
| 		if err == nil { | 		if err != nil { | ||||||
| 			defer rows.Close() | 			return err | ||||||
| 			rawColumnTypes, err := rows.ColumnTypes() |  | ||||||
| 			if err == nil { |  | ||||||
| 				for _, c := range rawColumnTypes { |  | ||||||
| 					columnTypes = append(columnTypes, c) |  | ||||||
| 				} |  | ||||||
| 			} |  | ||||||
| 		} | 		} | ||||||
| 		return err | 
 | ||||||
|  | 		defer rows.Close() | ||||||
|  | 
 | ||||||
|  | 		var rawColumnTypes []*sql.ColumnType | ||||||
|  | 		rawColumnTypes, err = rows.ColumnTypes() | ||||||
|  | 		if err != nil { | ||||||
|  | 			return err | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		for _, c := range rawColumnTypes { | ||||||
|  | 			columnTypes = append(columnTypes, c) | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		return nil | ||||||
| 	}) | 	}) | ||||||
| 	return | 
 | ||||||
|  | 	return columnTypes, execErr | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (m Migrator) CreateView(name string, option gorm.ViewOption) error { | func (m Migrator) CreateView(name string, option gorm.ViewOption) error { | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 heige
						heige