From 983e96f14253c071b8ab3fb96b4c9f103ad39e1c Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Sat, 8 Oct 2022 16:04:57 +0800 Subject: [PATCH] Add tests for alter column type --- tests/go.mod | 4 ++-- tests/migrate_test.go | 2 +- tests/postgres_test.go | 16 ++++++++++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/tests/go.mod b/tests/go.mod index d28c4bb9..3919a838 100644 --- a/tests/go.mod +++ b/tests/go.mod @@ -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 => ../ diff --git a/tests/migrate_test.go b/tests/migrate_test.go index 32e84e77..b918b4b5 100644 --- a/tests/migrate_test.go +++ b/tests/migrate_test.go @@ -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) diff --git a/tests/postgres_test.go b/tests/postgres_test.go index f45b2618..794ab8f7 100644 --- a/tests/postgres_test.go +++ b/tests/postgres_test.go @@ -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{}) +}