Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor: migrate PostgreSQL parser from pg_query_go to ANTLR in data update executor (#17206)
h3n4l007157536b26
diff --git a/backend/runner/taskrun/data_update_executor.go b/backend/runner/taskrun/data_update_executor.goindex 88e4717ebf..65a92d0557 100644--- a/backend/runner/taskrun/data_update_executor.go+++ b/backend/runner/taskrun/data_update_executor.go@@ -4,25 +4,25 @@ import ("context""fmt""log/slog"+ "strings""time"+ "github.com/antlr4-go/antlr/v4"+ "github.com/bytebase/parser/postgresql""github.com/pkg/errors"- storepb "github.com/bytebase/bytebase/backend/generated-go/store"-"github.com/bytebase/bytebase/backend/common""github.com/bytebase/bytebase/backend/common/log""github.com/bytebase/bytebase/backend/component/config""github.com/bytebase/bytebase/backend/component/dbfactory""github.com/bytebase/bytebase/backend/component/state""github.com/bytebase/bytebase/backend/enterprise"- "github.com/bytebase/bytebase/backend/runner/schemasync"-+ storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/db""github.com/bytebase/bytebase/backend/plugin/db/oracle"parserbase "github.com/bytebase/bytebase/backend/plugin/parser/base"- pgrawparser "github.com/bytebase/bytebase/backend/plugin/parser/pg/legacy"- "github.com/bytebase/bytebase/backend/plugin/parser/pg/legacy/ast"+ "github.com/bytebase/bytebase/backend/plugin/parser/pg"+ "github.com/bytebase/bytebase/backend/runner/schemasync""github.com/bytebase/bytebase/backend/store""github.com/bytebase/bytebase/backend/store/model")@@ -393,21 +393,103 @@ func getPrependStatements(engine storepb.Engine, statement string) (string, erroreturn "", nil}- nodes, err := pgrawparser.Parse(pgrawparser.ParseContext{}, statement)+ parseResult, err := pg.ParsePostgreSQL(statement)if err != nil {return "", errors.Wrap(err, "failed to parse statement")}- for _, node := range nodes {- if n, ok := node.(*ast.VariableSetStmt); ok {- switch n.Name {- case "role", "search_path":- return n.Text(), nil- default:- // Ignore other variable names- }+ visitor := &prependStatementsVisitor{+ statement: statement,+ }+ antlr.ParseTreeWalkerDefault.Walk(visitor, parseResult.Tree)++ return visitor.result, nil+}++// prependStatementsVisitor extracts SET role and search_path statements+type prependStatementsVisitor struct {+ *postgresql.BasePostgreSQLParserListener+ statement string+ result string+}++func (v *prependStatementsVisitor) EnterVariablesetstmt(ctx *postgresql.VariablesetstmtContext) {+ // If we already found a result, don't process more statements+ if v.result != "" {+ return+ }++ setRest := ctx.Set_rest()+ if setRest == nil {+ return+ }+ setRestMore := setRest.Set_rest_more()+ if setRestMore == nil {+ return+ }+ genericSet := setRestMore.Generic_set()+ if genericSet == nil {+ return+ }+ varName := genericSet.Var_name()+ if varName == nil {+ return+ }+ if len(varName.AllColid()) != 1 {+ return+ }++ name := pg.NormalizePostgreSQLColid(varName.Colid(0))+ if name == "role" || name == "search_path" {+ // Extract the text for this SET statement+ v.result = v.extractStatementText(ctx)+ }+}++// extractStatementText extracts the original text for a SET statement context+// This matches pg_query_go behavior: trim leading/trailing whitespace, preserve internal whitespace+func (v *prependStatementsVisitor) extractStatementText(ctx *postgresql.VariablesetstmtContext) string {+ // Extract text from the original statement+ start := ctx.GetStart().GetStart()+ stop := ctx.GetStop().GetStop()++ // Handle potential edge cases with token positions+ if start < 0 || stop < 0 || start >= len(v.statement) {+ return ""+ }++ // Find the semicolon that ends this statement by looking ahead from the stop token+ endPos := stop + 1+ stmtLen := len(v.statement)+ for endPos < stmtLen {+ char := v.statement[endPos]+ if char == ';' {+ // Include the semicolon and any whitespace before it+ stop = endPos+ break+ }+ if char != ' ' && char != '\t' && char != '\n' && char != '\r' {+ // Hit non-whitespace, non-semicolon character, stop looking+ break}+ endPos+++ }++ // Ensure stop doesn't exceed statement length+ if stop >= stmtLen {+ stop = stmtLen - 1+ }++ // Extract the raw text+ text := v.statement[start : stop+1]++ // Match pg_query_go behavior: trim leading and trailing whitespace but preserve internal whitespace+ text = strings.TrimSpace(text)++ // Add semicolon if not present (to match pg_query_go behavior)+ if !strings.HasSuffix(text, ";") {+ text += ";"}- return "", nil+ return text}diff --git a/backend/runner/taskrun/data_update_executor_test.go b/backend/runner/taskrun/data_update_executor_test.gonew file mode 100644index 0000000000..8529e03115--- /dev/null+++ b/backend/runner/taskrun/data_update_executor_test.go@@ -0,0 +1,292 @@+package taskrun++import (+ "testing"++ "github.com/stretchr/testify/require"++ storepb "github.com/bytebase/bytebase/backend/generated-go/store"+)++func TestGetPrependStatements(t *testing.T) {+ tests := []struct {+ name string+ engine storepb.Engine+ statement string+ want string+ wantErr bool+ description string+ }{+ // Non-PostgreSQL engines should return empty string+ {+ name: "mysql engine returns empty",+ engine: storepb.Engine_MYSQL,+ statement: "SET role = 'admin';",+ want: "",+ wantErr: false,+ description: "Non-PostgreSQL engines should always return empty string",+ },+ {+ name: "oracle engine returns empty",+ engine: storepb.Engine_ORACLE,+ statement: "SET role = 'admin';",+ want: "",+ wantErr: false,+ description: "Non-PostgreSQL engines should always return empty string",+ },++ // PostgreSQL role statements+ {+ name: "set role with single quotes",+ engine: storepb.Engine_POSTGRES,+ statement: "SET role = 'admin';",+ want: "SET role = 'admin';",+ wantErr: false,+ description: "Should return the exact text of SET role statement",+ },+ {+ name: "set role without quotes",+ engine: storepb.Engine_POSTGRES,+ statement: "SET role = admin;",+ want: "SET role = admin;",+ wantErr: false,+ description: "Should handle unquoted role names",+ },+ {+ name: "set role with double quotes",+ engine: storepb.Engine_POSTGRES,+ statement: `SET role = "admin_user";`,+ want: `SET role = "admin_user";`,+ wantErr: false,+ description: "Should handle double-quoted role names",+ },+ {+ name: "set role to none",+ engine: storepb.Engine_POSTGRES,+ statement: "SET role = none;",+ want: "SET role = none;",+ wantErr: false,+ description: "Should handle ROLE set to NONE",+ },+ {+ name: "set role with whitespace",+ engine: storepb.Engine_POSTGRES,+ statement: " SET role = 'admin' ; ",+ want: "SET role = 'admin' ;",+ wantErr: false,+ description: "Should preserve internal whitespace but trim leading/trailing",+ },++ // PostgreSQL search_path statements+ {+ name: "set search_path single schema",+ engine: storepb.Engine_POSTGRES,+ statement: "SET search_path = 'public';",+ want: "SET search_path = 'public';",+ wantErr: false,+ description: "Should return search_path statement",+ },+ {+ name: "set search_path multiple schemas",+ engine: storepb.Engine_POSTGRES,+ statement: "SET search_path = schema1, schema2, public;",+ want: "SET search_path = schema1, schema2, public;",+ wantErr: false,+ description: "Should handle multiple schemas in search_path",+ },+ {+ name: "set search_path with quotes",+ engine: storepb.Engine_POSTGRES,+ statement: `SET search_path = "schema with spaces", public;`,+ want: `SET search_path = "schema with spaces", public;`,+ wantErr: false,+ description: "Should handle quoted schema names with spaces",+ },+ {+ name: "set search_path to default",+ engine: storepb.Engine_POSTGRES,+ statement: "SET search_path TO DEFAULT;",+ want: "SET search_path TO DEFAULT;",+ wantErr: false,+ description: "Should handle search_path TO DEFAULT syntax",+ },++ // Multiple statements - should return first matching+ {+ name: "multiple statements with role first",+ engine: storepb.Engine_POSTGRES,+ statement: "SET role = 'admin'; SET search_path = public; SELECT 1;",+ want: "SET role = 'admin';",+ wantErr: false,+ description: "Should return first SET role statement when multiple statements present",+ },+ {+ name: "multiple statements with search_path first",+ engine: storepb.Engine_POSTGRES,+ statement: "SET search_path = public; SET role = 'admin'; SELECT 1;",+ want: "SET search_path = public;",+ wantErr: false,+ description: "Should return first SET search_path statement when multiple statements present",+ },+ {+ name: "multiple statements no role or search_path",+ engine: storepb.Engine_POSTGRES,+ statement: "SET work_mem = '256MB'; SET max_parallel_workers = 4; SELECT 1;",+ want: "",+ wantErr: false,+ description: "Should return empty when no role or search_path statements present",+ },++ // Other SET statements should be ignored+ {+ name: "set timezone ignored",+ engine: storepb.Engine_POSTGRES,+ statement: "SET timezone = 'UTC';",+ want: "",+ wantErr: false,+ description: "SET statements other than role/search_path should be ignored",+ },+ {+ name: "set work_mem ignored",+ engine: storepb.Engine_POSTGRES,+ statement: "SET work_mem = '256MB';",+ want: "",+ wantErr: false,+ description: "SET work_mem should be ignored",+ },+ {+ name: "set statement_timeout ignored",+ engine: storepb.Engine_POSTGRES,+ statement: "SET statement_timeout = 30000;",+ want: "",+ wantErr: false,+ description: "SET statement_timeout should be ignored",+ },++ // Non-SET statements should be ignored+ {+ name: "select statement ignored",+ engine: storepb.Engine_POSTGRES,+ statement: "SELECT * FROM users WHERE role = 'admin';",+ want: "",+ wantErr: false,+ description: "Non-SET statements should be ignored",+ },+ {+ name: "create table ignored",+ engine: storepb.Engine_POSTGRES,+ statement: "CREATE TABLE role (id int, name text);",+ want: "",+ wantErr: false,+ description: "DDL statements should be ignored even if they contain 'role' keyword",+ },+ {+ name: "update statement ignored",+ engine: storepb.Engine_POSTGRES,+ statement: "UPDATE users SET role = 'admin' WHERE id = 1;",+ want: "",+ wantErr: false,+ description: "DML statements should be ignored",+ },++ // Case variations+ {+ name: "uppercase SET ROLE",+ engine: storepb.Engine_POSTGRES,+ statement: "SET ROLE = 'admin';",+ want: "SET ROLE = 'admin';",+ wantErr: false,+ description: "Should handle uppercase keywords",+ },+ {+ name: "mixed case Set Role",+ engine: storepb.Engine_POSTGRES,+ statement: "Set Role = 'admin';",+ want: "Set Role = 'admin';",+ wantErr: false,+ description: "Should handle mixed case keywords",+ },++ // Complex statements that should be parsed correctly+ {+ name: "set role with complex name",+ engine: storepb.Engine_POSTGRES,+ statement: "SET role = 'application_admin_readonly_2024';",+ want: "SET role = 'application_admin_readonly_2024';",+ wantErr: false,+ description: "Should handle complex role names",+ },+ {+ name: "set search_path with current user",+ engine: storepb.Engine_POSTGRES,+ statement: "SET search_path = '$user', public;",+ want: "SET search_path = '$user', public;",+ wantErr: false,+ description: "Should handle special values like $user in search_path",+ },++ // Test pg_query_go node.GetText() behavior with whitespace and formatting+ {+ name: "statement with newlines",+ engine: storepb.Engine_POSTGRES,+ statement: "SET role = \n'admin';",+ want: "SET role = \n'admin';",+ wantErr: false,+ description: "Should preserve newlines in the original statement text",+ },+ {+ name: "statement with tabs",+ engine: storepb.Engine_POSTGRES,+ statement: "SET\trole\t=\t'admin';",+ want: "SET\trole\t=\t'admin';",+ wantErr: false,+ description: "Should preserve tab characters in the original statement text",+ },+ {+ name: "statement with comments",+ engine: storepb.Engine_POSTGRES,+ statement: "SET role = 'admin'; -- Set admin role",+ want: "SET role = 'admin';",+ wantErr: false,+ description: "Should handle statements with comments (pg_query_go behavior)",+ },++ // Error cases+ {+ name: "invalid syntax",+ engine: storepb.Engine_POSTGRES,+ statement: "SET role = ;",+ want: "",+ wantErr: true,+ description: "Should return error for invalid PostgreSQL syntax",+ },+ {+ name: "empty statement",+ engine: storepb.Engine_POSTGRES,+ statement: "",+ want: "",+ wantErr: false,+ description: "Should handle empty statement gracefully",+ },+ {+ name: "whitespace only statement",+ engine: storepb.Engine_POSTGRES,+ statement: " \n\t ",+ want: "",+ wantErr: false,+ description: "Should handle whitespace-only statement gracefully",+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ got, err := getPrependStatements(tt.engine, tt.statement)+ if tt.wantErr {+ require.Error(t, err, "Expected error for test case: %s", tt.description)+ } else {+ require.NoError(t, err, "Unexpected error for test case: %s", tt.description)+ require.Equal(t, tt.want, got, "Test case: %s", tt.description)+ }+ })+ }+}