Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix(parser): classify truncate statements (#20310)
rebelice79d74a2c54e8
diff --git a/backend/plugin/parser/pg/statement_type_antlr.go b/backend/plugin/parser/pg/statement_type_antlr.goindex dfbbf2504e..0430f12df9 100644--- a/backend/plugin/parser/pg/statement_type_antlr.go+++ b/backend/plugin/parser/pg/statement_type_antlr.go@@ -41,6 +41,8 @@ func classifyStatementType(node ast.Node) storepb.StatementType {return getDropStatementTypeFromOmni(n)case *ast.DropdbStmt:return storepb.StatementType_DROP_DATABASE+ case *ast.TruncateStmt:+ return storepb.StatementType_TRUNCATE// DDL - ALTERcase *ast.AlterTableStmt:diff --git a/backend/plugin/parser/pg/test-data/test_statement_type.yaml b/backend/plugin/parser/pg/test-data/test_statement_type.yamlindex c24056b5f0..17a05c94f8 100644--- a/backend/plugin/parser/pg/test-data/test_statement_type.yaml+++ b/backend/plugin/parser/pg/test-data/test_statement_type.yaml@@ -88,6 +88,9 @@- statement: DELETE FROM t1 WHERE id = 1;want:- DELETE+- statement: TRUNCATE TABLE t1;+ want:+ - TRUNCATE- statement: COMMENT ON TABLE t1 IS 'test table';want:- COMMENTdiff --git a/backend/plugin/parser/redshift/statement_type.go b/backend/plugin/parser/redshift/statement_type.goindex 5f15dfa004..3ff356d332 100644--- a/backend/plugin/parser/redshift/statement_type.go+++ b/backend/plugin/parser/redshift/statement_type.go@@ -268,6 +268,13 @@ func (c *statementTypeCollectorWithPosition) EnterDropdbstmt(ctx *parser.Dropdbsc.addStatement(storepb.StatementType_DROP_DATABASE, ctx)}+func (c *statementTypeCollectorWithPosition) EnterTruncatestmt(ctx *parser.TruncatestmtContext) {+ if !isTopLevel(ctx.GetParent()) {+ return+ }+ c.addStatement(storepb.StatementType_TRUNCATE, ctx)+}+// DROP SCHEMA statementsfunc (c *statementTypeCollectorWithPosition) EnterDropschemastmt(ctx *parser.DropschemastmtContext) {if !isTopLevel(ctx.GetParent()) {diff --git a/backend/plugin/parser/redshift/statement_type_test.go b/backend/plugin/parser/redshift/statement_type_test.gonew file mode 100644index 0000000000..36ca574528--- /dev/null+++ b/backend/plugin/parser/redshift/statement_type_test.go@@ -0,0 +1,37 @@+package redshift++import (+ "testing"++ "github.com/stretchr/testify/require"++ storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/plugin/parser/base"+)++func TestGetStatementTypes(t *testing.T) {+ tests := []struct {+ name string+ statement string+ want []storepb.StatementType+ }{+ {+ name: "truncate table",+ statement: "TRUNCATE TABLE t1;",+ want: []storepb.StatementType{+ storepb.StatementType_TRUNCATE,+ },+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ stmts, err := base.ParseStatements(storepb.Engine_REDSHIFT, tt.statement)+ require.NoError(t, err)++ got, err := GetStatementTypes(base.ExtractASTs(stmts))+ require.NoError(t, err)+ require.Equal(t, tt.want, got)+ })+ }+}