From 46bce170cae701615e2b2f8b2448b54524be9648 Mon Sep 17 00:00:00 2001 From: Cr <631807682@qq.com> Date: Mon, 4 Jul 2022 16:42:27 +0800 Subject: [PATCH] test: pg array type (#5480) --- tests/migrate_test.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tests/migrate_test.go b/tests/migrate_test.go index 3d6a7858..0b5bc5eb 100644 --- a/tests/migrate_test.go +++ b/tests/migrate_test.go @@ -923,3 +923,39 @@ func TestDifferentTypeWithoutDeclaredLength(t *testing.T) { AssertEqual(t, "text", strings.ToLower(ct.DatabaseTypeName())) } + +func TestMigrateArrayTypeModel(t *testing.T) { + if DB.Dialector.Name() != "postgres" { + return + } + + type ArrayTypeModel struct { + ID uint + Number string `gorm:"type:varchar(51);NOT NULL"` + TextArray []string `gorm:"type:text[];NOT NULL"` + NestedTextArray [][]string `gorm:"type:text[][]"` + NestedIntArray [][]int64 `gorm:"type:integer[3][3]"` + } + + var err error + DB.Migrator().DropTable(&ArrayTypeModel{}) + + err = DB.AutoMigrate(&ArrayTypeModel{}) + AssertEqual(t, nil, err) + + ct, err := findColumnType(&ArrayTypeModel{}, "number") + AssertEqual(t, nil, err) + AssertEqual(t, "varchar", ct.DatabaseTypeName()) + + ct, err = findColumnType(&ArrayTypeModel{}, "text_array") + AssertEqual(t, nil, err) + AssertEqual(t, "text[]", ct.DatabaseTypeName()) + + ct, err = findColumnType(&ArrayTypeModel{}, "nested_text_array") + AssertEqual(t, nil, err) + AssertEqual(t, "text[]", ct.DatabaseTypeName()) + + ct, err = findColumnType(&ArrayTypeModel{}, "nested_int_array") + AssertEqual(t, nil, err) + AssertEqual(t, "integer[]", ct.DatabaseTypeName()) +}