Skip to content

Commit

Permalink
refactor: group delete check (#2368)
Browse files Browse the repository at this point in the history
Co-authored-by: Xu Bin <140785332+Reditiny@users.noreply.github.com>
  • Loading branch information
710leo and Reditiny authored Dec 18, 2024
1 parent 7cc65a2 commit 5702fc8
Showing 1 changed file with 43 additions and 22 deletions.
65 changes: 43 additions & 22 deletions models/busi_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,53 +115,74 @@ func BusiGroupExists(ctx *ctx.Context, where string, args ...interface{}) (bool,
return num > 0, err
}

var entries = []struct {
entry interface{}
errorMessage string
}{
// RegisterGroupDelCheckEntries 提供给外部注册删除 group 时需要检查的表
func RegisterGroupDelCheckEntries(e []CheckEntry) {
entries = append(entries, e...)
}

type CheckEntry struct {
Entry interface{}
ErrorMessage string
FieldName string
}

var entries = []CheckEntry{
{
Entry: &AlertRule{},
ErrorMessage: "Some alert rules still in the BusiGroup",
FieldName: "group_id",
},
{
entry: &AlertRule{},
errorMessage: "Some alert rules still in the BusiGroup",
Entry: &AlertMute{},
ErrorMessage: "Some alert mutes still in the BusiGroup",
FieldName: "group_id",
},
{
entry: &AlertMute{},
errorMessage: "Some alert mutes still in the BusiGroup",
Entry: &AlertSubscribe{},
ErrorMessage: "Some alert subscribes still in the BusiGroup",
FieldName: "group_id",
},
{
entry: &AlertSubscribe{},
errorMessage: "Some alert subscribes still in the BusiGroup",
Entry: &Board{},
ErrorMessage: "Some Board still in the BusiGroup",
FieldName: "group_id",
},
{
entry: &Target{},
errorMessage: "Some targets still in the BusiGroup",
Entry: &Target{},
ErrorMessage: "Some targets still in the BusiGroup",
FieldName: "group_id",
},
{
entry: &RecordingRule{},
errorMessage: "Some recording rules still in the BusiGroup",
Entry: &RecordingRule{},
ErrorMessage: "Some recording rules still in the BusiGroup",
FieldName: "group_id",
},
{
entry: &TaskTpl{},
errorMessage: "Some recovery scripts still in the BusiGroup",
Entry: &TaskTpl{},
ErrorMessage: "Some recovery scripts still in the BusiGroup",
FieldName: "group_id",
},
{
entry: &TaskRecord{},
errorMessage: "Some Task Record records still in the BusiGroup",
Entry: &TaskRecord{},
ErrorMessage: "Some Task Record records still in the BusiGroup",
FieldName: "group_id",
},
{
entry: &TargetBusiGroup{},
errorMessage: "Some target busigroups still in the BusiGroup",
Entry: &TargetBusiGroup{},
ErrorMessage: "Some target busigroups still in the BusiGroup",
FieldName: "group_id",
},
}

func (bg *BusiGroup) Del(ctx *ctx.Context) error {
for _, e := range entries {
has, err := Exists(DB(ctx).Model(e.entry).Where("group_id=?", bg.Id))
has, err := Exists(DB(ctx).Model(e.Entry).Where(fmt.Sprintf("%s=?", e.FieldName), bg.Id))
if err != nil {
return err
}

if has {
return errors.New(e.errorMessage)
return errors.New(e.ErrorMessage)
}
}

Expand Down

0 comments on commit 5702fc8

Please sign in to comment.