58 lines
1.9 KiB
Go
58 lines
1.9 KiB
Go
package version
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
"gorm.io/driver/postgres"
|
|
"gorm.io/gorm"
|
|
|
|
migrationModels "git.ilapage.cn/ila/yovision/Sense/server/cmd/migrate/migration/models"
|
|
common "git.ilapage.cn/ila/yovision/Sense/server/common/models"
|
|
)
|
|
|
|
func TestLiveviewMigrationOnPostgres(t *testing.T) {
|
|
dsn := os.Getenv("SENSE_LIVEVIEW_MIGRATION_TEST_DATABASE_URL")
|
|
if dsn == "" {
|
|
t.Skip("set SENSE_LIVEVIEW_MIGRATION_TEST_DATABASE_URL to run the PostgreSQL migration test")
|
|
}
|
|
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
const schema = "sense_liveview_68_test"
|
|
if err = db.Exec("DROP SCHEMA IF EXISTS " + schema + " CASCADE").Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = db.Exec("CREATE SCHEMA " + schema).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { db.Exec("DROP SCHEMA IF EXISTS " + schema + " CASCADE") })
|
|
sqlDB, err := db.DB()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
sqlDB.SetMaxOpenConns(1)
|
|
if err = db.Exec("SET search_path TO " + schema).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)
|
|
}
|
|
}
|
|
if err = migrateSenseLiveview(db, "2026081420000_liveview.go"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var menus, policies, applied int64
|
|
db.Model(&migrationModels.SysMenu{}).Where("menu_name = ?", "SenseLiveview").Count(&menus)
|
|
db.Model(&deviceCasbinRule{}).Where("v1 LIKE ?", "/api/v1/liveview%").Count(&policies)
|
|
db.Model(&common.Migration{}).Where("version = ?", "2026081420000_liveview.go").Count(&applied)
|
|
if menus != 1 || policies != 9 || applied != 1 {
|
|
t.Fatalf("menus=%d policies=%d applied=%d", menus, policies, applied)
|
|
}
|
|
}
|