optimize migrator.go MigrateColumn and ColumnTypes func.

This commit is contained in:
daheige 2021-07-14 22:23:35 +08:00
parent 7a49629fd1
commit f9c349d1a2

View File

@ -2,9 +2,11 @@ package migrator
import (
"context"
"database/sql"
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
"gorm.io/gorm"
@ -390,7 +392,8 @@ func (m Migrator) MigrateColumn(value interface{}, field *schema.Field, columnTy
// Since the following code is frequently called in the for loop, reg optimization is needed here
matches := regRealDataType.FindAllStringSubmatch(realDataType, -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] != strconv.Itoa(field.Size) || !field.PrimaryKey) &&
(len(matches2) == 1 && matches2[0][1] != strconv.FormatInt(length, 10)) {
alterColumn = true
}
}
@ -418,22 +421,31 @@ func (m Migrator) MigrateColumn(value interface{}, field *schema.Field, columnTy
return nil
}
func (m Migrator) ColumnTypes(value interface{}) (columnTypes []gorm.ColumnType, err error) {
columnTypes = make([]gorm.ColumnType, 0)
err = m.RunWithValue(value, func(stmt *gorm.Statement) error {
// ColumnTypes return columnTypes []gorm.ColumnType and execErr error
func (m Migrator) ColumnTypes(value interface{}) ([]gorm.ColumnType, 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()
if err == nil {
if err != nil {
return err
}
defer rows.Close()
rawColumnTypes, err := rows.ColumnTypes()
if err == nil {
var rawColumnTypes []*sql.ColumnType
rawColumnTypes, err = rows.ColumnTypes()
if err != nil {
return err
}
for _, c := range rawColumnTypes {
columnTypes = append(columnTypes, c)
}
}
}
return err
return nil
})
return
return columnTypes, execErr
}
func (m Migrator) CreateView(name string, option gorm.ViewOption) error {