feat: migrator support type aliases

This commit is contained in:
a631807682 2022-08-19 16:24:25 +08:00
parent 8c3018b96a
commit 2fec56db78
No known key found for this signature in database
GPG Key ID: 137D1D75522168AB
2 changed files with 21 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,10 +408,22 @@ func (m Migrator) MigrateColumn(value interface{}, field *schema.Field, columnTy
alterColumn := false
if !field.PrimaryKey {
// check type
if !field.PrimaryKey && !strings.HasPrefix(fullDataType, realDataType) {
var isSameType bool
aliases := m.DB.Migrator().GetTypeAliases(realDataType)
aliases = append(aliases, realDataType)
for _, alias := range aliases {
if strings.HasPrefix(fullDataType, alias) {
isSameType = true
break
}
}
if !isSameType {
alterColumn = true
}
}
// check size
if length, ok := columnType.Length(); length != int64(field.Size) {
@ -863,3 +875,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
}