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 ALTER TABLE statements in PostgreSQL walkthrough (#4269)
rebelice24ec7b888d5e
diff --git a/plugin/advisor/catalog/state.go b/plugin/advisor/catalog/state.goindex aa5b8d523a..e283238b8a 100644--- a/plugin/advisor/catalog/state.go+++ b/plugin/advisor/catalog/state.go@@ -344,6 +344,11 @@ type IndexState struct {visible *bool// Comment isn't supported for SQLite.comment *string++ // PostgreSQL specific fields.++ // PostgreSQL treats INDEX and CONSTRAINT differently.+ isConstraint bool}func (idx *IndexState) copy() *IndexState {@@ -355,6 +360,7 @@ func (idx *IndexState) copy() *IndexState {primary: copyBoolPointer(idx.primary),visible: copyBoolPointer(idx.visible),comment: copyStringPointer(idx.comment),+ isConstraint: idx.isConstraint,}}diff --git a/plugin/advisor/catalog/test/pg_walk_through.yaml b/plugin/advisor/catalog/test/pg_walk_through.yamlindex f3729fb7a5..f8c353f5fb 100644--- a/plugin/advisor/catalog/test/pg_walk_through.yaml+++ b/plugin/advisor/catalog/test/pg_walk_through.yaml@@ -187,3 +187,113 @@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 TABLE t rename to t1;+ ALTER TABLE t1 rename column a to a1;+ alter table t1 rename constraint uk_a_b to ukk_a_b;+ alter table t1 add column d int;+ alter table t1 drop column c;+ alter table t1 alter column b set data type bigint;+ alter table t1 alter column a1 set default 1;+ alter table t1 alter column b drop default;+ alter table t1 alter column d set not null;+ alter table t1 add constraint uk_d unique (d);+ alter table t1 add constraint ukk_d unique (d);+ alter table t1 drop constraint ukk_d;+ want:+ name: postgres+ schemas:+ - name: public+ tables:+ - name: t1+ columns:+ - name: a1+ position: 1+ default:+ value: "1"+ nullable: false+ type: integer+ characterset: ""+ collation: ""+ comment: ""+ - name: b+ position: 2+ default: null+ nullable: false+ type: bigint+ characterset: ""+ collation: ""+ comment: ""+ - name: d+ position: 4+ default: null+ nullable: false+ type: integer+ characterset: ""+ collation: ""+ comment: ""+ indexes:+ - name: t_a_b_c_idx+ expressions:+ - a1+ - 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:+ - a1+ type: btree+ unique: true+ primary: true+ visible: false+ comment: ""+ - name: uk_d+ expressions:+ - d+ type: btree+ unique: true+ primary: false+ visible: false+ comment: ""+ - name: ukk_a_b+ expressions:+ - a1+ - 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: nulldiff --git a/plugin/advisor/catalog/walk_through.go b/plugin/advisor/catalog/walk_through.goindex 844cc7bf87..d2e08fc7b9 100644--- a/plugin/advisor/catalog/walk_through.go+++ b/plugin/advisor/catalog/walk_through.go@@ -30,6 +30,8 @@ const (ErrorTypeUnsupported WalkThroughErrorType = 1// ErrorTypeInternal is the error for internal errors.ErrorTypeInternal WalkThroughErrorType = 2+ // ErrorTypeInvalidStatement is the error type for invalid statement errors.+ ErrorTypeInvalidStatement WalkThroughErrorType = 3// 101 parse error type.@@ -106,6 +108,11 @@ const (// ErrorTypeRelationExists is the error that relation already exists.ErrorTypeRelationExists = 801++ // 901 ~ 999 constraint error type.++ // ErrorTypeConstraintNotExists is the error that constraint doesn't exist.+ ErrorTypeConstraintNotExists = 901)// WalkThroughError is the error for walking-through.diff --git a/plugin/advisor/catalog/walk_through_for_pg.go b/plugin/advisor/catalog/walk_through_for_pg.goindex a62ed88afc..d444cf5f54 100644--- a/plugin/advisor/catalog/walk_through_for_pg.go+++ b/plugin/advisor/catalog/walk_through_for_pg.go@@ -48,17 +48,306 @@ func (d *DatabaseState) pgChangeState(in ast.Node) (err *WalkThroughError) {return d.pgCreateTable(node)case *ast.CreateIndexStmt:return d.pgCreateIndex(node)+ case *ast.AlterTableStmt:+ return d.pgAlterTable(node)default:return nil}}+func (d *DatabaseState) pgAlterTable(node *ast.AlterTableStmt) *WalkThroughError {+ schema, err := d.getSchema(node.Table.Schema)+ if err != nil {+ return err+ }+ table, err := schema.getTable(node.Table.Name)+ if err != nil {+ return err+ }++ for _, item := range node.AlterItemList {+ switch itemNode := item.(type) {+ case *ast.RenameColumnStmt:+ if err := table.pgRenameColumn(itemNode); err != nil {+ return err+ }+ case *ast.RenameConstraintStmt:+ if err := schema.pgRenameConstraint(table, itemNode); err != nil {+ return err+ }+ case *ast.RenameTableStmt:+ if err := schema.pgRenameTable(table, itemNode); err != nil {+ return err+ }+ case *ast.SetSchemaStmt:+ if err := d.pgSetSchema(schema, table, itemNode); err != nil {+ return err+ }+ case *ast.AddColumnListStmt:+ if err := schema.pgAddColumn(table, itemNode); err != nil {+ return err+ }+ case *ast.DropColumnStmt:+ if err := schema.pgDropColumn(table, itemNode); err != nil {+ return err+ }+ case *ast.AlterColumnTypeStmt:+ if err := schema.pgAlterColumnType(table, itemNode); err != nil {+ return err+ }+ case *ast.SetDefaultStmt:+ if err := table.pgSetDefault(itemNode); err != nil {+ return err+ }+ case *ast.DropDefaultStmt:+ if err := table.pgDropDefault(itemNode); err != nil {+ return err+ }+ case *ast.SetNotNullStmt:+ if err := table.pgSetNotNull(itemNode); err != nil {+ return err+ }+ case *ast.DropNotNullStmt:+ if err := table.pgDropNotNull(itemNode); err != nil {+ return err+ }+ case *ast.AddConstraintStmt:+ if err := schema.pgCreateTableConstraint(table, itemNode.Constraint); err != nil {+ return err+ }+ case *ast.DropConstraintStmt:+ if err := schema.pgDropConstraint(table, itemNode); err != nil {+ return err+ }+ }+ }++ return nil+}++func (s *SchemaState) pgDropConstraint(t *TableState, node *ast.DropConstraintStmt) *WalkThroughError {+ if index, exists := t.indexSet[node.ConstraintName]; exists {+ delete(s.identifierMap, index.name)+ delete(t.indexSet, index.name)+ }++ // TODO(rebelice): deal with other constraints++ // TODO(rebelice): deal with CASCADE++ return nil+}++func (t *TableState) pgDropNotNull(node *ast.DropNotNullStmt) *WalkThroughError {+ column, err := t.getColumn(node.ColumnName)+ if err != nil {+ return err+ }++ column.nullable = newTruePointer()+ return nil+}++func (t *TableState) pgSetNotNull(node *ast.SetNotNullStmt) *WalkThroughError {+ column, err := t.getColumn(node.ColumnName)+ if err != nil {+ return err+ }++ column.nullable = newFalsePointer()+ return nil+}++func (t *TableState) pgDropDefault(node *ast.DropDefaultStmt) *WalkThroughError {+ column, err := t.getColumn(node.ColumnName)+ if err != nil {+ return err+ }++ column.defaultValue = nil+ return nil+}++func (t *TableState) pgSetDefault(node *ast.SetDefaultStmt) *WalkThroughError {+ column, err := t.getColumn(node.ColumnName)+ if err != nil {+ return err+ }++ column.defaultValue = newStringPointer(node.Expression.Text())+ return nil+}++func (*SchemaState) pgAlterColumnType(t *TableState, node *ast.AlterColumnTypeStmt) *WalkThroughError {+ column, err := t.getColumn(node.ColumnName)+ if err != nil {+ return err+ }++ typeString, deparseErr := parser.Deparse(parser.Postgres, parser.DeparseContext{}, node.Type)+ if deparseErr != nil {+ return &WalkThroughError{+ Type: ErrorTypeDeparseError,+ Content: err.Error(),+ }+ }+ column.columnType = &typeString+ column.collation = newStringPointer(normalizeCollation(node.Collation))+ // TODO(rebelice): support USING expression+ return nil+}++func (*SchemaState) pgDropColumn(t *TableState, node *ast.DropColumnStmt) *WalkThroughError {+ if _, exists := t.columnSet[node.ColumnName]; !exists {+ if node.IfExists {+ return nil+ }+ return NewColumnNotExistsError(t.name, node.ColumnName)+ }++ // Drop the constraints and indexes involving the column.+ var dropIndexList []string++ for _, index := range t.indexSet {+ for _, key := range index.expressionList {+ // TODO(rebelice): deal with expression key.+ if key == index.name {+ dropIndexList = append(dropIndexList, index.name)+ }+ }+ }+ for _, indexName := range dropIndexList {+ delete(t.indexSet, indexName)+ }++ // TODO(rebelice): deal with other constraints.++ // TODO(rebelice): deal with CASCADE.++ delete(t.columnSet, node.ColumnName)+ return nil+}++func (s *SchemaState) pgAddColumn(t *TableState, node *ast.AddColumnListStmt) *WalkThroughError {+ if len(node.ColumnList) != 1 {+ return &WalkThroughError{+ Type: ErrorTypeInvalidStatement,+ Content: "PostgreSQL doesn't support to add multi-columns in one ADD COLUMN statement",+ }+ }+ return s.pgCreateColumn(t, node.ColumnList[0], node.IfNotExists)+}++func (d *DatabaseState) pgSetSchema(oldSchema *SchemaState, t *TableState, node *ast.SetSchemaStmt) *WalkThroughError {+ newSchema, exists := d.schemaSet[node.NewSchema]+ if !exists {+ return &WalkThroughError{+ Type: ErrorTypeSchemaNotExists,+ Content: fmt.Sprintf("Schema %q does not exist", node.NewSchema),+ }+ }++ if _, exists := newSchema.identifierMap[t.name]; exists {+ return NewRelationExistsError(t.name, newSchema.name)+ }++ for indexName := range t.indexSet {+ if _, exists := newSchema.identifierMap[indexName]; exists {+ return NewRelationExistsError(indexName, newSchema.name)+ }+ }++ // TODO(rebelice): check other constraints and sequences here.++ for indexName := range t.indexSet {+ delete(oldSchema.identifierMap, indexName)+ newSchema.identifierMap[indexName] = true+ }++ delete(oldSchema.identifierMap, t.name)+ delete(oldSchema.tableSet, t.name)+ newSchema.identifierMap[t.name] = true+ newSchema.tableSet[t.name] = t+ return nil+}++func (s *SchemaState) pgRenameTable(t *TableState, node *ast.RenameTableStmt) *WalkThroughError {+ if _, exists := s.identifierMap[node.NewName]; exists {+ return NewRelationExistsError(node.NewName, s.name)+ }++ delete(s.identifierMap, t.name)+ delete(s.tableSet, t.name)+ t.name = node.NewName+ s.identifierMap[t.name] = true+ s.tableSet[t.name] = t+ return nil+}++func (s *SchemaState) pgRenameConstraint(t *TableState, node *ast.RenameConstraintStmt) *WalkThroughError {+ index, exists := t.indexSet[node.ConstraintName]+ if !exists {+ // We haven't deal with foreign and check constraints, so skip if not exists.+ return nil+ }+ // TODO(rebelice): check other constraints here.++ if !index.isConstraint {+ return &WalkThroughError{+ Type: ErrorTypeConstraintNotExists,+ Content: fmt.Sprintf("Constraint %q for table %q does not exist", node.ConstraintName, t.name),+ }+ }++ if _, exists := s.identifierMap[node.NewName]; exists {+ return NewRelationExistsError(node.NewName, s.name)+ }++ delete(s.identifierMap, node.ConstraintName)+ delete(t.indexSet, node.ConstraintName)+ index.name = node.NewName+ s.identifierMap[index.name] = true+ t.indexSet[index.name] = index+ return nil+}++func (t *TableState) pgRenameColumn(node *ast.RenameColumnStmt) *WalkThroughError {+ column, err := t.getColumn(node.ColumnName)+ if err != nil {+ return err+ }+ if node.ColumnName == node.NewName {+ return nil+ }+ if _, exists := t.columnSet[node.NewName]; exists {+ return &WalkThroughError{+ Type: ErrorTypeColumnExists,+ Content: fmt.Sprintf("The column %q already exists in table %q", node.NewName, t.name),+ }+ }++ delete(t.columnSet, column.name)++ for _, index := range t.indexSet {+ for i, key := range index.expressionList {+ // TODO(rebelice): only deal with the column type index key here.+ if key == column.name {+ index.expressionList[i] = node.NewName+ }+ }+ }++ column.name = node.NewName+ t.columnSet[node.NewName] = column+ return nil+}+func (d *DatabaseState) pgCreateIndex(node *ast.CreateIndexStmt) *WalkThroughError {schema, err := d.getSchema(node.Index.Table.Schema)if err != nil {return err}- return schema.pgCreateIndex(node, false /* isPrimary */)+ return schema.pgCreateIndex(node, false /* isPrimary */, false /* isConstraint */)}func (d *DatabaseState) pgCreateTable(node *ast.CreateTableStmt) *WalkThroughError {@@ -92,7 +381,7 @@ func (d *DatabaseState) pgCreateTable(node *ast.CreateTableStmt) *WalkThroughErrschema.tableSet[table.name] = tablefor _, column := range node.ColumnList {- if err := schema.pgCreateColumn(table, column); err != nil {+ if err := schema.pgCreateColumn(table, column, false /* ifNotExists */); err != nil {err.Line = column.LastLine()return err}@@ -133,7 +422,7 @@ func (s *SchemaState) pgCreateTableConstraint(t *TableState, constraint *ast.ConMethod: ast.IndexMethodTypeBTree,KeyList: indexKeyList,},- }, false /* isPrimary */)+ }, false /* isPrimary */, true /* isConstraint */)case ast.ConstraintTypeUniqueUsingIndex:return s.pgCreateUniqueUsingIndex(t, constraint)case ast.ConstraintTypeCheck:@@ -146,8 +435,11 @@ func (s *SchemaState) pgCreateTableConstraint(t *TableState, constraint *ast.Conreturn nil}-func (s *SchemaState) pgCreateColumn(t *TableState, column *ast.ColumnDef) *WalkThroughError {+func (s *SchemaState) pgCreateColumn(t *TableState, column *ast.ColumnDef, ifNotExists bool) *WalkThroughError {if _, exists := t.columnSet[column.ColumnName]; exists {+ if ifNotExists {+ return nil+ }return &WalkThroughError{Type: ErrorTypeColumnExists,Content: fmt.Sprintf("The column %q already exists in table %q", column.ColumnName, t.name),@@ -169,7 +461,8 @@ func (s *SchemaState) pgCreateColumn(t *TableState, column *ast.ColumnDef) *WalkdefaultValue: nil,nullable: newTruePointer(),columnType: &typeString,- // TODO(rebelice): support collation and comment here.+ collation: newStringPointer(normalizeCollation(column.Collation)),+ // TODO(rebelice): support comment here.}t.columnSet[columnState.name] = columnState@@ -204,7 +497,7 @@ func (s *SchemaState) pgCreateColumn(t *TableState, column *ast.ColumnDef) *Walk},Method: ast.IndexMethodTypeBTree,},- }, false /* isPrimary */); err != nil {+ }, false /* isPrimary */, true /* isConstraint */); err != nil {return err}case ast.ConstraintTypeUniqueUsingIndex:@@ -302,7 +595,7 @@ func (s *SchemaState) pgCreatePrimaryKey(t *TableState, createPrimaryKey *ast.CoMethod: ast.IndexMethodTypeBTree,KeyList: pgColumnNameListToKeyList(createPrimaryKey.KeyList),},- }, true /* isPrimary */)+ }, true /* isPrimary */, true /* isConstraint */)}func pgColumnNameListToKeyList(columnList []string) []*ast.IndexKeyDef {@@ -330,7 +623,7 @@ func (s *SchemaState) pgGeneratePrimaryKeyName(tableName string) string {}}-func (s *SchemaState) pgCreateIndex(createIndex *ast.CreateIndexStmt, isPrimary bool) *WalkThroughError {+func (s *SchemaState) pgCreateIndex(createIndex *ast.CreateIndexStmt, isPrimary bool, isConstraint bool) *WalkThroughError {indexName := createIndex.Index.Nameif len(createIndex.Index.KeyList) == 0 {return &WalkThroughError{@@ -372,6 +665,7 @@ func (s *SchemaState) pgCreateIndex(createIndex *ast.CreateIndexStmt, isPrimaryindexType: newStringPointer(getIndexMethod(createIndex.Index.Method)),unique: newBoolPointer(createIndex.Index.Unique),primary: newBoolPointer(isPrimary),+ isConstraint: isConstraint,}table.indexSet[index.name] = index@@ -474,6 +768,40 @@ func (d *DatabaseState) getSchema(schemaName string) (*SchemaState, *WalkThroughreturn schema, nil}+func (s *SchemaState) getTable(tableName string) (*TableState, *WalkThroughError) {+ table, exists := s.tableSet[tableName]+ if !exists {+ return nil, &WalkThroughError{+ Type: ErrorTypeTableNotExists,+ Content: fmt.Sprintf("The table %q doesn't exists in schema %q", tableName, s.name),+ }+ }+ return table, nil+}++func (t *TableState) getColumn(columnName string) (*ColumnState, *WalkThroughError) {+ column, exists := t.columnSet[columnName]+ if !exists {+ return nil, &WalkThroughError{+ Type: ErrorTypeColumnNotExists,+ Content: fmt.Sprintf("The column %q doesn't exists in table %q", columnName, t.name),+ }+ }+ return column, nil+}+func pgParse(stmt string) ([]ast.Node, error) {return parser.Parse(parser.Postgres, parser.ParseContext{}, stmt)}++func normalizeCollation(collation *ast.CollationNameDef) string {+ if collation == nil {+ return ""+ }++ if collation.Schema == "" || collation.Schema == "public" {+ return collation.Name+ }++ return fmt.Sprintf("%q.%q", collation.Schema, collation.Name)+}diff --git a/plugin/parser/ast/add_column_list_stmt.go b/plugin/parser/ast/add_column_list_stmt.goindex 3cbef515b7..b9b9143c8b 100644--- a/plugin/parser/ast/add_column_list_stmt.go+++ b/plugin/parser/ast/add_column_list_stmt.go@@ -5,6 +5,7 @@ package asttype AddColumnListStmt struct {node- Table *TableDef- ColumnList []*ColumnDef+ Table *TableDef+ ColumnList []*ColumnDef+ IfNotExists bool}diff --git a/plugin/parser/ast/drop_column_stmt.go b/plugin/parser/ast/drop_column_stmt.goindex 637d9511f0..ab7fc73142 100644--- a/plugin/parser/ast/drop_column_stmt.go+++ b/plugin/parser/ast/drop_column_stmt.go@@ -6,4 +6,6 @@ type DropColumnStmt struct {Table *TableDefColumnName string+ IfExists bool+ Behavior DropBehavior}diff --git a/plugin/parser/engine/pg/convert.go b/plugin/parser/engine/pg/convert.goindex b1ad25c7c1..65cbbfcbab 100644--- a/plugin/parser/engine/pg/convert.go+++ b/plugin/parser/engine/pg/convert.go@@ -51,8 +51,9 @@ func convert(node *pgquery.Node, statement parser.SingleSQL) (res ast.Node, err}addColumn := &ast.AddColumnListStmt{- Table: alterTable.Table,- ColumnList: []*ast.ColumnDef{column},+ Table: alterTable.Table,+ ColumnList: []*ast.ColumnDef{column},+ IfNotExists: alterCmd.MissingOk,}alterTable.AlterItemList = append(alterTable.AlterItemList, addColumn)@@ -60,6 +61,8 @@ func convert(node *pgquery.Node, statement parser.SingleSQL) (res ast.Node, errdropColumn := &ast.DropColumnStmt{Table: alterTable.Table,ColumnName: alterCmd.Name,+ IfExists: alterCmd.MissingOk,+ Behavior: convertDropBehavior(alterCmd.Behavior),}alterTable.AlterItemList = append(alterTable.AlterItemList, dropColumn)case pgquery.AlterTableType_AT_AddConstraint:diff --git a/plugin/parser/engine/pg/convert_test.go b/plugin/parser/engine/pg/convert_test.goindex cfe797f6de..5695a1ec15 100644--- a/plugin/parser/engine/pg/convert_test.go+++ b/plugin/parser/engine/pg/convert_test.go@@ -552,7 +552,7 @@ func TestPGConvertCreateTableStmt(t *testing.T) {func TestPGAddColumnStmt(t *testing.T) {tests := []testData{{- stmt: "ALTER TABLE techbook ADD COLUMN a int",+ stmt: "ALTER TABLE techbook ADD COLUMN IF NOT EXISTS a int",want: []ast.Node{&ast.AlterTableStmt{Table: &ast.TableDef{@@ -565,6 +565,7 @@ func TestPGAddColumnStmt(t *testing.T) {Type: ast.TableTypeBaseTable,Name: "techbook",},+ IfNotExists: true,ColumnList: []*ast.ColumnDef{{ColumnName: "a",@@ -577,7 +578,7 @@ func TestPGAddColumnStmt(t *testing.T) {},statementList: []parser.SingleSQL{{- Text: "ALTER TABLE techbook ADD COLUMN a int",+ Text: "ALTER TABLE techbook ADD COLUMN IF NOT EXISTS a int",LastLine: 1,},},@@ -1436,7 +1437,7 @@ func TestPGAddConstraintStmt(t *testing.T) {func TestPGDropColumnStmt(t *testing.T) {tests := []testData{{- stmt: "ALTER TABLE tech_book DROP COLUMN a",+ stmt: "ALTER TABLE tech_book DROP COLUMN IF EXISTS a CASCADE",want: []ast.Node{&ast.AlterTableStmt{Table: &ast.TableDef{@@ -1450,13 +1451,15 @@ func TestPGDropColumnStmt(t *testing.T) {Name: "tech_book",},ColumnName: "a",+ IfExists: true,+ Behavior: ast.DropBehaviorCascade,},},},},statementList: []parser.SingleSQL{{- Text: "ALTER TABLE tech_book DROP COLUMN a",+ Text: "ALTER TABLE tech_book DROP COLUMN IF EXISTS a CASCADE",LastLine: 1,},},diff --git a/plugin/parser/engine/pg/deparse.go b/plugin/parser/engine/pg/deparse.goindex 8fd01b2a29..56cb80415e 100644--- a/plugin/parser/engine/pg/deparse.go+++ b/plugin/parser/engine/pg/deparse.go@@ -729,7 +729,18 @@ func deparseDropColumn(context parser.DeparseContext, in *ast.DropColumnStmt, buif _, err := buf.WriteString("DROP COLUMN "); err != nil {return err}- return writeSurrounding(buf, in.ColumnName, `"`)++ if in.IfExists {+ if _, err := buf.WriteString("IF EXISTS "); err != nil {+ return err+ }+ }++ if err := writeSurrounding(buf, in.ColumnName, `"`); err != nil {+ return err+ }++ return deparseDropBehavior(context, in.Behavior, buf)}func deparseAlterColumnType(context parser.DeparseContext, in *ast.AlterColumnTypeStmt, buf *strings.Builder) error {@@ -758,6 +769,12 @@ func deparseAddColumnList(context parser.DeparseContext, in *ast.AddColumnListStreturn err}+ if in.IfNotExists {+ if _, err := buf.WriteString("IF NOT EXISTS "); err != nil {+ return err+ }+ }+if len(in.ColumnList) != 1 {return errors.Errorf("PostgreSQL doesn't support zero or multi-columns for ALTER TABLE ADD COLUMN statements")}diff --git a/plugin/parser/engine/pg/test-data/test_alter_table_data.yaml b/plugin/parser/engine/pg/test-data/test_alter_table_data.yamlindex d31eec51c0..63530a8d8a 100644--- a/plugin/parser/engine/pg/test-data/test_alter_table_data.yaml+++ b/plugin/parser/engine/pg/test-data/test_alter_table_data.yaml@@ -8,19 +8,19 @@ALTER COLUMN "a" SET DATA TYPE character varying(20);- stmt: |-alter table t- add column a integer,+ add column if not exists a integer,add column b char(20)want: |-ALTER TABLE "t"- ADD COLUMN "a" integer,+ ADD COLUMN IF NOT EXISTS "a" integer,ADD COLUMN "b" character(20);- stmt: |-alter table t- drop column a,+ drop column if exists a cascade,drop column bwant: |-ALTER TABLE "t"- DROP COLUMN "a",+ DROP COLUMN IF EXISTS "a" CASCADE,DROP COLUMN "b";- stmt: |-alter table t