Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

Add the advisor plugin interface

天舟dd7938c72bdc
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.go
new file mode 100644
index 0000000000..4138b32eb4
--- /dev/null
+++ b/plugin/advisor/advisor.go
@@ -0,0 +1,94 @@
+// Advisor defines the interface for analyzing sql statements.
+// The advisor could be syntax checker, index suggestion etc.
+package advisor
+
+import (
+ "fmt"
+ "sync"
+
+ "github.com/bytebase/bytebase/plugin/db"
+ "go.uber.org/zap"
+)
+
+type Severity string
+
+const (
+ INFO Severity = "INFO"
+ WARN Severity = "WARN"
+ ERROR Severity = "ERROR"
+)
+
+func (e Severity) String() string {
+ switch e {
+ case INFO:
+ return "INFO"
+ case WARN:
+ return "WARN"
+ case ERROR:
+ return "ERROR"
+ }
+ return "UNKNOWN"
+}
+
+type AdvisorType string
+
+const (
+ Fake AdvisorType = "bb.plugin.advisor.fake"
+)
+
+type Advice struct {
+ Severity Severity
+ Title string
+ Content string
+}
+
+type AdvisorContext struct {
+ Logger *zap.Logger
+}
+
+type Advisor interface {
+ Check(ctx AdvisorContext, statement string) ([]Advice, error)
+}
+
+var (
+ advisorMu sync.RWMutex
+ advisors = make(map[db.Type]map[AdvisorType]Advisor)
+)
+
+// Register makes a advisor available by the provided id.
+// If Register is called twice with the same name or if advisor is nil,
+// it panics.
+func Register(dbType db.Type, advType AdvisorType, f Advisor) {
+ advisorMu.Lock()
+ defer advisorMu.Unlock()
+ if f == nil {
+ panic("advisor: Register advisor is nil")
+ }
+ dbAdvisors, ok := advisors[dbType]
+ if !ok {
+ advisors[dbType] = map[AdvisorType]Advisor{
+ advType: f,
+ }
+ } else {
+ if _, dup := dbAdvisors[advType]; dup {
+ panic(fmt.Sprintf("advisor: Register called twice for advisor %v for %v", advType, dbType))
+ }
+ dbAdvisors[advType] = f
+ }
+}
+
+func Check(dbType db.Type, advType AdvisorType, ctx AdvisorContext, statement string) ([]Advice, error) {
+ advisorMu.RLock()
+ dbAdvisors, ok := advisors[dbType]
+ defer advisorMu.RUnlock()
+ if !ok {
+ return nil, fmt.Errorf("advisor: unknown advisor %v for %v", advType, dbType)
+ }
+
+ f, ok := dbAdvisors[advType]
+ if !ok {
+ return nil, fmt.Errorf("advisor: unknown advisor %v for %v", advType, dbType)
+ }
+
+ return f.Check(ctx, statement)
+}
diff --git a/plugin/advisor/fake/fake_advisor.go b/plugin/advisor/fake/fake_advisor.go
new file mode 100644
index 0000000000..1d450a39f7
--- /dev/null
+++ b/plugin/advisor/fake/fake_advisor.go
@@ -0,0 +1,40 @@
+package fake
+
+import (
+ "github.com/bytebase/bytebase/plugin/advisor"
+ "github.com/bytebase/bytebase/plugin/db"
+)
+
+var (
+ _ advisor.Advisor = (*Advisor)(nil)
+)
+
+func init() {
+ advisor.Register(db.MySQL, advisor.Fake, &Advisor{})
+ advisor.Register(db.Postgres, advisor.Fake, &Advisor{})
+ advisor.Register(db.TiDB, advisor.Fake, &Advisor{})
+}
+
+type Advisor struct {
+}
+
+// A fake advisor to report 1 advice for each severity.
+func (adv *Advisor) Check(ctx advisor.AdvisorContext, statement string) ([]advisor.Advice, error) {
+ return []advisor.Advice{
+ {
+ Severity: advisor.INFO,
+ Title: "INFO check",
+ Content: "Fake check returnings INFO result",
+ },
+ {
+ Severity: advisor.WARN,
+ Title: "WARN check",
+ Content: "Fake check returnings WARN result",
+ },
+ {
+ Severity: advisor.ERROR,
+ Title: "ERROR check",
+ Content: "Fake check returnings ERROR result",
+ },
+ }, nil
+}