Skip to content

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

feat(sql-review, snowflake): add sql review rule statement select no select all for snowflake (#7006)

h3n4lb3021e0d11f7
diff --git a/backend/plugin/advisor/advisor.go b/backend/plugin/advisor/advisor.go
index 7b07c6db0e..afcec368c9 100644
--- a/backend/plugin/advisor/advisor.go
+++ b/backend/plugin/advisor/advisor.go
@@ -429,6 +429,9 @@ const (
// SnowflakeNoSelectAll is an advisor type for Snowflake no select all.
SnowflakeNoSelectAll Type = "bb.plugin.advisor.snowflake.select.no-select-all"
+
+ // SnowflakeTableDropNamingConvention is an advisor type for Snowflake table drop with naming convention.
+ SnowflakeTableDropNamingConvention Type = "bb.plugin.advisor.snowflake.table.drop-naming-convention"
)
// Advice is the result of an advisor.
diff --git a/backend/plugin/advisor/snowflake/advisor_table_drop_naming_convention.go b/backend/plugin/advisor/snowflake/advisor_table_drop_naming_convention.go
new file mode 100644
index 0000000000..5a691160c6
--- /dev/null
+++ b/backend/plugin/advisor/snowflake/advisor_table_drop_naming_convention.go
@@ -0,0 +1,92 @@
+// Package snowflake is the advisor for snowflake database.
+package snowflake
+
+import (
+ "fmt"
+ "regexp"
+
+ "github.com/antlr4-go/antlr/v4"
+ parser "github.com/bytebase/snowsql-parser"
+ "github.com/pkg/errors"
+
+ "github.com/bytebase/bytebase/backend/plugin/advisor"
+ "github.com/bytebase/bytebase/backend/plugin/advisor/db"
+ bbparser "github.com/bytebase/bytebase/backend/plugin/parser/sql"
+)
+
+var (
+ _ advisor.Advisor = (*TableDropNamingConventionAdvisor)(nil)
+)
+
+func init() {
+ advisor.Register(db.Snowflake, advisor.SnowflakeTableDropNamingConvention, &TableDropNamingConventionAdvisor{})
+}
+
+// TableDropNamingConventionAdvisor is the advisor checking for table drop with naming convention.
+type TableDropNamingConventionAdvisor struct {
+}
+
+// Check checks for table drop with naming convention.
+func (*TableDropNamingConventionAdvisor) Check(ctx advisor.Context, _ string) ([]advisor.Advice, error) {
+ tree, ok := ctx.AST.(antlr.Tree)
+ if !ok {
+ return nil, errors.Errorf("failed to convert to Tree")
+ }
+
+ level, err := advisor.NewStatusBySQLReviewRuleLevel(ctx.Rule.Level)
+ if err != nil {
+ return nil, err
+ }
+
+ format, _, err := advisor.UnmarshalNamingRulePayloadAsRegexp(ctx.Rule.Payload)
+ if err != nil {
+ return nil, err
+ }
+
+ listener := &tableDropNamingConventionChecker{
+ level: level,
+ title: string(ctx.Rule.Type),
+ format: format,
+ }
+
+ antlr.ParseTreeWalkerDefault.Walk(listener, tree)
+
+ return listener.generateAdvice()
+}
+
+// tableDropNamingConventionChecker is the listener for table drop with naming convention.
+type tableDropNamingConventionChecker struct {
+ *parser.BaseSnowflakeParserListener
+
+ level advisor.Status
+ title string
+ format *regexp.Regexp
+
+ adviceList []advisor.Advice
+}
+
+// generateAdvice returns the advices generated by the listener, the advices must not be empty.
+func (l *tableDropNamingConventionChecker) generateAdvice() ([]advisor.Advice, error) {
+ if len(l.adviceList) == 0 {
+ l.adviceList = append(l.adviceList, advisor.Advice{
+ Status: advisor.Success,
+ Code: advisor.Ok,
+ Title: "OK",
+ Content: "",
+ })
+ }
+ return l.adviceList, nil
+}
+
+func (l *tableDropNamingConventionChecker) EnterDrop_table(ctx *parser.Drop_tableContext) {
+ normalizedObjectName := bbparser.NormalizeObjectNamePart(ctx.Object_name().GetO())
+ if !l.format.MatchString(normalizedObjectName) {
+ l.adviceList = append(l.adviceList, advisor.Advice{
+ Status: l.level,
+ Code: advisor.TableDropNamingConventionMismatch,
+ Title: l.title,
+ Content: fmt.Sprintf("%q mismatches drop table naming convention, naming format should be %q", normalizedObjectName, l.format),
+ Line: ctx.Object_name().GetO().GetStart().GetLine(),
+ })
+ }
+}
diff --git a/backend/plugin/advisor/snowflake/snowflake_rules_test.go b/backend/plugin/advisor/snowflake/snowflake_rules_test.go
index b2efb124a3..589fbe3e9b 100644
--- a/backend/plugin/advisor/snowflake/snowflake_rules_test.go
+++ b/backend/plugin/advisor/snowflake/snowflake_rules_test.go
@@ -21,6 +21,7 @@ func TestSnowflakeRules(t *testing.T) {
advisor.SchemaRuleIdentifierCase,
advisor.SchemaRuleColumnNotNull,
advisor.SchemaRuleStatementNoSelectAll,
+ advisor.SchemaRuleTableDropNamingConvention,
}
for _, rule := range snowflakeRules {
diff --git a/backend/plugin/advisor/snowflake/test/table_drop_naming_convention.yaml b/backend/plugin/advisor/snowflake/test/table_drop_naming_convention.yaml
new file mode 100644
index 0000000000..3e3fccce27
--- /dev/null
+++ b/backend/plugin/advisor/snowflake/test/table_drop_naming_convention.yaml
@@ -0,0 +1,16 @@
+- statement: DROP TABLE IF EXISTS "FOO_delete"
+ want:
+ - status: SUCCESS
+ code: 0
+ title: OK
+ content: ""
+ line: 0
+ details: ""
+- statement: DROP TABLE IF EXISTS FOO
+ want:
+ - status: WARN
+ code: 603
+ title: table.drop-naming-convention
+ content: '"FOO" mismatches drop table naming convention, naming format should be "_delete$"'
+ line: 1
+ details: ""
diff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.go
index 20c1ed9258..45ebe568ea 100644
--- a/backend/plugin/advisor/sql_review.go
+++ b/backend/plugin/advisor/sql_review.go
@@ -1259,6 +1259,8 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, err
return MySQLTableDropNamingConvention, nil
case db.Postgres:
return PostgreSQLTableDropNamingConvention, nil
+ case db.Snowflake:
+ return SnowflakeTableDropNamingConvention, nil
}
case SchemaRuleTableCommentConvention:
switch engine {