From 1f3ce610b9cb5e8f8887becb367d6302d588d924 Mon Sep 17 00:00:00 2001 From: QiuSW <105186638@qq.com> Date: Wed, 30 Sep 2026 10:02:32 +0800 Subject: [PATCH] test(#352): verify additive source-time migration --- ...1789810000000_syb_stock_created_at_test.go | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 server/cmd/migrate/migration/version-local/1789810000000_syb_stock_created_at_test.go diff --git a/server/cmd/migrate/migration/version-local/1789810000000_syb_stock_created_at_test.go b/server/cmd/migrate/migration/version-local/1789810000000_syb_stock_created_at_test.go new file mode 100644 index 0000000..86b42a1 --- /dev/null +++ b/server/cmd/migrate/migration/version-local/1789810000000_syb_stock_created_at_test.go @@ -0,0 +1,43 @@ +package version_local + +import ( + "testing" + "time" + + "go-admin/app/goauto/models" + common "go-admin/common/models" + "gorm.io/driver/sqlite" + "gorm.io/gorm" +) + +func TestMigrateSYBStockCreatedAtPreservesLegacyRows(t *testing.T) { + db, err := gorm.Open(sqlite.Open("file:syb_stock_created_migration?mode=memory&cache=shared"), &gorm.Config{}) + if err != nil { + t.Fatal(err) + } + if err := db.Exec("CREATE TABLE syb_product (id integer primary key, created_at datetime)").Error; err != nil { + t.Fatal(err) + } + if err := db.Exec("INSERT INTO syb_product (id, created_at) VALUES (1, '2026-09-28 00:05:00')").Error; err != nil { + t.Fatal(err) + } + if err := db.AutoMigrate(&common.Migration{}); err != nil { + t.Fatal(err) + } + if err := migrateSYBStockCreatedAt(db, "test_syb_stock_created_at"); err != nil { + t.Fatal(err) + } + if !db.Migrator().HasColumn(&models.SYBProduct{}, "SYBStockCreatedAt") || !db.Migrator().HasIndex(&models.SYBProduct{}, "SYBStockCreatedAt") { + t.Fatal("migration must add the source time column and index") + } + var row struct { + CreatedAt time.Time + SYBStockCreatedAt *string + } + if err := db.Table("syb_product").Where("id = 1").Take(&row).Error; err != nil { + t.Fatal(err) + } + if row.CreatedAt.Format("2006-01-02 15:04:05") != "2026-09-28 00:05:00" || row.SYBStockCreatedAt != nil { + t.Fatalf("migration must preserve import time and leave source time unknown: %+v", row) + } +}