forked from mirror/gorm
feat: migrator support type aliases (#5627)
* feat: migrator support type aliases * perf: check type
This commit is contained in:
parent
101a7c789f
commit
73bc53f061
|
@ -68,6 +68,7 @@ type Migrator interface {
|
||||||
// Database
|
// Database
|
||||||
CurrentDatabase() string
|
CurrentDatabase() string
|
||||||
FullDataTypeOf(*schema.Field) clause.Expr
|
FullDataTypeOf(*schema.Field) clause.Expr
|
||||||
|
GetTypeAliases(databaseTypeName string) []string
|
||||||
|
|
||||||
// Tables
|
// Tables
|
||||||
CreateTable(dst ...interface{}) error
|
CreateTable(dst ...interface{}) error
|
||||||
|
|
|
@ -408,10 +408,28 @@ func (m Migrator) MigrateColumn(value interface{}, field *schema.Field, columnTy
|
||||||
|
|
||||||
alterColumn := false
|
alterColumn := false
|
||||||
|
|
||||||
|
if !field.PrimaryKey {
|
||||||
// check type
|
// check type
|
||||||
if !field.PrimaryKey && !strings.HasPrefix(fullDataType, realDataType) {
|
var isSameType bool
|
||||||
|
if strings.HasPrefix(fullDataType, realDataType) {
|
||||||
|
isSameType = true
|
||||||
|
}
|
||||||
|
|
||||||
|
// check type aliases
|
||||||
|
if !isSameType {
|
||||||
|
aliases := m.DB.Migrator().GetTypeAliases(realDataType)
|
||||||
|
for _, alias := range aliases {
|
||||||
|
if strings.HasPrefix(fullDataType, alias) {
|
||||||
|
isSameType = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !isSameType {
|
||||||
alterColumn = true
|
alterColumn = true
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// check size
|
// check size
|
||||||
if length, ok := columnType.Length(); length != int64(field.Size) {
|
if length, ok := columnType.Length(); length != int64(field.Size) {
|
||||||
|
@ -863,3 +881,8 @@ func (m Migrator) CurrentTable(stmt *gorm.Statement) interface{} {
|
||||||
func (m Migrator) GetIndexes(dst interface{}) ([]gorm.Index, error) {
|
func (m Migrator) GetIndexes(dst interface{}) ([]gorm.Index, error) {
|
||||||
return nil, errors.New("not support")
|
return nil, errors.New("not support")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetTypeAliases return database type aliases
|
||||||
|
func (m Migrator) GetTypeAliases(databaseTypeName string) []string {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue