Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Issue ee 1059 #1922

Merged
merged 2 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions sqle/driver/v2/driver_grpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ type Rule struct {

// Category is the category of the rule. Such as "Naming Conventions"...
// Rules will be displayed on the SQLE rule list page by category.
Category string
Level RuleLevel
Params params.Params
Category string
Level RuleLevel
Params params.Params
Knowledge RuleKnowledge
}

type Config struct {
Expand Down
4 changes: 4 additions & 0 deletions sqle/driver/v2/driver_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,3 +297,7 @@ func NewKillProcessInfo(errorMessage string) *KillProcessInfo {
ErrMessage: errorMessage,
}
}

type RuleKnowledge struct {
Content string
}
381 changes: 205 additions & 176 deletions sqle/driver/v2/proto/driver_v2.pb.go

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions sqle/driver/v2/proto/driver_v2.proto
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ message Rule {
string category = 4;
repeated Param params = 5;
string annotation = 6;
Knowledge knowledge = 7;
}

message Knowledge {
string content = 1;
}

// Metas
Expand Down
1 change: 1 addition & 0 deletions sqle/driver/v2/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func ConvertRuleFromProtoToDriver(rule *protoV2.Rule) *Rule {
Annotation: rule.Annotation,
Level: RuleLevel(rule.Level),
Params: ps,
Knowledge: RuleKnowledge{Content: rule.Knowledge.GetContent()},
}
}

Expand Down
4 changes: 2 additions & 2 deletions sqle/model/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,9 @@ func (s *Storage) GetRule(name, dbType string) (*Rule, bool, error) {
return &rule, true, errors.New(errors.ConnectStorageError, err)
}

func (s *Storage) GetAllRule() ([]*Rule, error) {
func (s *Storage) GetAllRules() ([]*Rule, error) {
rules := []*Rule{}
err := s.db.Find(&rules).Error
err := s.db.Preload("RuleKnowledge").Find(&rules).Error
return rules, errors.New(errors.ConnectStorageError, err)
}

Expand Down
24 changes: 20 additions & 4 deletions sqle/model/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,24 @@ func (s *Storage) AutoMigrate() error {
}

func (s *Storage) CreateRulesIfNotExist(rules map[string][]*driverV2.Rule) error {
isRuleExistInDB := func(rulesInDB []*Rule, targetRuleName, dbType string) (*Rule, bool) {
for i := range rulesInDB {
rule := rulesInDB[i]
if rule.DBType != dbType || rule.Name != targetRuleName {
continue
}
return rule, true
}
return nil, false
}

rulesInDB, err := s.GetAllRules()
if err != nil {
return err
}
for dbType, rules := range rules {
for _, rule := range rules {
existedRule, exist, err := s.GetRule(rule.Name, dbType)
if err != nil {
return err
}
existedRule, exist := isRuleExistInDB(rulesInDB, rule.Name, dbType)
// rule will be created or update if:
// 1. rule not exist;
if !exist {
Expand All @@ -226,6 +238,10 @@ func (s *Storage) CreateRulesIfNotExist(rules map[string][]*driverV2.Rule) error
isParamSame := reflect.DeepEqual(existRuleParam, pluginRuleParam)

if !isRuleDescSame || !isRuleAnnotationSame || !isRuleLevelSame || !isRuleTypSame || !isParamSame {
if existedRule.Knowledge.Content != "" {
// 知识库是可以在页面上编辑的,而插件里只是默认内容,以页面上编辑后的内容为准
rule.Knowledge.Content = existedRule.Knowledge.Content
}
err := s.Save(GenerateRuleByDriverRule(rule, dbType))
if err != nil {
return err
Expand Down
Loading