Skip to content

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

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.go
index 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 - ALTER
case *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.yaml
index 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:
- COMMENT
diff --git a/backend/plugin/parser/redshift/statement_type.go b/backend/plugin/parser/redshift/statement_type.go
index 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.Dropdbs
c.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 statements
func (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.go
new file mode 100644
index 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)
+ })
+ }
+}