Skip to content

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

feat: skeleton code for checking PostgreSQL require PK (#1754)

tianzhou59fb57b6c4d2
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.go
index 1ab3a48cf3..8e80e45c71 100644
--- a/plugin/advisor/advisor.go
+++ b/plugin/advisor/advisor.go
@@ -109,6 +109,9 @@ const (
// PostgreSQLNamingColumnConvention is an advisor type for PostgreSQL column naming convention.
PostgreSQLNamingColumnConvention Type = "bb.plugin.advisor.postgresql.naming.column"
+
+ // PostgreSQLTableRequirePK is an advisor type for PostgreSQL table require primary key.
+ PostgreSQLTableRequirePK Type = "bb.plugin.advisor.postgresql.table.require-pk"
)
// Advice is the result of an advisor.
diff --git a/plugin/advisor/pg/advisor_table_require_pk.go b/plugin/advisor/pg/advisor_table_require_pk.go
new file mode 100644
index 0000000000..47117f062d
--- /dev/null
+++ b/plugin/advisor/pg/advisor_table_require_pk.go
@@ -0,0 +1,53 @@
+package pg
+
+import (
+ "github.com/bytebase/bytebase/plugin/advisor"
+ "github.com/bytebase/bytebase/plugin/parser/ast"
+)
+
+var (
+ _ advisor.Advisor = (*TableRequirePKAdvisor)(nil)
+)
+
+func init() {
+ advisor.Register(advisor.Postgres, advisor.PostgreSQLTableRequirePK, &TableRequirePKAdvisor{})
+}
+
+// TableRequirePKAdvisor is the advisor checking table requires PK.
+type TableRequirePKAdvisor struct {
+}
+
+// Check parses the given statement and checks for errors.
+func (adv *TableRequirePKAdvisor) Check(ctx advisor.Context, statement string) ([]advisor.Advice, error) {
+ stmts, errAdvice := parseStatement(statement)
+ if errAdvice != nil {
+ return errAdvice, nil
+ }
+
+ _, err := advisor.NewStatusBySchemaReviewRuleLevel(ctx.Rule.Level)
+ if err != nil {
+ return nil, err
+ }
+
+ checker := &tableRequirePKChecker{}
+
+ for _, stmt := range stmts {
+ ast.Walk(checker, stmt)
+ }
+
+ return []advisor.Advice{}, nil
+}
+
+// TODO(rebelice): fill the implementation to check table PK.
+type tableRequirePKChecker struct {
+ // adviceList []advisor.Advice
+ // level advisor.Status
+ // title string
+ // tables: make(tablePK),
+ // catalog catalog.Catalog
+}
+
+// Visit implements the ast.Visitor interface.
+func (checker *tableRequirePKChecker) Visit(node ast.Node) ast.Visitor {
+ return checker
+}
diff --git a/plugin/advisor/pg/advisor_table_require_pk_test.go b/plugin/advisor/pg/advisor_table_require_pk_test.go
new file mode 100644
index 0000000000..903ac8adb7
--- /dev/null
+++ b/plugin/advisor/pg/advisor_table_require_pk_test.go
@@ -0,0 +1,22 @@
+package pg
+
+import (
+ "testing"
+
+ "github.com/bytebase/bytebase/plugin/advisor"
+)
+
+func TestRequirePK(t *testing.T) {
+ tests := []advisor.TestCase{
+ {
+ Statement: "CREATE TABLE t(id INT PRIMARY KEY)",
+ Want: []advisor.Advice{},
+ },
+ }
+
+ advisor.RunSchemaReviewRuleTests(t, tests, &TableRequirePKAdvisor{}, &advisor.SchemaReviewRule{
+ Type: advisor.SchemaRuleTableRequirePK,
+ Level: advisor.SchemaRuleLevelError,
+ Payload: "",
+ }, &advisor.MockCatalogService{})
+}