增加获取表列表方法

This commit is contained in:
2025-09-13 15:14:08 +08:00
parent fcea68c7c7
commit d169f9b9d1
3 changed files with 15 additions and 10 deletions

View File

@@ -0,0 +1,13 @@
package models
// GetAllModels 返回一个包含所有数据库模型实例的切片。
// 这个函数用于在数据库初始化时自动迁移所有的表结构。
func GetAllModels() []interface{} {
return []interface{}{
&User{},
&Device{},
&Plan{},
&SubPlan{},
&Task{},
}
}

View File

@@ -17,7 +17,7 @@ func setupTestDB(t *testing.T) *gorm.DB {
assert.NoError(t, err, "连接内存数据库时发生错误")
// 自动迁移所有需要的表结构
err = db.AutoMigrate(&models.User{}, &models.Device{}, &models.SubPlan{}, &models.Task{}, &models.Plan{})
err = db.AutoMigrate(models.GetAllModels()...)
assert.NoError(t, err, "数据库迁移时发生错误")
return db