test: 覆盖旧设备完整迁移链 (#92)
This commit is contained in:
@@ -10,6 +10,8 @@ import (
|
|||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
|
||||||
deviceModels "git.ilapage.cn/ila/yovision/Sense/server/app/sense/device/models"
|
deviceModels "git.ilapage.cn/ila/yovision/Sense/server/app/sense/device/models"
|
||||||
|
migrationModels "git.ilapage.cn/ila/yovision/Sense/server/cmd/migrate/migration/models"
|
||||||
|
common "git.ilapage.cn/ila/yovision/Sense/server/common/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCanonicalLegacyCapabilities(t *testing.T) {
|
func TestCanonicalLegacyCapabilities(t *testing.T) {
|
||||||
@@ -126,6 +128,40 @@ func TestDeviceCapabilitiesMigrationOnPostgres(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("completes the device migration and records its version", func(t *testing.T) {
|
||||||
|
resetLegacyDeviceTable(t, db)
|
||||||
|
if err = db.Exec(`INSERT INTO sense_devices (id, capabilities) VALUES ('legacy', 'video')`).Error; err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err = db.AutoMigrate(&migrationModels.SysRole{}, &migrationModels.SysMenu{}, &deviceCasbinRule{}, &common.Migration{}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
for _, role := range []string{"implementation_operator", "site_admin", "viewer"} {
|
||||||
|
if err = db.Create(&migrationModels.SysRole{RoleName: role, RoleKey: role, Status: "2"}).Error; err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const migrationVersion = "2026081414000_device.go"
|
||||||
|
if err = migrateSenseDeviceLedger(db, migrationVersion); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
assertCapabilitiesColumn(t, db, "jsonb", "'[]'::jsonb")
|
||||||
|
var migratedValue string
|
||||||
|
if err = db.Raw(`SELECT capabilities::text FROM sense_devices WHERE id = 'legacy'`).Scan(&migratedValue).Error; err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if strings.ReplaceAll(migratedValue, " ", "") != `["video"]` {
|
||||||
|
t.Fatalf("unexpected full-migration value: %q", migratedValue)
|
||||||
|
}
|
||||||
|
var applied int64
|
||||||
|
if err = db.Model(&common.Migration{}).Where("version = ?", migrationVersion).Count(&applied).Error; err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if applied != 1 {
|
||||||
|
t.Fatalf("migration record count=%d", applied)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func resetLegacyDeviceTable(t *testing.T, db *gorm.DB) {
|
func resetLegacyDeviceTable(t *testing.T, db *gorm.DB) {
|
||||||
|
|||||||
Reference in New Issue
Block a user