feat: migrator support type aliases (#5627)

* feat: migrator support type aliases

* perf: check type
This commit is contained in:
Cr 2022-09-22 15:56:32 +08:00 committed by GitHub
parent 101a7c789f
commit 73bc53f061
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 3 deletions

View File

@ -68,6 +68,7 @@ type Migrator interface {
// Database
CurrentDatabase() string
FullDataTypeOf(*schema.Field) clause.Expr
GetTypeAliases(databaseTypeName string) []string
// Tables
CreateTable(dst ...interface{}) error

View File

@ -408,9 +408,27 @@ func (m Migrator) MigrateColumn(value interface{}, field *schema.Field, columnTy
alterColumn := false
// check type
if !field.PrimaryKey && !strings.HasPrefix(fullDataType, realDataType) {
alterColumn = true
if !field.PrimaryKey {
// check type
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
}
}
// check size
@ -863,3 +881,8 @@ func (m Migrator) CurrentTable(stmt *gorm.Statement) interface{} {
func (m Migrator) GetIndexes(dst interface{}) ([]gorm.Index, error) {
return nil, errors.New("not support")
}
// GetTypeAliases return database type aliases
func (m Migrator) GetTypeAliases(databaseTypeName string) []string {
return nil
}