From 1cd8afb35df3d6f4061934d3b84591e378c64b6c Mon Sep 17 00:00:00 2001 From: samwafgo <138433335+samwafgo@users.noreply.github.com> Date: Tue, 13 Oct 2026 09:30:07 +0800 Subject: [PATCH] fix: cascade-delete CC rules when removing a host and clean up existing orphan rules (#1013) #1010 --- service/waf_service/waf_host.go | 2 ++ wafdb/migrations_core.go | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/service/waf_service/waf_host.go b/service/waf_service/waf_host.go index e08b281..305caf0 100644 --- a/service/waf_service/waf_host.go +++ b/service/waf_service/waf_host.go @@ -376,6 +376,8 @@ func (receiver *WafHostService) DelHostApi(req request.WafHostDelReq) (model.Hos err = global.GWAF_LOCAL_DB.Where("Host_Code = ?", req.CODE).Delete(model.Rules{}).Error //删除Anticc err = global.GWAF_LOCAL_DB.Where("Host_Code = ?", req.CODE).Delete(model.AntiCC{}).Error + //删除CC多规则 + err = global.GWAF_LOCAL_DB.Where("host_code = ?", req.CODE).Delete(model.AntiCCRule{}).Error //删除禁用ip err = global.GWAF_LOCAL_DB.Where("Host_Code = ?", req.CODE).Delete(model.IPBlockList{}).Error //删除禁用url diff --git a/wafdb/migrations_core.go b/wafdb/migrations_core.go index 35596c1..f96aafe 100644 --- a/wafdb/migrations_core.go +++ b/wafdb/migrations_core.go @@ -2385,6 +2385,27 @@ func RunCoreDBMigrations(db *gorm.DB) error { return tx.Migrator().DropColumn(&model.ShareDb{}, "PeriodKey") }, }, + // 迁移: 清理所属站点已不存在的新版CC规则。 + // 引擎按站点加载规则,这类记录没有任何消费方,留着只会把列表统计数抬高 + { + ID: "202610130001_clean_orphan_anti_cc_rules", + Migrate: func(tx *gorm.DB) error { + zlog.Info("迁移 202610130001: 清理无对应站点的新版CC规则") + subQuery := tx.Model(&model.Hosts{}).Select("CODE").Where("CODE <> ''") + res := tx.Where("host_code NOT IN (?)", subQuery).Delete(&model.AntiCCRule{}) + if res.Error != nil { + return fmt.Errorf("清理无对应站点的CC规则失败: %w", res.Error) + } + if res.RowsAffected > 0 { + zlog.Info(fmt.Sprintf("迁移 202610130001: 清除 %d 条无对应站点的CC规则", res.RowsAffected)) + } + return nil + }, + Rollback: func(tx *gorm.DB) error { + // 数据清除类迁移,回滚为空操作 + return nil + }, + }, }) // 执行迁移