Skip to content

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

chore: hello world advisor (#17782)

h3n4lfe132fa26090
diff --git a/backend/plugin/advisor/pgantlr/advisor_hello_world.go b/backend/plugin/advisor/pgantlr/advisor_hello_world.go
new file mode 100644
index 0000000000..36f8f4e2e6
--- /dev/null
+++ b/backend/plugin/advisor/pgantlr/advisor_hello_world.go
@@ -0,0 +1,41 @@
+package pgantlr
+
+import (
+ "context"
+
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/plugin/advisor"
+)
+
+var (
+ _ advisor.Advisor = (*HelloWorldAdvisor)(nil)
+)
+
+const (
+ // HelloWorldRule is a test-only rule type for hello world advisor
+ HelloWorldRule advisor.SQLReviewRuleType = "hello-world"
+)
+
+func init() {
+ advisor.Register(storepb.Engine_POSTGRES, HelloWorldRule, &HelloWorldAdvisor{})
+}
+
+// HelloWorldAdvisor is a test advisor that always reports "hello world".
+type HelloWorldAdvisor struct {
+}
+
+// Check always returns a warning at line 0 column 0 with message "hello world".
+func (*HelloWorldAdvisor) Check(_ context.Context, _ advisor.Context) ([]*storepb.Advice, error) {
+ return []*storepb.Advice{
+ {
+ Status: storepb.Advice_WARNING,
+ Code: advisor.Ok.Int32(),
+ Title: "Hello World Test",
+ Content: "hello world",
+ StartPosition: &storepb.Position{
+ Line: 0,
+ Column: 0,
+ },
+ },
+ }, nil
+}
diff --git a/backend/plugin/advisor/pgantlr/pgantlr_test.go b/backend/plugin/advisor/pgantlr/pgantlr_test.go
new file mode 100644
index 0000000000..95ce6b0d55
--- /dev/null
+++ b/backend/plugin/advisor/pgantlr/pgantlr_test.go
@@ -0,0 +1,31 @@
+package pgantlr
+
+import (
+ "testing"
+
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/plugin/advisor"
+)
+
+func TestPostgreSQLANTLRRules(t *testing.T) {
+ antlrRules := []advisor.SQLReviewRuleType{
+ HelloWorldRule, // Test advisor to verify framework works
+ // Add real rules here as you migrate them from legacy pg/ folder
+ // Example:
+ // advisor.SchemaRuleStatementDisallowCommit,
+ // advisor.SchemaRuleStatementInsertMustSpecifyColumn,
+ // advisor.SchemaRuleTableNaming,
+ // etc.
+ }
+
+ for _, rule := range antlrRules {
+ needMetaData := advisorNeedMockData[rule]
+ RunANTLRAdvisorRuleTest(t, rule, storepb.Engine_POSTGRES, needMetaData, false /* record */)
+ }
+}
+
+// Add SQL review type here if you need metadata for test.
+var advisorNeedMockData = map[advisor.SQLReviewRuleType]bool{
+ // advisor.SchemaRuleFullyQualifiedObjectName: true,
+ // advisor.BuiltinRulePriorBackupCheck: true,
+}
diff --git a/backend/plugin/advisor/pgantlr/test/hello_world.yaml b/backend/plugin/advisor/pgantlr/test/hello_world.yaml
new file mode 100644
index 0000000000..ba6a79cb0f
--- /dev/null
+++ b/backend/plugin/advisor/pgantlr/test/hello_world.yaml
@@ -0,0 +1,35 @@
+- statement: SELECT 1
+ changeType: 1
+ want:
+ - status: 2
+ code: 0
+ title: Hello World Test
+ content: hello world
+ startposition:
+ line: 0
+ column: 0
+ endposition: null
+- statement: ""
+ changeType: 1
+ want:
+ - status: 2
+ code: 0
+ title: Hello World Test
+ content: hello world
+ startposition:
+ line: 0
+ column: 0
+ endposition: null
+- statement: |-
+ CREATE TABLE test(id int);
+ INSERT INTO test VALUES (1);
+ changeType: 1
+ want:
+ - status: 2
+ code: 0
+ title: Hello World Test
+ content: hello world
+ startposition:
+ line: 0
+ column: 0
+ endposition: null
diff --git a/backend/plugin/advisor/pgantlr/test_helper.go b/backend/plugin/advisor/pgantlr/test_helper.go
new file mode 100644
index 0000000000..cbebfd13c8
--- /dev/null
+++ b/backend/plugin/advisor/pgantlr/test_helper.go
@@ -0,0 +1,146 @@
+package pgantlr
+
+import (
+ "context"
+ "io"
+ "os"
+ "path/filepath"
+ "slices"
+ "strings"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+ "gopkg.in/yaml.v3"
+
+ "github.com/bytebase/bytebase/backend/component/sheet"
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/plugin/advisor"
+ "github.com/bytebase/bytebase/backend/plugin/advisor/catalog"
+)
+
+// TestCase is the data struct for test.
+type TestCase struct {
+ Statement string `yaml:"statement"`
+ ChangeType storepb.PlanCheckRunConfig_ChangeDatabaseType `yaml:"changeType"`
+ Want []*storepb.Advice `yaml:"want,omitempty"`
+}
+
+// RunANTLRAdvisorRuleTest helps to test ANTLR-based SQL review rules.
+// This is similar to advisor.RunSQLReviewRuleTest but for ANTLR advisors.
+func RunANTLRAdvisorRuleTest(t *testing.T, rule advisor.SQLReviewRuleType, dbType storepb.Engine, needMetaData bool, record bool) {
+ var tests []TestCase
+
+ fileName := strings.Map(func(r rune) rune {
+ switch r {
+ case '.', '-':
+ return '_'
+ default:
+ return r
+ }
+ }, string(rule))
+ filepath := filepath.Join("test", fileName+".yaml")
+ yamlFile, err := os.Open(filepath)
+ require.NoError(t, err)
+ defer yamlFile.Close()
+
+ byteValue, err := io.ReadAll(yamlFile)
+ require.NoError(t, err)
+ err = yaml.Unmarshal(byteValue, &tests)
+ require.NoError(t, err, rule)
+
+ sm := sheet.NewManager(nil)
+ for i, tc := range tests {
+ // Add engine types here for mocked database metadata.
+ var schemaMetadata *storepb.DatabaseSchemaMetadata
+ if needMetaData {
+ switch dbType {
+ case storepb.Engine_POSTGRES:
+ schemaMetadata = advisor.MockPostgreSQLDatabase
+ default:
+ t.Fatalf("%s doesn't have mocked metadata support", storepb.Engine_name[int32(dbType)])
+ }
+ }
+
+ database := advisor.MockPostgreSQLDatabase
+ finder := catalog.NewFinder(database, &catalog.FinderContext{CheckIntegrity: true, EngineType: dbType})
+
+ // Get default payload, or use empty string for test-only rules
+ payload, err := advisor.SetDefaultSQLReviewRulePayload(rule, dbType)
+ if err != nil {
+ // For test-only rules (like "hello-world"), use empty payload
+ payload = ""
+ }
+
+ ruleList := []*storepb.SQLReviewRule{
+ {
+ Type: string(rule),
+ Level: storepb.SQLReviewRuleLevel_WARNING,
+ Payload: payload,
+ },
+ }
+
+ checkCtx := advisor.SQLReviewCheckContext{
+ Charset: "",
+ Collation: "",
+ DBType: dbType,
+ Catalog: &testCatalog{finder: finder},
+ Driver: nil,
+ CurrentDatabase: "TEST_DB",
+ DBSchema: schemaMetadata,
+ ChangeType: tc.ChangeType,
+ EnablePriorBackup: true,
+ NoAppendBuiltin: true,
+ UsePostgresDatabaseOwner: true,
+ }
+
+ adviceList, err := advisor.SQLReviewCheck(context.Background(), sm, tc.Statement, ruleList, checkCtx)
+
+ // Sort adviceList by (line, content) for consistent comparison
+ slices.SortFunc(adviceList, func(x, y *storepb.Advice) int {
+ if x.GetStartPosition() == nil || y.GetStartPosition() == nil {
+ if x.GetStartPosition() == nil && y.GetStartPosition() == nil {
+ return 0
+ } else if x.GetStartPosition() == nil {
+ return -1
+ }
+ return 1
+ }
+ if x.GetStartPosition().Line != y.GetStartPosition().Line {
+ if x.GetStartPosition().Line < y.GetStartPosition().Line {
+ return -1
+ }
+ return 1
+ }
+ if x.Content < y.Content {
+ return -1
+ } else if x.Content > y.Content {
+ return 1
+ }
+ return 0
+ })
+
+ require.NoError(t, err)
+ if record {
+ tests[i].Want = adviceList
+ } else {
+ require.Equalf(t, tc.Want, adviceList, "rule: %s, statements: %s", rule, tc.Statement)
+ }
+ }
+
+ if record {
+ err := yamlFile.Close()
+ require.NoError(t, err)
+ byteValue, err := yaml.Marshal(tests)
+ require.NoError(t, err)
+ err = os.WriteFile(filepath, byteValue, 0644)
+ require.NoError(t, err)
+ }
+}
+
+type testCatalog struct {
+ finder *catalog.Finder
+}
+
+func (c *testCatalog) GetFinder() *catalog.Finder {
+ return c.finder
+}