Add tests for alter column type

This commit is contained in:
Jinzhu 2022-10-08 16:04:57 +08:00
parent 34fbe84580
commit 983e96f142
3 changed files with 19 additions and 3 deletions

View File

@ -9,10 +9,10 @@ require (
github.com/lib/pq v1.10.7
golang.org/x/crypto v0.0.0-20221005025214-4161e89ecf1b // indirect
gorm.io/driver/mysql v1.4.0
gorm.io/driver/postgres v1.4.1
gorm.io/driver/postgres v1.4.3
gorm.io/driver/sqlite v1.4.1
gorm.io/driver/sqlserver v1.4.0
gorm.io/gorm v1.23.10
gorm.io/gorm v1.24.0
)
replace gorm.io/gorm => ../

View File

@ -400,7 +400,7 @@ func TestMigrateColumns(t *testing.T) {
t.Fatalf("column code unique should be correct, name: %v, column: %#v", columnType.Name(), columnType)
}
if v, ok := columnType.DefaultValue(); !sqlserver && (!ok || v != "hello") {
t.Fatalf("column code default value should be correct, name: %v, column: %#v", columnType.Name(), columnType)
t.Fatalf("column code default value should be correct, name: %v, column: %#v, default value: %v", columnType.Name(), columnType, v)
}
if v, ok := columnType.Comment(); !sqlite && !sqlserver && (!ok || v != "my code2") {
t.Fatalf("column code comment should be correct, name: %v, column: %#v", columnType.Name(), columnType)

View File

@ -8,6 +8,7 @@ import (
"github.com/lib/pq"
"gorm.io/gorm"
"gorm.io/gorm/clause"
. "gorm.io/gorm/utils/tests"
)
func TestPostgresReturningIDWhichHasStringType(t *testing.T) {
@ -199,3 +200,18 @@ func TestPostgresOnConstraint(t *testing.T) {
t.Errorf("expected 1 thing got more")
}
}
type CompanyNew struct {
ID int
Name int
}
func TestAlterColumnDataType(t *testing.T) {
DB.AutoMigrate(Company{})
if err := DB.Table("companies").Migrator().AlterColumn(CompanyNew{}, "name"); err != nil {
t.Fatalf("failed to alter column from string to int, got error %v", err)
}
DB.AutoMigrate(Company{})
}