test(#352): verify additive source-time migration

This commit is contained in:
QiuSW
2026-09-30 10:02:32 +08:00
parent 31ff045e8c
commit 1f3ce610b9
@@ -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)
}
}