Skip to content

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

refactor: remove pg_query_go usage in cockroachdb sync (#17202)

h3n4l14b6a10cb06f
diff --git a/backend/plugin/db/cockroachdb/cockroachdb.go b/backend/plugin/db/cockroachdb/cockroachdb.go
index 2cf0ea275b..731318e9b2 100644
--- a/backend/plugin/db/cockroachdb/cockroachdb.go
+++ b/backend/plugin/db/cockroachdb/cockroachdb.go
@@ -20,8 +20,6 @@ import (
"golang.org/x/crypto/ssh"
"google.golang.org/protobuf/types/known/durationpb"
- pgquery "github.com/pganalyze/pg_query_go/v6"
-
"github.com/bytebase/bytebase/backend/common"
"github.com/bytebase/bytebase/backend/common/log"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
@@ -30,6 +28,7 @@ import (
"github.com/bytebase/bytebase/backend/plugin/db/util"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
crdbparser "github.com/bytebase/bytebase/backend/plugin/parser/cockroachdb"
+ pgparser "github.com/bytebase/bytebase/backend/plugin/parser/pg"
)
var (
@@ -283,7 +282,7 @@ func (d *Driver) Execute(ctx context.Context, statement string, opts db.ExecuteO
// we should execute it as a single statement without transaction.
// If the statement is a PL/pgSQL block, we should execute it as a single statement.
// https://www.postgresql.org/docs/current/plpgsql-control-structures.html
- if len(singleSQLs) == 1 && isPlSQLBlock(singleSQLs[0]) {
+ if len(singleSQLs) == 1 && pgparser.IsPlSQLBlock(singleSQLs[0]) {
isPlsql = true
}
@@ -754,20 +753,3 @@ func getStatementWithResultLimit(stmt string, limit int) string {
// Add two new line symbol here.
return fmt.Sprintf("WITH result AS (\n%s\n) SELECT * FROM result LIMIT %d;", util.TrimStatement(stmt), limit)
}
-
-func isPlSQLBlock(stmt string) bool {
- tree, err := pgquery.Parse(stmt)
- if err != nil {
- return false
- }
-
- if len(tree.Stmts) != 1 {
- return false
- }
-
- if _, ok := tree.Stmts[0].Stmt.Node.(*pgquery.Node_DoStmt); ok {
- return true
- }
-
- return false
-}