Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(parser): support CREATE/DROP trigger (#3772)
rebelice230f467f8aa7
diff --git a/plugin/parser/ast/create_trigger_stmt.go b/plugin/parser/ast/create_trigger_stmt.gonew file mode 100644index 0000000000..572d314133--- /dev/null+++ b/plugin/parser/ast/create_trigger_stmt.go@@ -0,0 +1,8 @@+package ast++// CreateTriggerStmt is the definition of create trigger statements.+type CreateTriggerStmt struct {+ ddl++ Trigger *TriggerDef+}diff --git a/plugin/parser/ast/drop_trigger_stmt.go b/plugin/parser/ast/drop_trigger_stmt.gonew file mode 100644index 0000000000..f0c5016cd2--- /dev/null+++ b/plugin/parser/ast/drop_trigger_stmt.go@@ -0,0 +1,10 @@+package ast++// DropTriggerStmt is the definition of drop trigger statements.+type DropTriggerStmt struct {+ ddl++ Trigger *TriggerDef+ IfExists bool+ Behavior DropBehavior+}diff --git a/plugin/parser/ast/trigger_def.go b/plugin/parser/ast/trigger_def.gonew file mode 100644index 0000000000..77539a36ee--- /dev/null+++ b/plugin/parser/ast/trigger_def.go@@ -0,0 +1,9 @@+package ast++// TriggerDef is the definition of trigger.+type TriggerDef struct {+ node++ Name string+ Table *TableDef+}diff --git a/plugin/parser/engine/pg/convert.go b/plugin/parser/engine/pg/convert.goindex 7ac37230b8..2c66555c90 100644--- a/plugin/parser/engine/pg/convert.go+++ b/plugin/parser/engine/pg/convert.go@@ -373,6 +373,48 @@ func convert(node *pgquery.Node, statement parser.SingleSQL) (res ast.Node, err}return dropFunctionStmt, nil+ case pgquery.ObjectType_OBJECT_TRIGGER:+ dropTriggerStmt := &ast.DropTriggerStmt{+ IfExists: in.DropStmt.MissingOk,+ Behavior: convertDropBehavior(in.DropStmt.Behavior),+ }++ if len(in.DropStmt.Objects) != 1 {+ return nil, parser.NewConvertErrorf("expected one trigger but found %d", len(in.DropStmt.Objects))+ }+ listNode, ok := in.DropStmt.Objects[0].Node.(*pgquery.Node_List)+ if !ok {+ return nil, parser.NewConvertErrorf("expected List but found %d", in.DropStmt.Objects[0].Node)+ }++ list, err := convertListToStringList(listNode)+ if err != nil {+ return nil, err+ }+ switch len(list) {+ case 3:+ dropTriggerStmt.Trigger = &ast.TriggerDef{+ Name: list[2],+ Table: &ast.TableDef{+ Type: ast.TableTypeUnknown,+ Schema: list[0],+ Name: list[1],+ },+ }+ case 2:+ dropTriggerStmt.Trigger = &ast.TriggerDef{+ Name: list[1],+ Table: &ast.TableDef{+ Type: ast.TableTypeUnknown,+ Schema: "",+ Name: list[0],+ },+ }+ default:+ return nil, parser.NewConvertErrorf("expected one or two but found %d", len(list))+ }++ return dropTriggerStmt, nil}case *pgquery.Node_DropdbStmt:return &ast.DropDatabaseStmt{@@ -629,6 +671,15 @@ func convert(node *pgquery.Node, statement parser.SingleSQL) (res ast.Node, err}return &ast.CreateFunctionStmt{Function: functionDef}, nil+ case *pgquery.Node_CreateTrigStmt:+ createTriggerStmt := &ast.CreateTriggerStmt{+ Trigger: &ast.TriggerDef{+ Name: in.CreateTrigStmt.Trigname,+ Table: convertRangeVarToTableName(in.CreateTrigStmt.Relation, ast.TableTypeUnknown),+ },+ }++ return createTriggerStmt, nildefault:return &ast.UnconvertedStmt{}, nil}diff --git a/plugin/parser/engine/pg/convert_test.go b/plugin/parser/engine/pg/convert_test.goindex ee1f280ea6..93c6819b85 100644--- a/plugin/parser/engine/pg/convert_test.go+++ b/plugin/parser/engine/pg/convert_test.go@@ -2813,3 +2813,61 @@ func TestDropFunction(t *testing.T) {runTests(t, tests)}++func TestCreateTrigger(t *testing.T) {+ tests := []testData{+ {+ stmt: `CREATE TRIGGER update_principal_updated_ts BEFORE UPDATE ON public.principal FOR EACH ROW EXECUTE FUNCTION public.trigger_update_updated_ts();`,+ want: []ast.Node{+ &ast.CreateTriggerStmt{+ Trigger: &ast.TriggerDef{+ Name: "update_principal_updated_ts",+ Table: &ast.TableDef{+ Type: ast.TableTypeUnknown,+ Schema: "public",+ Name: "principal",+ },+ },+ },+ },+ statementList: []parser.SingleSQL{+ {+ Text: `CREATE TRIGGER update_principal_updated_ts BEFORE UPDATE ON public.principal FOR EACH ROW EXECUTE FUNCTION public.trigger_update_updated_ts();`,+ LastLine: 1,+ },+ },+ },+ }++ runTests(t, tests)+}++func TestDropTrigger(t *testing.T) {+ tests := []testData{+ {+ stmt: `DROP TRIGGER update_ts ON public.principal`,+ want: []ast.Node{+ &ast.DropTriggerStmt{+ IfExists: false,+ Behavior: ast.DropBehaviorRestrict,+ Trigger: &ast.TriggerDef{+ Name: "update_ts",+ Table: &ast.TableDef{+ Type: ast.TableTypeUnknown,+ Schema: "public",+ Name: "principal",+ },+ },+ },+ },+ statementList: []parser.SingleSQL{+ {+ Text: `DROP TRIGGER update_ts ON public.principal`,+ LastLine: 1,+ },+ },+ },+ }++ runTests(t, tests)+}