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(catalog): implement Rename Index and DROP statements for PostgreSQL walkthrough (#4281)
rebeliceac0420be39c5
diff --git a/plugin/advisor/catalog/test/pg_walk_through.yaml b/plugin/advisor/catalog/test/pg_walk_through.yamlindex f8c353f5fb..35fa3f8a2f 100644--- a/plugin/advisor/catalog/test/pg_walk_through.yaml+++ b/plugin/advisor/catalog/test/pg_walk_through.yaml@@ -297,3 +297,211 @@collation: ""extensions: []err: null+- statement: |-+ CREATE TABLE public.t(+ a int primary key,+ b int NOT NULL DEFAULT 1,+ c int UNIQUE,+ UNIQUE (a, b, c),+ CONSTRAINT uk_a_b UNIQUE (a, b)+ );+ alter index uk_a_b rename to ukk_a_b;+ want:+ name: postgres+ schemas:+ - name: public+ tables:+ - name: t+ columns:+ - name: a+ position: 1+ default: null+ nullable: false+ type: integer+ characterset: ""+ collation: ""+ comment: ""+ - name: b+ position: 2+ default:+ value: "1"+ nullable: false+ type: integer+ characterset: ""+ collation: ""+ comment: ""+ - name: c+ position: 3+ default: null+ nullable: true+ type: integer+ characterset: ""+ collation: ""+ comment: ""+ indexes:+ - name: t_a_b_c_idx+ expressions:+ - a+ - b+ - c+ type: btree+ unique: true+ primary: false+ visible: false+ comment: ""+ - name: t_c_idx+ expressions:+ - c+ type: btree+ unique: true+ primary: false+ visible: false+ comment: ""+ - name: t_pkey+ expressions:+ - a+ type: btree+ unique: true+ primary: true+ visible: false+ comment: ""+ - name: ukk_a_b+ expressions:+ - a+ - b+ type: btree+ unique: true+ primary: false+ visible: false+ comment: ""+ engine: ""+ collation: ""+ rowcount: 0+ datasize: 0+ indexsize: 0+ datafree: 0+ createoptions: ""+ comment: ""+ foreignkeys: []+ views: []+ characterset: ""+ collation: ""+ extensions: []+ err: null+- statement: |-+ CREATE TABLE public.t(+ a int primary key,+ b int NOT NULL DEFAULT 1,+ c int UNIQUE,+ UNIQUE (a, b, c),+ CONSTRAINT uk_a_b UNIQUE (a, b)+ );+ drop index uk_a_b;+ want:+ name: postgres+ schemas:+ - name: public+ tables:+ - name: t+ columns:+ - name: a+ position: 1+ default: null+ nullable: false+ type: integer+ characterset: ""+ collation: ""+ comment: ""+ - name: b+ position: 2+ default:+ value: "1"+ nullable: false+ type: integer+ characterset: ""+ collation: ""+ comment: ""+ - name: c+ position: 3+ default: null+ nullable: true+ type: integer+ characterset: ""+ collation: ""+ comment: ""+ indexes:+ - name: t_a_b_c_idx+ expressions:+ - a+ - b+ - c+ type: btree+ unique: true+ primary: false+ visible: false+ comment: ""+ - name: t_c_idx+ expressions:+ - c+ type: btree+ unique: true+ primary: false+ visible: false+ comment: ""+ - name: t_pkey+ expressions:+ - a+ type: btree+ unique: true+ primary: true+ visible: false+ comment: ""+ engine: ""+ collation: ""+ rowcount: 0+ datasize: 0+ indexsize: 0+ datafree: 0+ createoptions: ""+ comment: ""+ foreignkeys: []+ views: []+ characterset: ""+ collation: ""+ extensions: []+ err: null+- statement: |-+ CREATE TABLE public.t(+ a int primary key,+ b int NOT NULL DEFAULT 1,+ c int UNIQUE,+ UNIQUE (a, b, c),+ CONSTRAINT uk_a_b UNIQUE (a, b)+ );+ drop table t;+ want:+ name: postgres+ schemas:+ - name: public+ tables: []+ views: []+ characterset: ""+ collation: ""+ extensions: []+ err: null+- statement: |-+ CREATE TABLE public.t(+ a int primary key,+ b int NOT NULL DEFAULT 1,+ c int UNIQUE,+ UNIQUE (a, b, c),+ CONSTRAINT uk_a_b UNIQUE (a, b)+ );+ drop schema public;+ want:+ name: postgres+ schemas: []+ characterset: ""+ collation: ""+ extensions: []+ err: nulldiff --git a/plugin/advisor/catalog/walk_through.go b/plugin/advisor/catalog/walk_through.goindex d2e08fc7b9..08377e9723 100644--- a/plugin/advisor/catalog/walk_through.go+++ b/plugin/advisor/catalog/walk_through.go@@ -103,6 +103,8 @@ const (// ErrorTypeSchemaNotExists is the error that schema does not exist.ErrorTypeSchemaNotExists = 701+ // ErrorTypeSchemaExists is the error that schema already exists.+ ErrorTypeSchemaExists = 702// 801 ~ 899 relation error type.diff --git a/plugin/advisor/catalog/walk_through_for_pg.go b/plugin/advisor/catalog/walk_through_for_pg.goindex d444cf5f54..cc446604b7 100644--- a/plugin/advisor/catalog/walk_through_for_pg.go+++ b/plugin/advisor/catalog/walk_through_for_pg.go@@ -50,11 +50,158 @@ func (d *DatabaseState) pgChangeState(in ast.Node) (err *WalkThroughError) {return d.pgCreateIndex(node)case *ast.AlterTableStmt:return d.pgAlterTable(node)+ case *ast.RenameIndexStmt:+ return d.pgRenameIndex(node)+ case *ast.CreateSchemaStmt:+ return d.pgCreateSchema(node)+ case *ast.DropSchemaStmt:+ return d.pgDropSchema(node)+ case *ast.DropTableStmt:+ return d.pgDropTableList(node)+ case *ast.DropIndexStmt:+ return d.pgDropIndexList(node)default:return nil}}+func (d *DatabaseState) pgDropIndexList(node *ast.DropIndexStmt) *WalkThroughError {+ for _, indexDef := range node.IndexList {+ if err := d.pgDropIndex(indexDef, node.IfExists, node.Behavior); err != nil {+ return err+ }+ }++ return nil+}++func (d *DatabaseState) pgDropIndex(indexDef *ast.IndexDef, ifExists bool, _ ast.DropBehavior) *WalkThroughError {+ schemaName := ""+ if indexDef.Table != nil {+ schemaName = indexDef.Table.Schema+ }+ schema, err := d.getSchema(schemaName)+ if err != nil {+ if ifExists {+ return nil+ }+ return err+ }++ table, index, err := schema.getIndex(indexDef.Name)+ if err != nil {+ return err+ }++ delete(schema.identifierMap, index.name)+ delete(table.indexSet, index.name)+ return nil+}++func (d *DatabaseState) pgDropTableList(node *ast.DropTableStmt) *WalkThroughError {+ for _, tableName := range node.TableList {+ if err := d.pgDropTable(tableName, node.IfExists, node.Behavior); err != nil {+ return err+ }+ }++ return nil+}++func (d *DatabaseState) pgDropTable(tableDef *ast.TableDef, ifExists bool, _ ast.DropBehavior) *WalkThroughError {+ schema, err := d.getSchema(tableDef.Schema)+ if err != nil {+ if ifExists {+ return nil+ }+ return err+ }++ table, err := schema.getTable(tableDef.Name)+ if err != nil {+ if ifExists {+ return nil+ }+ return err+ }++ for indexName := range table.indexSet {+ delete(schema.identifierMap, indexName)+ }++ delete(schema.identifierMap, table.name)+ delete(schema.tableSet, table.name)+ return nil+}++func (d *DatabaseState) pgDropSchema(node *ast.DropSchemaStmt) *WalkThroughError {+ for _, schemaName := range node.SchemaList {+ schema, err := d.getSchema(schemaName)+ if err != nil {+ if node.IfExists {+ continue+ }+ return err+ }++ delete(d.schemaSet, schema.name)+ }++ return nil+}++func (d *DatabaseState) pgCreateSchema(node *ast.CreateSchemaStmt) *WalkThroughError {+ if _, exists := d.schemaSet[node.Name]; exists {+ return &WalkThroughError{+ Type: ErrorTypeSchemaExists,+ Content: fmt.Sprintf("Schema %q already exists", node.Name),+ }+ }++ schema := &SchemaState{+ name: node.Name,+ identifierMap: make(identifierMap),+ tableSet: make(tableStateMap),+ viewSet: make(viewStateMap),+ }+ d.schemaSet[schema.name] = schema++ for _, item := range node.SchemaElementList {+ switch itemNode := item.(type) {+ // TODO(rebelice): deal with other item type here.+ case *ast.CreateTableStmt:+ if err := d.pgCreateTable(itemNode); err != nil {+ return err+ }+ default:+ // TODO: hack the linter.+ }+ }++ return nil+}++func (d *DatabaseState) pgRenameIndex(node *ast.RenameIndexStmt) *WalkThroughError {+ schema, err := d.getSchema(node.Table.Schema)+ if err != nil {+ return err+ }+ table, index, err := schema.getIndex(node.IndexName)+ if err != nil {+ return err+ }+ if _, exists := schema.identifierMap[node.NewName]; exists {+ return NewRelationExistsError(node.NewName, schema.name)+ }++ delete(schema.identifierMap, index.name)+ delete(table.indexSet, index.name)+ index.name = node.NewName+ schema.identifierMap[index.name] = true+ table.indexSet[index.name] = index+ return nil+}+func (d *DatabaseState) pgAlterTable(node *ast.AlterTableStmt) *WalkThroughError {schema, err := d.getSchema(node.Table.Schema)if err != nil {@@ -779,6 +926,19 @@ func (s *SchemaState) getTable(tableName string) (*TableState, *WalkThroughErrorreturn table, nil}+func (s *SchemaState) getIndex(indexName string) (*TableState, *IndexState, *WalkThroughError) {+ for _, table := range s.tableSet {+ if index, exists := table.indexSet[indexName]; exists {+ return table, index, nil+ }+ }++ return nil, nil, &WalkThroughError{+ Type: ErrorTypeIndexNotExists,+ Content: fmt.Sprintf("Index %q does not exists in schema %q", indexName, s.name),+ }+}+func (t *TableState) getColumn(columnName string) (*ColumnState, *WalkThroughError) {column, exists := t.columnSet[columnName]if !exists {