GetMissingModels returns model names that are referenced in the system
()
| 2 | |
| 3 | // GetMissingModels returns model names that are referenced in the system |
| 4 | func GetMissingModels() ([]string, error) { |
| 5 | // 1. 获取所有已启用模型(去重) |
| 6 | models := GetEnabledModels() |
| 7 | if len(models) == 0 { |
| 8 | return []string{}, nil |
| 9 | } |
| 10 | |
| 11 | // 2. 查询已有的元数据模型名 |
| 12 | var existing []string |
| 13 | if err := DB.Model(&Model{}).Where("model_name IN ?", models).Pluck("model_name", &existing).Error; err != nil { |
| 14 | return nil, err |
| 15 | } |
| 16 | |
| 17 | existingSet := make(map[string]struct{}, len(existing)) |
| 18 | for _, e := range existing { |
| 19 | existingSet[e] = struct{}{} |
| 20 | } |
| 21 | |
| 22 | // 3. 收集缺失模型 |
| 23 | var missing []string |
| 24 | for _, name := range models { |
| 25 | if _, ok := existingSet[name]; !ok { |
| 26 | missing = append(missing, name) |
| 27 | } |
| 28 | } |
| 29 | return missing, nil |
| 30 | } |
no test coverage detected