Skip to content

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

chore: add ConvertError (#1722)

rebelice85a148435e45
diff --git a/plugin/advisor/pg/advisor_syntax.go b/plugin/advisor/pg/advisor_syntax.go
index 2aa7a77883..1c0de41125 100644
--- a/plugin/advisor/pg/advisor_syntax.go
+++ b/plugin/advisor/pg/advisor_syntax.go
@@ -18,16 +18,27 @@ type SyntaxAdvisor struct {
// Check parses the given statement and checks for errors.
func (adv *SyntaxAdvisor) Check(ctx advisor.Context, statement string) ([]advisor.Advice, error) {
+ var res []advisor.Advice
if _, errAdvice := parseStatement(statement); errAdvice != nil {
- return errAdvice, nil
+ for _, advice := range errAdvice {
+ // Here is to filter parser.ConvertError.
+ // The reason for this is to remove potential conversion errors from the syntax check.
+ // Syntax check doesn't actually require the transformed AST either.
+ // TODO(rebelice): remove it when conversion function is complete.
+ if advice.Code == advisor.StatementSyntaxError {
+ res = append(res, advice)
+ }
+ }
}
- return []advisor.Advice{
- {
+ if len(res) == 0 {
+ res = append(res, advisor.Advice{
Status: advisor.Success,
Code: advisor.Ok,
Title: "Syntax OK",
Content: "OK",
- },
- }, nil
+ })
+ }
+
+ return res, nil
}
diff --git a/plugin/advisor/pg/parser.go b/plugin/advisor/pg/parser.go
index 48dee2c7e1..a1ee4de855 100644
--- a/plugin/advisor/pg/parser.go
+++ b/plugin/advisor/pg/parser.go
@@ -9,6 +9,16 @@ import (
func parseStatement(statement string) ([]ast.Node, []advisor.Advice) {
nodes, err := parser.Parse(parser.Postgres, parser.Context{}, statement)
if err != nil {
+ if _, ok := err.(*parser.ConvertError); ok {
+ return nil, []advisor.Advice{
+ {
+ Status: advisor.Error,
+ Code: advisor.Internal,
+ Title: "Parser conversion error",
+ Content: err.Error(),
+ },
+ }
+ }
return nil, []advisor.Advice{
{
Status: advisor.Error,
diff --git a/plugin/parser/engine/pg/convert.go b/plugin/parser/engine/pg/convert.go
index d35e7f2c3e..98d3533565 100644
--- a/plugin/parser/engine/pg/convert.go
+++ b/plugin/parser/engine/pg/convert.go
@@ -1,8 +1,7 @@
package pg
import (
- "fmt"
-
+ "github.com/bytebase/bytebase/plugin/parser"
"github.com/bytebase/bytebase/plugin/parser/ast"
pgquery "github.com/pganalyze/pg_query_go/v2"
)
@@ -23,7 +22,7 @@ func convert(node *pgquery.Node) (ast.Node, error) {
case pgquery.AlterTableType_AT_AddColumn:
def, ok := alterCmd.Def.Node.(*pgquery.Node_ColumnDef)
if !ok {
- return nil, fmt.Errorf("expected ColumnDef but found %t", alterCmd.Def.Node)
+ return nil, parser.NewConvertErrorf("expected ColumnDef but found %t", alterCmd.Def.Node)
}
addColumn := &ast.AddColumnListStmt{
@@ -59,7 +58,7 @@ func convert(node *pgquery.Node) (ast.Node, error) {
for _, cons := range item.ColumnDef.Constraints {
constraint, ok := cons.Node.(*pgquery.Node_Constraint)
if !ok {
- return nil, fmt.Errorf("expected Constraint but found %t", cons.Node)
+ return nil, parser.NewConvertErrorf("expected Constraint but found %t", cons.Node)
}
columnCons, err := convertConstraint(constraint)
if err != nil {
@@ -115,7 +114,7 @@ func convertConstraint(in *pgquery.Node_Constraint) (*ast.ConstraintDef, error)
for _, key := range in.Constraint.Keys {
name, ok := key.Node.(*pgquery.Node_String_)
if !ok {
- return nil, fmt.Errorf("expected String but found %t", key.Node)
+ return nil, parser.NewConvertErrorf("expected String but found %t", key.Node)
}
cons.KeyList = append(cons.KeyList, name.String_.Str)
}
@@ -128,7 +127,7 @@ func convertConstraint(in *pgquery.Node_Constraint) (*ast.ConstraintDef, error)
for _, item := range in.Constraint.PkAttrs {
name, ok := item.Node.(*pgquery.Node_String_)
if !ok {
- return nil, fmt.Errorf("expected String but found %t", item.Node)
+ return nil, parser.NewConvertErrorf("expected String but found %t", item.Node)
}
cons.Foreign.ColumnList = append(cons.Foreign.ColumnList, name.String_.Str)
}
@@ -136,7 +135,7 @@ func convertConstraint(in *pgquery.Node_Constraint) (*ast.ConstraintDef, error)
for _, item := range in.Constraint.FkAttrs {
name, ok := item.Node.(*pgquery.Node_String_)
if !ok {
- return nil, fmt.Errorf("expected String but found %t", item.Node)
+ return nil, parser.NewConvertErrorf("expected String but found %t", item.Node)
}
cons.KeyList = append(cons.KeyList, name.String_.Str)
}
diff --git a/plugin/parser/error.go b/plugin/parser/error.go
new file mode 100644
index 0000000000..1f7fa19097
--- /dev/null
+++ b/plugin/parser/error.go
@@ -0,0 +1,22 @@
+package parser
+
+import "fmt"
+
+// ConvertError is the conversion error.
+// Because this module is still in its early stages and lacks sufficient testing to ensure compatibility,
+// we add ConvertError to differentiate between syntax errors and conversion errors.
+// We will remove it in the future.
+// TODO(rebelice): remove it.
+type ConvertError struct {
+ err error
+}
+
+// Error implements the error interface
+func (e *ConvertError) Error() string {
+ return e.err.Error()
+}
+
+// NewConvertErrorf returns the new conversion error.
+func NewConvertErrorf(format string, a ...interface{}) *ConvertError {
+ return &ConvertError{err: fmt.Errorf(format, a...)}
+}