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: add TableType for TableDef (#2069)
rebelice445b90fdf00d
diff --git a/plugin/parser/ast/table_def.go b/plugin/parser/ast/table_def.goindex 4c6fcf6b51..cedab3da2c 100644--- a/plugin/parser/ast/table_def.go+++ b/plugin/parser/ast/table_def.go@@ -1,9 +1,23 @@package ast+// TableType is the type for table.+type TableType int++const (+ // TableTypeNone is the type for table or view, which means we can't be sure of it.+ TableTypeNone TableType = iota+ // TableTypeBaseTable is the type for table.+ TableTypeBaseTable+ // TableTypeView is the type for view.+ TableTypeView+)+// TableDef is the strcut for table.type TableDef struct {node+ // Type is the table type for table: base table or view.+ Type TableType// Database is the name of database.// It's also called "catalog" in PostgreSQL.Database stringdiff --git a/plugin/parser/engine/pg/convert.go b/plugin/parser/engine/pg/convert.goindex 4ddb5c0f0a..ee3c1389ab 100644--- a/plugin/parser/engine/pg/convert.go+++ b/plugin/parser/engine/pg/convert.go@@ -18,7 +18,7 @@ func convert(node *pgquery.Node, text string) (res ast.Node, err error) {switch in := node.Node.(type) {case *pgquery.Node_AlterTableStmt:alterTable := &ast.AlterTableStmt{- Table: convertRangeVarToTableName(in.AlterTableStmt.Relation),+ Table: convertRangeVarToTableName(in.AlterTableStmt.Relation, ast.TableTypeBaseTable),AlterItemList: []ast.Node{},}for _, cmd := range in.AlterTableStmt.Cmds {@@ -94,7 +94,7 @@ func convert(node *pgquery.Node, text string) (res ast.Node, err error) {case *pgquery.Node_CreateStmt:table := &ast.CreateTableStmt{IfNotExists: in.CreateStmt.IfNotExists,- Name: convertRangeVarToTableName(in.CreateStmt.Relation),+ Name: convertRangeVarToTableName(in.CreateStmt.Relation, ast.TableTypeBaseTable),}for _, elt := range in.CreateStmt.TableElts {@@ -117,32 +117,36 @@ func convert(node *pgquery.Node, text string) (res ast.Node, err error) {case *pgquery.Node_RenameStmt:switch in.RenameStmt.RenameType {case pgquery.ObjectType_OBJECT_COLUMN:+ tableType, err := convertToTableType(in.RenameStmt.RelationType)+ if err != nil {+ return nil, err+ }return &ast.RenameColumnStmt{- Table: convertRangeVarToTableName(in.RenameStmt.Relation),+ Table: convertRangeVarToTableName(in.RenameStmt.Relation, tableType),ColumnName: in.RenameStmt.Subname,NewName: in.RenameStmt.Newname,}, nilcase pgquery.ObjectType_OBJECT_TABLE:return &ast.RenameTableStmt{- Table: convertRangeVarToTableName(in.RenameStmt.Relation),+ Table: convertRangeVarToTableName(in.RenameStmt.Relation, ast.TableTypeBaseTable),NewName: in.RenameStmt.Newname,}, nilcase pgquery.ObjectType_OBJECT_TABCONSTRAINT:return &ast.RenameConstraintStmt{- Table: convertRangeVarToTableName(in.RenameStmt.Relation),+ Table: convertRangeVarToTableName(in.RenameStmt.Relation, ast.TableTypeBaseTable),ConstraintName: in.RenameStmt.Subname,NewName: in.RenameStmt.Newname,}, nilcase pgquery.ObjectType_OBJECT_INDEX:return &ast.RenameIndexStmt{- Table: convertRangeVarToIndexTableName(in.RenameStmt.Relation),+ Table: convertRangeVarToIndexTableName(in.RenameStmt.Relation, ast.TableTypeNone),IndexName: in.RenameStmt.Relation.Relname,NewName: in.RenameStmt.Newname,}, nil}case *pgquery.Node_IndexStmt:indexDef := &ast.IndexDef{- Table: convertRangeVarToTableName(in.IndexStmt.Relation),+ Table: convertRangeVarToTableName(in.IndexStmt.Relation, ast.TableTypeNone),Name: in.IndexStmt.Idxname,Unique: in.IndexStmt.Unique,}@@ -176,7 +180,7 @@ func convert(node *pgquery.Node, text string) (res ast.Node, err error) {if !ok {return nil, parser.NewConvertErrorf("expected List but found %t", object.Node)}- indexDef, err := convertListToIndexDef(list)+ indexDef, err := convertListToIndexDef(list, ast.TableTypeNone)if err != nil {return nil, err}@@ -190,7 +194,7 @@ func convert(node *pgquery.Node, text string) (res ast.Node, err error) {if !ok {return nil, parser.NewConvertErrorf("expected List but found %t", object.Node)}- tableDef, err := convertListToTableDef(list)+ tableDef, err := convertListToTableDef(list, ast.TableTypeBaseTable)if err != nil {return nil, err}@@ -404,7 +408,7 @@ func convertSetOperation(t pgquery.SetOperation) (ast.SetOperationType, error) {}}-func convertListToTableDef(in *pgquery.Node_List) (*ast.TableDef, error) {+func convertListToTableDef(in *pgquery.Node_List, tableType ast.TableType) (*ast.TableDef, error) {stringList, err := convertListToStringList(in)if err != nil {return nil, err@@ -412,17 +416,21 @@ func convertListToTableDef(in *pgquery.Node_List) (*ast.TableDef, error) {switch len(in.List.Items) {case 2:return &ast.TableDef{+ Type: tableType,Schema: stringList[0],Name: stringList[1],}, nilcase 1:- return &ast.TableDef{Name: stringList[0]}, nil+ return &ast.TableDef{+ Type: tableType,+ Name: stringList[0],+ }, nildefault:return nil, parser.NewConvertErrorf("expected length is 1 or 2, but found %d", len(in.List.Items))}}-func convertListToIndexDef(in *pgquery.Node_List) (*ast.IndexDef, error) {+func convertListToIndexDef(in *pgquery.Node_List, tableType ast.TableType) (*ast.IndexDef, error) {stringList, err := convertListToStringList(in)if err != nil {return nil, err@@ -430,7 +438,10 @@ func convertListToIndexDef(in *pgquery.Node_List) (*ast.IndexDef, error) {indexDef := &ast.IndexDef{}switch len(in.List.Items) {case 2:- indexDef.Table = &ast.TableDef{Schema: stringList[0]}+ indexDef.Table = &ast.TableDef{+ Type: tableType,+ Schema: stringList[0],+ }indexDef.Name = stringList[1]case 1:indexDef.Name = stringList[0]@@ -452,16 +463,18 @@ func convertListToStringList(in *pgquery.Node_List) ([]string, error) {return res, nil}-func convertRangeVarToTableName(in *pgquery.RangeVar) *ast.TableDef {+func convertRangeVarToTableName(in *pgquery.RangeVar, tableType ast.TableType) *ast.TableDef {return &ast.TableDef{+ Type: tableType,Database: in.Catalogname,Schema: in.Schemaname,Name: in.Relname,}}-func convertRangeVarToIndexTableName(in *pgquery.RangeVar) *ast.TableDef {+func convertRangeVarToIndexTableName(in *pgquery.RangeVar, tableType ast.TableType) *ast.TableDef {return &ast.TableDef{+ Type: tableType,Schema: in.Schemaname,}}@@ -484,7 +497,7 @@ func convertConstraint(in *pgquery.Node_Constraint) (*ast.ConstraintDef, error)}case ast.ConstraintTypeForeign:cons.Foreign = &ast.ForeignDef{- Table: convertRangeVarToTableName(in.Constraint.Pktable),+ Table: convertRangeVarToTableName(in.Constraint.Pktable, ast.TableTypeBaseTable),}for _, item := range in.Constraint.PkAttrs {@@ -560,3 +573,14 @@ func convertColumnDef(in *pgquery.Node_ColumnDef) (*ast.ColumnDef, error) {return column, nil}++func convertToTableType(relationType pgquery.ObjectType) (ast.TableType, error) {+ switch relationType {+ case pgquery.ObjectType_OBJECT_TABLE:+ return ast.TableTypeBaseTable, nil+ case pgquery.ObjectType_OBJECT_VIEW:+ return ast.TableTypeView, nil+ default:+ return ast.TableTypeNone, parser.NewConvertErrorf("expected TABLE or VIEW but found %s", relationType)+ }+}diff --git a/plugin/parser/engine/pg/convert_test.go b/plugin/parser/engine/pg/convert_test.goindex 2a6b1d26db..0bab199806 100644--- a/plugin/parser/engine/pg/convert_test.go+++ b/plugin/parser/engine/pg/convert_test.go@@ -35,6 +35,7 @@ func TestPGConvertCreateTableStmt(t *testing.T) {&ast.CreateTableStmt{IfNotExists: false,Name: &ast.TableDef{+ Type: ast.TableTypeBaseTable,Name: "techBook",},ColumnList: []*ast.ColumnDef{@@ -70,6 +71,7 @@ func TestPGConvertCreateTableStmt(t *testing.T) {&ast.CreateTableStmt{IfNotExists: true,Name: &ast.TableDef{+ Type: ast.TableTypeBaseTable,Name: "techbook",},ColumnList: []*ast.ColumnDef{@@ -87,6 +89,7 @@ func TestPGConvertCreateTableStmt(t *testing.T) {want: []ast.Node{&ast.CreateTableStmt{Name: &ast.TableDef{+ Type: ast.TableTypeBaseTable,Name: "tech_book",},ColumnList: []*ast.ColumnDef{@@ -112,6 +115,7 @@ func TestPGConvertCreateTableStmt(t *testing.T) {want: []ast.Node{&ast.CreateTableStmt{Name: &ast.TableDef{+ Type: ast.TableTypeBaseTable,Name: "tech_book",},ColumnList: []*ast.ColumnDef{@@ -147,6 +151,7 @@ func TestPGConvertCreateTableStmt(t *testing.T) {want: []ast.Node{&ast.CreateTableStmt{Name: &ast.TableDef{+ Type: ast.TableTypeBaseTable,Name: "tech_book",},ColumnList: []*ast.ColumnDef{@@ -158,7 +163,10 @@ func TestPGConvertCreateTableStmt(t *testing.T) {Type: ast.ConstraintTypeForeign,KeyList: []string{"a"},Foreign: &ast.ForeignDef{- Table: &ast.TableDef{Name: "people"},+ Table: &ast.TableDef{+ Type: ast.TableTypeBaseTable,+ Name: "people",+ },ColumnList: []string{"id"},},},@@ -171,7 +179,10 @@ func TestPGConvertCreateTableStmt(t *testing.T) {Type: ast.ConstraintTypeForeign,KeyList: []string{"a"},Foreign: &ast.ForeignDef{- Table: &ast.TableDef{Name: "people"},+ Table: &ast.TableDef{+ Type: ast.TableTypeBaseTable,+ Name: "people",+ },ColumnList: []string{"b"},},},@@ -194,11 +205,13 @@ func TestPGAddColumnStmt(t *testing.T) {want: []ast.Node{&ast.AlterTableStmt{Table: &ast.TableDef{+ Type: ast.TableTypeBaseTable,Name: "techbook",},AlterItemList: []ast.Node{&ast.AddColumnListStmt{Table: &ast.TableDef{+ Type: ast.TableTypeBaseTable,Name: "techbook",},ColumnList: []*ast.ColumnDef{@@ -217,11 +230,13 @@ func TestPGAddColumnStmt(t *testing.T) {want: []ast.Node{&ast.AlterTableStmt{Table: &ast.TableDef{+ Type: ast.TableTypeBaseTable,Name: "techbook",},AlterItemList: []ast.Node{&ast.AddColumnListStmt{Table: &ast.TableDef{+ Type: ast.TableTypeBaseTable,Name: "techbook",},ColumnList: []*ast.ColumnDef{@@ -256,6 +271,7 @@ func TestPGRenameTableStmt(t *testing.T) {want: []ast.Node{&ast.RenameTableStmt{Table: &ast.TableDef{+ Type: ast.TableTypeBaseTable,Name: "techbook",},NewName: "techBook",@@ -277,6 +293,7 @@ func TestPGRenameColumnStmt(t *testing.T) {want: []ast.Node{&ast.RenameColumnStmt{Table: &ast.TableDef{+ Type: ast.TableTypeBaseTable,Name: "techbook",},ColumnName: "abc",@@ -298,7 +315,10 @@ func TestPGRenameConstraintStmt(t *testing.T) {stmt: "ALTER TABLE tech_book RENAME CONSTRAINT uk_tech_a to \"UK_TECH_A\"",want: []ast.Node{&ast.RenameConstraintStmt{- Table: &ast.TableDef{Name: "tech_book"},+ Table: &ast.TableDef{+ Type: ast.TableTypeBaseTable,+ Name: "tech_book",+ },ConstraintName: "uk_tech_a",NewName: "UK_TECH_A",},@@ -424,10 +444,16 @@ func TestPGDropConstraintStmt(t *testing.T) {stmt: "ALTER TABLE tech_book DROP CONSTRAINT uk_tech_a",want: []ast.Node{&ast.AlterTableStmt{- Table: &ast.TableDef{Name: "tech_book"},+ Table: &ast.TableDef{+ Type: ast.TableTypeBaseTable,+ Name: "tech_book",+ },AlterItemList: []ast.Node{&ast.DropConstraintStmt{- Table: &ast.TableDef{Name: "tech_book"},+ Table: &ast.TableDef{+ Type: ast.TableTypeBaseTable,+ Name: "tech_book",+ },ConstraintName: "uk_tech_a",},},@@ -448,10 +474,16 @@ func TestPGAddConstraintStmt(t *testing.T) {stmt: "ALTER TABLE tech_book ADD CONSTRAINT check_a_bigger_than_b CHECK (a > b) NOT VALID",want: []ast.Node{&ast.AlterTableStmt{- Table: &ast.TableDef{Name: "tech_book"},+ Table: &ast.TableDef{+ Type: ast.TableTypeBaseTable,+ Name: "tech_book",+ },AlterItemList: []ast.Node{&ast.AddConstraintStmt{- Table: &ast.TableDef{Name: "tech_book"},+ Table: &ast.TableDef{+ Type: ast.TableTypeBaseTable,+ Name: "tech_book",+ },Constraint: &ast.ConstraintDef{Type: ast.ConstraintTypeCheck,Name: "check_a_bigger_than_b",@@ -470,10 +502,16 @@ func TestPGAddConstraintStmt(t *testing.T) {stmt: "ALTER TABLE tech_book ADD CONSTRAINT uk_tech_book_id UNIQUE (id)",want: []ast.Node{&ast.AlterTableStmt{- Table: &ast.TableDef{Name: "tech_book"},+ Table: &ast.TableDef{+ Type: ast.TableTypeBaseTable,+ Name: "tech_book",+ },AlterItemList: []ast.Node{&ast.AddConstraintStmt{- Table: &ast.TableDef{Name: "tech_book"},+ Table: &ast.TableDef{+ Type: ast.TableTypeBaseTable,+ Name: "tech_book",+ },Constraint: &ast.ConstraintDef{Type: ast.ConstraintTypeUnique,Name: "uk_tech_book_id",@@ -491,10 +529,16 @@ func TestPGAddConstraintStmt(t *testing.T) {stmt: "ALTER TABLE tech_book ADD CONSTRAINT pk_tech_book_id PRIMARY KEY (id)",want: []ast.Node{&ast.AlterTableStmt{- Table: &ast.TableDef{Name: "tech_book"},+ Table: &ast.TableDef{+ Type: ast.TableTypeBaseTable,+ Name: "tech_book",+ },AlterItemList: []ast.Node{&ast.AddConstraintStmt{- Table: &ast.TableDef{Name: "tech_book"},+ Table: &ast.TableDef{+ Type: ast.TableTypeBaseTable,+ Name: "tech_book",+ },Constraint: &ast.ConstraintDef{Type: ast.ConstraintTypePrimary,Name: "pk_tech_book_id",@@ -512,16 +556,25 @@ func TestPGAddConstraintStmt(t *testing.T) {stmt: "ALTER TABLE tech_book ADD CONSTRAINT fk_tech_book_id FOREIGN KEY (id) REFERENCES people(id)",want: []ast.Node{&ast.AlterTableStmt{- Table: &ast.TableDef{Name: "tech_book"},+ Table: &ast.TableDef{+ Type: ast.TableTypeBaseTable,+ Name: "tech_book",+ },AlterItemList: []ast.Node{&ast.AddConstraintStmt{- Table: &ast.TableDef{Name: "tech_book"},+ Table: &ast.TableDef{+ Type: ast.TableTypeBaseTable,+ Name: "tech_book",+ },Constraint: &ast.ConstraintDef{Type: ast.ConstraintTypeForeign,Name: "fk_tech_book_id",KeyList: []string{"id"},Foreign: &ast.ForeignDef{- Table: &ast.TableDef{Name: "people"},+ Table: &ast.TableDef{+ Type: ast.TableTypeBaseTable,+ Name: "people",+ },ColumnList: []string{"id"},},},@@ -537,10 +590,16 @@ func TestPGAddConstraintStmt(t *testing.T) {stmt: "ALTER TABLE tech_book ADD CONSTRAINT uk_tech_book_id UNIQUE USING INDEX uk_id",want: []ast.Node{&ast.AlterTableStmt{- Table: &ast.TableDef{Name: "tech_book"},+ Table: &ast.TableDef{+ Type: ast.TableTypeBaseTable,+ Name: "tech_book",+ },AlterItemList: []ast.Node{&ast.AddConstraintStmt{- Table: &ast.TableDef{Name: "tech_book"},+ Table: &ast.TableDef{+ Type: ast.TableTypeBaseTable,+ Name: "tech_book",+ },Constraint: &ast.ConstraintDef{Type: ast.ConstraintTypeUniqueUsingIndex,Name: "uk_tech_book_id",@@ -558,10 +617,16 @@ func TestPGAddConstraintStmt(t *testing.T) {stmt: "ALTER TABLE tech_book ADD CONSTRAINT pk_tech_book_id PRIMARY KEY USING INDEX pk_id",want: []ast.Node{&ast.AlterTableStmt{- Table: &ast.TableDef{Name: "tech_book"},+ Table: &ast.TableDef{+ Type: ast.TableTypeBaseTable,+ Name: "tech_book",+ },AlterItemList: []ast.Node{&ast.AddConstraintStmt{- Table: &ast.TableDef{Name: "tech_book"},+ Table: &ast.TableDef{+ Type: ast.TableTypeBaseTable,+ Name: "tech_book",+ },Constraint: &ast.ConstraintDef{Type: ast.ConstraintTypePrimaryUsingIndex,Name: "pk_tech_book_id",@@ -586,10 +651,16 @@ func TestPGDropColumnStmt(t *testing.T) {stmt: "ALTER TABLE tech_book DROP COLUMN a",want: []ast.Node{&ast.AlterTableStmt{- Table: &ast.TableDef{Name: "tech_book"},+ Table: &ast.TableDef{+ Type: ast.TableTypeBaseTable,+ Name: "tech_book",+ },AlterItemList: []ast.Node{&ast.DropColumnStmt{- Table: &ast.TableDef{Name: "tech_book"},+ Table: &ast.TableDef{+ Type: ast.TableTypeBaseTable,+ Name: "tech_book",+ },ColumnName: "a",},},@@ -612,9 +683,11 @@ func TestPGDropTableStmt(t *testing.T) {&ast.DropTableStmt{TableList: []*ast.TableDef{{+ Type: ast.TableTypeBaseTable,Name: "tech_book",},{+ Type: ast.TableTypeBaseTable,Schema: "xschema",Name: "user",},@@ -636,10 +709,16 @@ func TestPGNotNullStmt(t *testing.T) {stmt: "ALTER TABLE tech_book ALTER COLUMN id SET NOT NULL",want: []ast.Node{&ast.AlterTableStmt{- Table: &ast.TableDef{Name: "tech_book"},+ Table: &ast.TableDef{+ Type: ast.TableTypeBaseTable,+ Name: "tech_book",+ },AlterItemList: []ast.Node{&ast.SetNotNullStmt{- Table: &ast.TableDef{Name: "tech_book"},+ Table: &ast.TableDef{+ Type: ast.TableTypeBaseTable,+ Name: "tech_book",+ },ColumnName: "id",},},@@ -653,10 +732,16 @@ func TestPGNotNullStmt(t *testing.T) {stmt: "ALTER TABLE tech_book ALTER COLUMN id DROP NOT NULL",want: []ast.Node{&ast.AlterTableStmt{- Table: &ast.TableDef{Name: "tech_book"},+ Table: &ast.TableDef{+ Type: ast.TableTypeBaseTable,+ Name: "tech_book",+ },AlterItemList: []ast.Node{&ast.DropNotNullStmt{- Table: &ast.TableDef{Name: "tech_book"},+ Table: &ast.TableDef{+ Type: ast.TableTypeBaseTable,+ Name: "tech_book",+ },ColumnName: "id",},},