Skip to content

Commit df49e19

Browse files
authored
add migrate decimal test
1 parent a69938f commit df49e19

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

tests/migrate_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1987,3 +1987,53 @@ func TestMigrateWithUniqueIndexAndUnique(t *testing.T) {
19871987
}
19881988
}
19891989
}
1990+
1991+
func TestAutoMigrateDecimal(t *testing.T) {
1992+
if DB.Dialector.Name() != "mysql" {
1993+
return
1994+
}
1995+
tracer := Tracer{
1996+
Logger: DB.Config.Logger,
1997+
Test: func(ctx context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), err error) {
1998+
sql, _ := fc()
1999+
if strings.HasPrefix(sql, "ALTER TABLE ") {
2000+
t.Fatalf("shouldn't execute ALTER COLUMN TYPE if decimal is not change: sql: %s", sql)
2001+
}
2002+
},
2003+
}
2004+
session := DB.Session(&gorm.Session{Logger: tracer})
2005+
2006+
type MigrateDecimalColumn struct {
2007+
RecID1 int64 `gorm:"column:recid1;type:decimal(9,0);not null" json:"recid1"`
2008+
}
2009+
DB.Migrator().DropTable(&MigrateDecimalColumn{})
2010+
2011+
if err := DB.AutoMigrate(&MigrateDecimalColumn{}); err != nil {
2012+
t.Fatalf("failed to auto migrate, got error: %v", err)
2013+
}
2014+
if err := session.AutoMigrate(&MigrateDecimalColumn{}); err != nil {
2015+
t.Fatalf("failed to auto migrate, got error: %v", err)
2016+
}
2017+
type MigrateDecimalColumn2 struct {
2018+
RecID1 int64 `gorm:"column:recid1;type:decimal(8,2);not null" json:"recid1"`
2019+
}
2020+
tracer2 := Tracer{
2021+
Logger: DB.Config.Logger,
2022+
Test: func(ctx context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), err error) {
2023+
sql, _ := fc()
2024+
if strings.HasPrefix(sql, "ALTER TABLE ") {
2025+
if sql != "ALTER TABLE `migrate_decimal_columns` MODIFY COLUMN `recid1` decimal(8,2) NOT NULL" {
2026+
t.Fatalf("decimal alter error: %s", sql)
2027+
}
2028+
2029+
}
2030+
},
2031+
}
2032+
session2 := DB.Session(&gorm.Session{Logger: tracer2})
2033+
2034+
err := session2.Table("migrate_decimal_columns").Migrator().AutoMigrate(&MigrateDecimalColumn2{})
2035+
if err != nil {
2036+
t.Fatalf("failed to get column types, got error: %v", err)
2037+
}
2038+
}
2039+

0 commit comments

Comments
 (0)