forked from mirror/gorm
parent
b0e13d95b4
commit
3d91802b1d
|
@ -120,7 +120,10 @@ func (m Migrator) AutoMigrate(values ...interface{}) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var (
|
||||
parseIndexes = stmt.Schema.ParseIndexes()
|
||||
parseCheckConstraints = stmt.Schema.ParseCheckConstraints()
|
||||
)
|
||||
for _, dbName := range stmt.Schema.DBNames {
|
||||
field := stmt.Schema.FieldsByDBName[dbName]
|
||||
var foundColumn gorm.ColumnType
|
||||
|
@ -157,7 +160,7 @@ func (m Migrator) AutoMigrate(values ...interface{}) error {
|
|||
}
|
||||
}
|
||||
|
||||
for _, chk := range stmt.Schema.ParseCheckConstraints() {
|
||||
for _, chk := range parseCheckConstraints {
|
||||
if !queryTx.Migrator().HasConstraint(value, chk.Name) {
|
||||
if err := execTx.Migrator().CreateConstraint(value, chk.Name); err != nil {
|
||||
return err
|
||||
|
@ -165,7 +168,7 @@ func (m Migrator) AutoMigrate(values ...interface{}) error {
|
|||
}
|
||||
}
|
||||
|
||||
for _, idx := range stmt.Schema.ParseIndexes() {
|
||||
for _, idx := range parseIndexes {
|
||||
if !queryTx.Migrator().HasIndex(value, idx.Name) {
|
||||
if err := execTx.Migrator().CreateIndex(value, idx.Name); err != nil {
|
||||
return err
|
||||
|
@ -430,7 +433,8 @@ func (m Migrator) MigrateColumn(value interface{}, field *schema.Field, columnTy
|
|||
realDataType := strings.ToLower(columnType.DatabaseTypeName())
|
||||
|
||||
var (
|
||||
alterColumn, isSameType bool
|
||||
alterColumn bool
|
||||
isSameType = fullDataType == realDataType
|
||||
)
|
||||
|
||||
if !field.PrimaryKey {
|
||||
|
|
|
@ -129,6 +129,7 @@ func parseFieldIndexes(field *Field) (indexes []Index, err error) {
|
|||
}
|
||||
|
||||
if (k == "UNIQUEINDEX") || settings["UNIQUE"] != "" {
|
||||
field.Unique = true
|
||||
settings["CLASS"] = "UNIQUE"
|
||||
}
|
||||
|
||||
|
|
|
@ -13,7 +13,7 @@ require (
|
|||
gorm.io/driver/postgres v1.4.6
|
||||
gorm.io/driver/sqlite v1.4.4
|
||||
gorm.io/driver/sqlserver v1.4.1
|
||||
gorm.io/gorm v1.24.2
|
||||
gorm.io/gorm v1.24.3
|
||||
)
|
||||
|
||||
replace gorm.io/gorm => ../
|
||||
|
|
|
@ -1270,6 +1270,38 @@ func TestMigrateDefaultNullString(t *testing.T) {
|
|||
AssertEqual(t, ok, false)
|
||||
}
|
||||
|
||||
func TestMigrateMySQLWithCustomizedTypes(t *testing.T) {
|
||||
if DB.Dialector.Name() != "mysql" {
|
||||
t.Skip()
|
||||
}
|
||||
|
||||
type MyTable struct {
|
||||
Def string `gorm:"size:512;index:idx_def,unique"`
|
||||
Abc string `gorm:"size:65000000"`
|
||||
}
|
||||
|
||||
DB.Migrator().DropTable("my_tables")
|
||||
|
||||
sql := "CREATE TABLE `my_tables` (`def` varchar(512),`abc` longtext,UNIQUE INDEX `idx_def` (`def`))"
|
||||
if err := DB.Exec(sql).Error; err != nil {
|
||||
t.Errorf("Failed, got error: %v", err)
|
||||
}
|
||||
|
||||
session := DB.Session(&gorm.Session{Logger: Tracer{
|
||||
Logger: DB.Config.Logger,
|
||||
Test: func(ctx context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), err error) {
|
||||
sql, _ := fc()
|
||||
if strings.HasPrefix(sql, "ALTER TABLE") {
|
||||
t.Errorf("shouldn't execute: sql=%s", sql)
|
||||
}
|
||||
},
|
||||
}})
|
||||
|
||||
if err := session.AutoMigrate(&MyTable{}); err != nil {
|
||||
t.Errorf("Failed, got error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateIgnoreRelations(t *testing.T) {
|
||||
type RelationModel1 struct {
|
||||
ID uint
|
||||
|
|
Loading…
Reference in New Issue