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: pg sync schema supports comments of views (#12303)
rebelice90b2315d230c
diff --git a/backend/plugin/parser/pg/differ.go b/backend/plugin/parser/pg/differ.goindex cf530276c7..139f1e42eb 100644--- a/backend/plugin/parser/pg/differ.go+++ b/backend/plugin/parser/pg/differ.go@@ -127,18 +127,20 @@ func newTableInfo(id int, createTable *ast.CreateTableStmt) *tableInfo {}type viewInfo struct {- id int- existsInNew bool- createView *ast.CreateViewStmt- comment string+ id int+ existsInNew bool+ createView *ast.CreateViewStmt+ comment string+ columnCommentMap map[string]string}func newViewInfo(id int, createView *ast.CreateViewStmt) *viewInfo {return &viewInfo{- id: id,- existsInNew: false,- createView: createView,- comment: "",+ id: id,+ existsInNew: false,+ createView: createView,+ comment: "",+ columnCommentMap: make(map[string]string),}}@@ -476,6 +478,21 @@ func (m schemaMap) addComment(comment *ast.CommentStmt) error {}table.comment = comment.Commentreturn nil+ case ast.ObjectTypeView:+ tableDef, ok := comment.Object.(*ast.TableDef)+ if !ok {+ return errors.Errorf("failed to add comment: expect view def, but found %v", comment.Object)+ }+ schema, exists := m[tableDef.Schema]+ if !exists {+ return errors.Errorf("failed to add comment: schema %s not found", tableDef.Schema)+ }+ view, exists := schema.viewMap[tableDef.Name]+ if !exists {+ return errors.Errorf("failed to add comment: view %s not found", tableDef.Name)+ }+ view.comment = comment.Comment+ return nilcase ast.ObjectTypeColumn:columnNameDef, ok := comment.Object.(*ast.ColumnNameDef)if !ok {@@ -483,13 +500,20 @@ func (m schemaMap) addComment(comment *ast.CommentStmt) error {}schema, exists := m[columnNameDef.Table.Schema]if !exists {- return errors.Errorf("failed to add comment: schema %s not found", columnNameDef.Table.Schema)+ slog.Debug("failed to add comment", slog.String("schema", columnNameDef.Table.Schema))+ return nil}table, exists := schema.tableMap[columnNameDef.Table.Name]- if !exists {- return errors.Errorf("failed to add comment: table %s not found", columnNameDef.Table.Name)+ if exists {+ table.columnCommentMap[columnNameDef.ColumnName] = comment.Comment+ return nil}- table.columnCommentMap[columnNameDef.ColumnName] = comment.Comment+ view, exists := schema.viewMap[columnNameDef.Table.Name]+ if exists {+ view.columnCommentMap[columnNameDef.ColumnName] = comment.Comment+ return nil+ }+ slog.Debug("failed to add comment", slog.String("table or view", columnNameDef.Table.Name))return nildefault:// We only support table and column comment.@@ -509,6 +533,18 @@ func (m schemaMap) getTableComment(schemaName string, tableName string) string {return table.comment}+func (m schemaMap) getViewComment(schemaName string, viewName string) string {+ schema, exists := m[schemaName]+ if !exists {+ return ""+ }+ view, exists := schema.viewMap[viewName]+ if !exists {+ return ""+ }+ return view.comment+}+func (m schemaMap) removeTableComment(schemaName string, tableName string) {schema, exists := m[schemaName]if !exists {@@ -521,20 +557,38 @@ func (m schemaMap) removeTableComment(schemaName string, tableName string) {table.comment = ""}-func (m schemaMap) getColumnComment(schemaName string, tableName string, columnName string) string {+func (m schemaMap) removeViewComment(schemaName string, viewName string) {schema, exists := m[schemaName]if !exists {- return ""+ return}- table, exists := schema.tableMap[tableName]+ view, exists := schema.viewMap[viewName]if !exists {- return ""+ return}- columnComment, exists := table.columnCommentMap[columnName]+ view.comment = ""+}++func (m schemaMap) getColumnComment(schemaName string, tableName string, columnName string) string {+ schema, exists := m[schemaName]if !exists {return ""}- return columnComment+ table, exists := schema.tableMap[tableName]+ if exists {+ columnComment, exists := table.columnCommentMap[columnName]+ if exists {+ return columnComment+ }+ }+ view, exists := schema.viewMap[tableName]+ if exists {+ columnComment, exists := view.columnCommentMap[columnName]+ if exists {+ return columnComment+ }+ }+ return ""}func (m schemaMap) removeColumnComment(schemaName string, tableName string, columnName string) {@@ -543,10 +597,14 @@ func (m schemaMap) removeColumnComment(schemaName string, tableName string, colureturn}table, exists := schema.tableMap[tableName]- if !exists {+ if exists {+ delete(table.columnCommentMap, columnName)return}- delete(table.columnCommentMap, columnName)+ view, exists := schema.viewMap[tableName]+ if exists {+ delete(view.columnCommentMap, columnName)+ }}func onlySetOwnedBy(sequence *ast.AlterSequenceStmt) bool {@@ -991,6 +1049,16 @@ func SchemaDiff(_ base.DiffContext, oldStmt, newStmt string) (string, error) {diff.setCommentList = append(diff.setCommentList, stmt)}oldSchemaMap.removeTableComment(tableDef.Schema, tableDef.Name)+ case ast.ObjectTypeView:+ viewDef, ok := stmt.Object.(*ast.TableDef)+ if !ok {+ return "", errors.Errorf("failed to add comment: expect view def, but found %v", stmt.Object)+ }+ oldComment := oldSchemaMap.getViewComment(viewDef.Schema, viewDef.Name)+ if oldComment == "" || oldComment != stmt.Comment {+ diff.setCommentList = append(diff.setCommentList, stmt)+ }+ oldSchemaMap.removeViewComment(viewDef.Schema, viewDef.Name)case ast.ObjectTypeColumn:columnNameDef, ok := stmt.Object.(*ast.ColumnNameDef)if !ok {@@ -1286,6 +1354,9 @@ func (diff *diffNode) modifyTableByConstraint(oldTable *ast.CreateTableStmt, newfunc (diff *diffNode) ModifyView(oldView *viewInfo, newView *ast.CreateViewStmt) error {if !equalView(oldView.createView, newView) {diff.dropViewList = append(diff.dropViewList, oldView.createView.Name)+ // drop the comments of the view+ oldView.comment = ""+ oldView.columnCommentMap = make(map[string]string)tempView, err := getTempView(newView)if err != nil {return err@@ -2102,6 +2173,7 @@ func dropFunction(m schemaMap) *ast.DropFunctionStmt {func (diff *diffNode) dropComment(m schemaMap) {var tables []*tableInfo+ var views []*viewInfofor _, schema := range m {for _, table := range schema.tableMap {if !table.existsInNew {@@ -2110,9 +2182,13 @@ func (diff *diffNode) dropComment(m schemaMap) {}tables = append(tables, table)}- }- if len(tables) == 0 {- return+ for _, view := range schema.viewMap {+ if !view.existsInNew {+ // no need to drop, because the comments are dropped when drop view+ continue+ }+ views = append(views, view)+ }}sort.Slice(tables, func(i, j int) bool {return tables[i].id < tables[j].id@@ -2152,6 +2228,45 @@ func (diff *diffNode) dropComment(m schemaMap) {})}}++ sort.Slice(views, func(i, j int) bool {+ return views[i].id < views[j].id+ })++ for _, view := range views {+ if view.comment != "" {+ diff.setCommentList = append(diff.setCommentList, &ast.CommentStmt{+ Comment: "",+ Object: &ast.TableDef{Schema: view.createView.Name.Schema, Name: view.createView.Name.Name},+ Type: ast.ObjectTypeView,+ })+ }++ type commentInfo struct {+ column string+ comment string+ }+ var columnCommentList []commentInfo+ for k, v := range view.columnCommentMap {+ columnCommentList = append(columnCommentList, commentInfo{+ column: k,+ comment: v,+ })+ }+ sort.Slice(columnCommentList, func(i, j int) bool {+ return columnCommentList[i].column < columnCommentList[j].column+ })+ for _, columnComment := range columnCommentList {+ diff.setCommentList = append(diff.setCommentList, &ast.CommentStmt{+ Comment: "",+ Object: &ast.ColumnNameDef{+ Table: &ast.TableDef{Schema: view.createView.Name.Schema, Name: view.createView.Name.Name},+ ColumnName: columnComment.column,+ },+ Type: ast.ObjectTypeColumn,+ })+ }+ }}func (diff *diffNode) dropTypeStmt(m schemaMap) {diff --git a/backend/plugin/parser/pg/test-data/test_differ_data.yaml b/backend/plugin/parser/pg/test-data/test_differ_data.yamlindex 20b0a4d8d2..705e48f439 100644--- a/backend/plugin/parser/pg/test-data/test_differ_data.yaml+++ b/backend/plugin/parser/pg/test-data/test_differ_data.yaml@@ -3,17 +3,39 @@CREATE VIEW public.v1 AS SELECT t1.a, t1.b, t1.c FROM public.t1;CREATE VIEW public.v2 AS SELECT v1.a, v1.b, v1.c FROM public.v1;CREATE VIEW public.v3 AS SELECT v2.a, v2.b, v2.c FROM public.v2;+ comment on column public.v1.a is 'comment with ''; test escape';+ comment on column public.v2.b is 'comment with ''; test escape for b';+ comment on column public.v3.a is 'comment with ''; test escape';++ comment on view public.v1 is 'comment with ''; test escape';+ comment on view public.v2 is 'comment with ''; test escape for b';+ comment on view public.v3 is 'comment with ''; test escape';newSchema: |CREATE TABLE public.t1(a int, b int, c int);CREATE VIEW public.v1 AS SELECT t1.a, t1.b, t1.c FROM public.t1;CREATE VIEW public.v2 AS SELECT t1.a, t1.b, t1.c FROM public.t1;CREATE VIEW public.v4 AS SELECT v2.a, v2.b, v2.c FROM public.v2;- diff: |+ comment on column public.v1.a is 'comment with ''; test escape';+ comment on column public.v2.b is 'comment with ''sdkfjaslkdjf; test escape for b';+ comment on column public.v4.a is 'comment with ''; test escape';++ comment on view public.v1 is 'comment with ''; test escape';+ comment on view public.v2 is 'comment with ''lkdjfaldsjf; test escape for b';+ comment on view public.v4 is 'comment with ''; test escape';+ diff: |+DROP VIEW "public"."v2", "public"."v3";CREATE VIEW "public"."v2" AS SELECT 1 AS "a", 1 AS "b", 1 AS "c";;CREATE VIEW "public"."v4" AS SELECT 1 AS "a", 1 AS "b", 1 AS "c";;CREATE OR REPLACE VIEW public.v2 AS SELECT t1.a, t1.b, t1.c FROM public.t1;CREATE OR REPLACE VIEW public.v4 AS SELECT v2.a, v2.b, v2.c FROM public.v2;+ COMMENT ON COLUMN "public"."v2"."b" IS 'comment with ''sdkfjaslkdjf; test escape for b';++ COMMENT ON COLUMN "public"."v4"."a" IS 'comment with ''; test escape';++ COMMENT ON VIEW "public"."v2" IS 'comment with ''lkdjfaldsjf; test escape for b';++ COMMENT ON VIEW "public"."v4" IS 'comment with ''; test escape';+- oldSchema: |CREATE TABLE public.projects ();newSchema: |diff --git a/backend/plugin/parser/sql/ast/comment_stmt.go b/backend/plugin/parser/sql/ast/comment_stmt.goindex 2fa9b7f1c6..f4b96e3f80 100644--- a/backend/plugin/parser/sql/ast/comment_stmt.go+++ b/backend/plugin/parser/sql/ast/comment_stmt.go@@ -5,6 +5,7 @@ type ObjectType intconst (ObjectTypeUndefined ObjectType = iotaObjectTypeTable+ ObjectTypeViewObjectTypeColumn)diff --git a/backend/plugin/parser/sql/engine/pg/convert.go b/backend/plugin/parser/sql/engine/pg/convert.goindex fea1d2a4b6..5e75b5351d 100644--- a/backend/plugin/parser/sql/engine/pg/convert.go+++ b/backend/plugin/parser/sql/engine/pg/convert.go@@ -691,6 +691,18 @@ func convert(node *pgquery.Node, statement base.SingleSQL) (res ast.Node, err erdefault:return nil, errors.Errorf("expect to get a list node but got %T", node)}+ case pgquery.ObjectType_OBJECT_VIEW:+ commentStmt.Type = ast.ObjectTypeView+ switch node := in.CommentStmt.Object.Node.(type) {+ case *pgquery.Node_List:+ tableDef, err := convertNodeListToTableDef(node.List.Items)+ if err != nil {+ return nil, err+ }+ commentStmt.Object = tableDef+ default:+ return nil, errors.Errorf("expect to get a list node but got %T", node)+ }}return &commentStmt, nildiff --git a/backend/plugin/parser/sql/engine/pg/deparse.go b/backend/plugin/parser/sql/engine/pg/deparse.goindex a2ec3648fd..08fd01a265 100644--- a/backend/plugin/parser/sql/engine/pg/deparse.go+++ b/backend/plugin/parser/sql/engine/pg/deparse.go@@ -117,8 +117,12 @@ func deparseComment(context DeparseContext, in *ast.CommentStmt, buf *strings.Bu}switch in.Type {- case ast.ObjectTypeTable:- if _, err := buf.WriteString("COMMENT ON TABLE \""); err != nil {+ case ast.ObjectTypeTable, ast.ObjectTypeView:+ typeText := "TABLE"+ if in.Type == ast.ObjectTypeView {+ typeText = "VIEW"+ }+ if _, err := fmt.Fprintf(buf, "COMMENT ON %s \"", typeText); err != nil {return err}tableDef, ok := in.Object.(*ast.TableDef)