From 727504393f69869b39d34ae5c78609e208962514 Mon Sep 17 00:00:00 2001 From: littleniannian Date: Thu, 26 Dec 2024 14:35:35 +0800 Subject: [PATCH 1/3] fix: new category mapping opt --- sqle/model/utils.go | 62 ++++++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 26 deletions(-) diff --git a/sqle/model/utils.go b/sqle/model/utils.go index c28a19f81..66d1f2a31 100644 --- a/sqle/model/utils.go +++ b/sqle/model/utils.go @@ -228,17 +228,7 @@ func (s *Storage) UpdateCustomRuleCategoryRels() error { if existed { return nil } - newCategoryMap := categoryMapping[customRule.Typ] - var tags []string - if newCategoryMap == nil { - // 自定义规则可以自己定义规则分类,对于这种分类的统一映射 - tags = []string{plocale.RuleTagDatabase.ID, plocale.RuleTagTablespace.ID, plocale.RuleTagTable.ID, plocale.RuleTagColumn.ID, plocale.RuleTagIndex.ID, plocale.RuleTagView.ID, plocale.RuleTagProcedure.ID, plocale.RuleTagFunction.ID, plocale.RuleTagTrigger.ID, plocale.RuleTagEvent.ID, plocale.RuleTagUser.ID} - } else { - // 这里的newCategoryMap只会有一次循环 - for _, newTags := range newCategoryMap { - tags = newTags - } - } + tags := mappingToNewCategory(customRule.Desc, customRule.Typ) // 获取分类表中的分类信息 auditRuleCategories, err := s.GetAuditRuleCategoryByTagIn(tags) if err != nil { @@ -566,12 +556,33 @@ func (s *Storage) CreateDefaultTemplateIfNotExist(projectId ProjectUID, rules ma return nil } +func mappingToNewCategory(ruleName string, oldCategory string) []string { + // 当旧规则是命名规范的映射关系 + if oldCategory == plocale.RuleTypeNamingConvention.Other { + if strings.Contains(ruleName, "database") || strings.Contains(ruleName, "object") { + return []string{plocale.RuleTagDatabase.ID} + } else if strings.Contains(ruleName, "index") || strings.Contains(ruleName, "pk") { + return []string{plocale.RuleTagIndex.ID} + } else { + return []string{ + plocale.RuleTagDatabase.ID, plocale.RuleTagTablespace.ID, plocale.RuleTagTable.ID, plocale.RuleTagColumn.ID, plocale.RuleTagIndex.ID, plocale.RuleTagView.ID, plocale.RuleTagProcedure.ID, plocale.RuleTagFunction.ID, plocale.RuleTagTrigger.ID, plocale.RuleTagEvent.ID, plocale.RuleTagUser.ID} + } + } + newCategoryMap := categoryMapping[oldCategory] + if newCategoryMap == nil { + return []string{ + plocale.RuleTagDatabase.ID, plocale.RuleTagTablespace.ID, plocale.RuleTagTable.ID, plocale.RuleTagColumn.ID, plocale.RuleTagIndex.ID, plocale.RuleTagView.ID, plocale.RuleTagProcedure.ID, plocale.RuleTagFunction.ID, plocale.RuleTagTrigger.ID, plocale.RuleTagEvent.ID, plocale.RuleTagUser.ID} + } + tags := make([]string, 0) + for _, newTags := range newCategoryMap { + tags = append(tags, newTags...) + } + return tags +} + var categoryMapping = map[string]map[string][]string{ plocale.RuleTypeGlobalConfig.Other: { - plocale.RuleCategoryAuditPurpose.ID: {plocale.RuleTagMaintenance.ID}, - }, - plocale.RuleTypeNamingConvention.Other: { - plocale.RuleCategoryOperand.ID: {plocale.RuleTagDatabase.ID, plocale.RuleTagTablespace.ID, plocale.RuleTagTable.ID, plocale.RuleTagColumn.ID, plocale.RuleTagIndex.ID, plocale.RuleTagView.ID, plocale.RuleTagProcedure.ID, plocale.RuleTagFunction.ID, plocale.RuleTagTrigger.ID, plocale.RuleTagEvent.ID, plocale.RuleTagUser.ID}, + plocale.RuleCategoryAuditPurpose.ID: {plocale.RuleTagPerformance.ID}, }, plocale.RuleTypeIndexingConvention.Other: { plocale.RuleCategoryOperand.ID: {plocale.RuleTagIndex.ID}, @@ -589,7 +600,7 @@ var categoryMapping = map[string]map[string][]string{ plocale.RuleCategorySQL.ID: {plocale.RuleTagDML.ID}, }, plocale.RuleTypeUsageSuggestion.Other: { - plocale.RuleCategoryOperand.ID: {plocale.RuleTagDatabase.ID, plocale.RuleTagTablespace.ID, plocale.RuleTagTable.ID, plocale.RuleTagColumn.ID, plocale.RuleTagIndex.ID, plocale.RuleTagView.ID, plocale.RuleTagProcedure.ID, plocale.RuleTagFunction.ID, plocale.RuleTagTrigger.ID, plocale.RuleTagEvent.ID, plocale.RuleTagUser.ID}, + plocale.RuleCategoryAuditPurpose.ID: {plocale.RuleTagMaintenance.ID}, }, plocale.RuleTypeExecutePlan.Other: { plocale.RuleCategoryAuditPurpose.ID: {plocale.RuleTagPerformance.ID}, @@ -609,19 +620,18 @@ func (s *Storage) UpdateRuleCategoryRels(rule *Rule) error { if existed { return nil } - for _, tags := range categoryMapping[oldCategory] { - // 获取分类表中的分类信息 - auditRuleCategories, err := s.GetAuditRuleCategoryByTagIn(tags) + tags := mappingToNewCategory(rule.Name, oldCategory) + // 获取分类表中的分类信息 + auditRuleCategories, err := s.GetAuditRuleCategoryByTagIn(tags) + if err != nil { + return err + } + for _, newCategory := range auditRuleCategories { + auditRuleCategoryRel := AuditRuleCategoryRel{CategoryId: newCategory.ID, RuleName: rule.Name, RuleDBType: rule.DBType} + err = s.db.Create(&auditRuleCategoryRel).Error if err != nil { return err } - for _, newCategory := range auditRuleCategories { - auditRuleCategoryRel := AuditRuleCategoryRel{CategoryId: newCategory.ID, RuleName: rule.Name, RuleDBType: rule.DBType} - err = s.db.Create(&auditRuleCategoryRel).Error - if err != nil { - return err - } - } } auditAccuracyCategories, err := s.GetAuditRuleCategoryByCategory(plocale.RuleCategoryAuditAccuracy.ID) if err != nil { From 6339c9a1ce0c8f0b34fce0512c961c64abbb0f64 Mon Sep 17 00:00:00 2001 From: littleniannian Date: Thu, 26 Dec 2024 17:27:59 +0800 Subject: [PATCH 2/3] fix: comment refresh category --- sqle/model/utils.go | 81 ++++++++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 38 deletions(-) diff --git a/sqle/model/utils.go b/sqle/model/utils.go index 66d1f2a31..fff8a5a60 100644 --- a/sqle/model/utils.go +++ b/sqle/model/utils.go @@ -220,27 +220,26 @@ func (s *Storage) UpdateCustomRuleCategoryRels() error { // 新的规则分类Typ字段为""说明已经有了新的分类关系,直接忽略 continue } - _, existed, err := s.FirstCustomRuleCategoryRelByCustomRuleId(customRule.RuleId) - if err != nil { - return err - } - // 已存在规则关系直接忽略 - if existed { - return nil - } tags := mappingToNewCategory(customRule.Desc, customRule.Typ) // 获取分类表中的分类信息 auditRuleCategories, err := s.GetAuditRuleCategoryByTagIn(tags) if err != nil { return err } - for _, newCategory := range auditRuleCategories { - customerCategoryRel := CustomRuleCategoryRel{CategoryId: newCategory.ID, CustomRuleId: customRule.RuleId} - err = s.db.Create(&customerCategoryRel).Error - if err != nil { - return err + err = s.db.Transaction(func(tx *gorm.DB) error { + for _, newCategory := range auditRuleCategories { + customerCategoryRel := CustomRuleCategoryRel{CategoryId: newCategory.ID, CustomRuleId: customRule.RuleId} + err = s.db.Delete(&customerCategoryRel).Error + if err != nil { + return err + } + err = s.db.Create(&customerCategoryRel).Error + if err != nil { + return err + } } - } + return nil + }) } return nil } @@ -612,42 +611,48 @@ var categoryMapping = map[string]map[string][]string{ func (s *Storage) UpdateRuleCategoryRels(rule *Rule) error { oldCategory := rule.I18nRuleInfo.GetRuleInfoByLangTag(language.Chinese).Category - _, existed, err := s.FirstAuditRuleCategoryRelByRule(rule.Name, rule.DBType) - if err != nil { - return err - } - // 某个规则存在分类不做处理 - if existed { - return nil - } tags := mappingToNewCategory(rule.Name, oldCategory) // 获取分类表中的分类信息 auditRuleCategories, err := s.GetAuditRuleCategoryByTagIn(tags) if err != nil { return err } - for _, newCategory := range auditRuleCategories { - auditRuleCategoryRel := AuditRuleCategoryRel{CategoryId: newCategory.ID, RuleName: rule.Name, RuleDBType: rule.DBType} - err = s.db.Create(&auditRuleCategoryRel).Error - if err != nil { - return err + err = s.db.Transaction(func(tx *gorm.DB) error { + for _, newCategory := range auditRuleCategories { + auditRuleCategoryRel := AuditRuleCategoryRel{CategoryId: newCategory.ID, RuleName: rule.Name, RuleDBType: rule.DBType} + err = tx.Delete(auditRuleCategoryRel).Error + if err != nil { + return err + } + err = tx.Create(&auditRuleCategoryRel).Error + if err != nil { + return err + } } - } + return nil + }) auditAccuracyCategories, err := s.GetAuditRuleCategoryByCategory(plocale.RuleCategoryAuditAccuracy.ID) if err != nil { return err } - // 根据离线/在线审核生成规则的分类关系 - for _, auditAccuracyCategory := range auditAccuracyCategories { - if !rule.AllowOffline && auditAccuracyCategory.Tag == plocale.RuleTagOffline.ID { - continue - } - auditRuleCategoryRel := AuditRuleCategoryRel{CategoryId: auditAccuracyCategory.ID, RuleName: rule.Name, RuleDBType: rule.DBType} - err = s.db.Create(&auditRuleCategoryRel).Error - if err != nil { - return err + err = s.db.Transaction(func(tx *gorm.DB) error { + // 根据离线/在线审核生成规则的分类关系 + for _, auditAccuracyCategory := range auditAccuracyCategories { + if !rule.AllowOffline && auditAccuracyCategory.Tag == plocale.RuleTagOffline.ID { + continue + } + auditRuleCategoryRel := AuditRuleCategoryRel{CategoryId: auditAccuracyCategory.ID, RuleName: rule.Name, RuleDBType: rule.DBType} + err = s.db.Delete(auditRuleCategoryRel).Error + if err != nil { + return err + } + err = s.db.Create(&auditRuleCategoryRel).Error + if err != nil { + return err + } } - } + return nil + }) return err } From ffda5262e4a9a4e918d9bb6843a4bb0098c668d3 Mon Sep 17 00:00:00 2001 From: littleniannian Date: Thu, 26 Dec 2024 18:00:52 +0800 Subject: [PATCH 3/3] rollback: utils.go --- sqle/model/utils.go | 81 +++++++++++++++++++++------------------------ 1 file changed, 38 insertions(+), 43 deletions(-) diff --git a/sqle/model/utils.go b/sqle/model/utils.go index fff8a5a60..66d1f2a31 100644 --- a/sqle/model/utils.go +++ b/sqle/model/utils.go @@ -220,26 +220,27 @@ func (s *Storage) UpdateCustomRuleCategoryRels() error { // 新的规则分类Typ字段为""说明已经有了新的分类关系,直接忽略 continue } + _, existed, err := s.FirstCustomRuleCategoryRelByCustomRuleId(customRule.RuleId) + if err != nil { + return err + } + // 已存在规则关系直接忽略 + if existed { + return nil + } tags := mappingToNewCategory(customRule.Desc, customRule.Typ) // 获取分类表中的分类信息 auditRuleCategories, err := s.GetAuditRuleCategoryByTagIn(tags) if err != nil { return err } - err = s.db.Transaction(func(tx *gorm.DB) error { - for _, newCategory := range auditRuleCategories { - customerCategoryRel := CustomRuleCategoryRel{CategoryId: newCategory.ID, CustomRuleId: customRule.RuleId} - err = s.db.Delete(&customerCategoryRel).Error - if err != nil { - return err - } - err = s.db.Create(&customerCategoryRel).Error - if err != nil { - return err - } + for _, newCategory := range auditRuleCategories { + customerCategoryRel := CustomRuleCategoryRel{CategoryId: newCategory.ID, CustomRuleId: customRule.RuleId} + err = s.db.Create(&customerCategoryRel).Error + if err != nil { + return err } - return nil - }) + } } return nil } @@ -611,48 +612,42 @@ var categoryMapping = map[string]map[string][]string{ func (s *Storage) UpdateRuleCategoryRels(rule *Rule) error { oldCategory := rule.I18nRuleInfo.GetRuleInfoByLangTag(language.Chinese).Category + _, existed, err := s.FirstAuditRuleCategoryRelByRule(rule.Name, rule.DBType) + if err != nil { + return err + } + // 某个规则存在分类不做处理 + if existed { + return nil + } tags := mappingToNewCategory(rule.Name, oldCategory) // 获取分类表中的分类信息 auditRuleCategories, err := s.GetAuditRuleCategoryByTagIn(tags) if err != nil { return err } - err = s.db.Transaction(func(tx *gorm.DB) error { - for _, newCategory := range auditRuleCategories { - auditRuleCategoryRel := AuditRuleCategoryRel{CategoryId: newCategory.ID, RuleName: rule.Name, RuleDBType: rule.DBType} - err = tx.Delete(auditRuleCategoryRel).Error - if err != nil { - return err - } - err = tx.Create(&auditRuleCategoryRel).Error - if err != nil { - return err - } + for _, newCategory := range auditRuleCategories { + auditRuleCategoryRel := AuditRuleCategoryRel{CategoryId: newCategory.ID, RuleName: rule.Name, RuleDBType: rule.DBType} + err = s.db.Create(&auditRuleCategoryRel).Error + if err != nil { + return err } - return nil - }) + } auditAccuracyCategories, err := s.GetAuditRuleCategoryByCategory(plocale.RuleCategoryAuditAccuracy.ID) if err != nil { return err } - err = s.db.Transaction(func(tx *gorm.DB) error { - // 根据离线/在线审核生成规则的分类关系 - for _, auditAccuracyCategory := range auditAccuracyCategories { - if !rule.AllowOffline && auditAccuracyCategory.Tag == plocale.RuleTagOffline.ID { - continue - } - auditRuleCategoryRel := AuditRuleCategoryRel{CategoryId: auditAccuracyCategory.ID, RuleName: rule.Name, RuleDBType: rule.DBType} - err = s.db.Delete(auditRuleCategoryRel).Error - if err != nil { - return err - } - err = s.db.Create(&auditRuleCategoryRel).Error - if err != nil { - return err - } + // 根据离线/在线审核生成规则的分类关系 + for _, auditAccuracyCategory := range auditAccuracyCategories { + if !rule.AllowOffline && auditAccuracyCategory.Tag == plocale.RuleTagOffline.ID { + continue } - return nil - }) + auditRuleCategoryRel := AuditRuleCategoryRel{CategoryId: auditAccuracyCategory.ID, RuleName: rule.Name, RuleDBType: rule.DBType} + err = s.db.Create(&auditRuleCategoryRel).Error + if err != nil { + return err + } + } return err }