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: support comment in sdl diff (#17805)
rebelice3e9a015ff967
diff --git a/backend/plugin/schema/differ.go b/backend/plugin/schema/differ.goindex 09564a76f4..f96b89d0ee 100644--- a/backend/plugin/schema/differ.go+++ b/backend/plugin/schema/differ.go@@ -4,6 +4,7 @@ import ("slices""strings"+ "github.com/antlr4-go/antlr/v4"parser "github.com/bytebase/parser/postgresql""github.com/bytebase/bytebase/backend/common"@@ -51,6 +52,9 @@ type MetadataDiff struct {// Event changesEventChanges []*EventDiff++ // Comment changes+ CommentChanges []*CommentDiff}// nolint@@ -246,6 +250,34 @@ type EventDiff struct {NewEvent *storepb.EventMetadata}+// CommentObjectType represents the type of database object that has a comment.+type CommentObjectType string++const (+ CommentObjectTypeSchema CommentObjectType = "SCHEMA"+ CommentObjectTypeTable CommentObjectType = "TABLE"+ CommentObjectTypeColumn CommentObjectType = "COLUMN"+ CommentObjectTypeView CommentObjectType = "VIEW"+ CommentObjectTypeFunction CommentObjectType = "FUNCTION"+ CommentObjectTypeSequence CommentObjectType = "SEQUENCE"+ CommentObjectTypeIndex CommentObjectType = "INDEX"+)++// CommentDiff represents changes to database object comments.+// Comments are tracked independently to avoid triggering object recreation when only comments change.+type CommentDiff struct {+ Action MetadataDiffAction // CREATE or ALTER (no DROP since object deletion removes comments automatically)+ ObjectType CommentObjectType+ SchemaName string+ ObjectName string // table/view/function/sequence/index name+ ColumnName string // only used for COLUMN comments+ IndexName string // only used for table-level INDEX comments+ OldComment string+ NewComment string+ OldASTNode antlr.ParserRuleContext+ NewASTNode antlr.ParserRuleContext+}+// GetDatabaseSchemaDiff compares two model.DatabaseSchema instances and returns the differences.func GetDatabaseSchemaDiff(engine storepb.Engine, oldSchema, newSchema *model.DatabaseSchema) (*MetadataDiff, error) {if oldSchema == nil || newSchema == nil {diff --git a/backend/plugin/schema/pg/get_sdl_diff.go b/backend/plugin/schema/pg/get_sdl_diff.goindex 36f7e360f0..c0a13207a6 100644--- a/backend/plugin/schema/pg/get_sdl_diff.go+++ b/backend/plugin/schema/pg/get_sdl_diff.go@@ -76,6 +76,7 @@ func GetSDLDiff(currentSDLText, previousUserSDLText string, currentSchema, previProcedureChanges: []*schema.ProcedureDiff{},SequenceChanges: []*schema.SequenceDiff{},EnumTypeChanges: []*schema.EnumTypeDiff{},+ CommentChanges: []*schema.CommentDiff{},}// Process table changes@@ -96,17 +97,23 @@ func GetSDLDiff(currentSDLText, previousUserSDLText string, currentSchema, previ// Process sequence changesprocessSequenceChanges(currentChunks, previousChunks, currentDBSDLChunks, diff)+ // Process comment changes (must be after all object changes are processed)+ processCommentChanges(currentChunks, previousChunks, diff)+return diff, nil}func ChunkSDLText(sdlText string) (*schema.SDLChunks, error) {if strings.TrimSpace(sdlText) == "" {return &schema.SDLChunks{- Tables: make(map[string]*schema.SDLChunk),- Views: make(map[string]*schema.SDLChunk),- Functions: make(map[string]*schema.SDLChunk),- Indexes: make(map[string]*schema.SDLChunk),- Sequences: make(map[string]*schema.SDLChunk),+ Tables: make(map[string]*schema.SDLChunk),+ Views: make(map[string]*schema.SDLChunk),+ Functions: make(map[string]*schema.SDLChunk),+ Indexes: make(map[string]*schema.SDLChunk),+ Sequences: make(map[string]*schema.SDLChunk),+ Schemas: make(map[string]*schema.SDLChunk),+ ColumnComments: make(map[string]map[string]antlr.ParserRuleContext),+ IndexComments: make(map[string]map[string]antlr.ParserRuleContext),}, nil}@@ -118,11 +125,14 @@ func ChunkSDLText(sdlText string) (*schema.SDLChunks, error) {extractor := &sdlChunkExtractor{sdlText: sdlText,chunks: &schema.SDLChunks{- Tables: make(map[string]*schema.SDLChunk),- Views: make(map[string]*schema.SDLChunk),- Functions: make(map[string]*schema.SDLChunk),- Indexes: make(map[string]*schema.SDLChunk),- Sequences: make(map[string]*schema.SDLChunk),+ Tables: make(map[string]*schema.SDLChunk),+ Views: make(map[string]*schema.SDLChunk),+ Functions: make(map[string]*schema.SDLChunk),+ Indexes: make(map[string]*schema.SDLChunk),+ Sequences: make(map[string]*schema.SDLChunk),+ Schemas: make(map[string]*schema.SDLChunk),+ ColumnComments: make(map[string]map[string]antlr.ParserRuleContext),+ IndexComments: make(map[string]map[string]antlr.ParserRuleContext),},tokens: parseResult.Tokens,}@@ -319,6 +329,187 @@ func (l *sdlChunkExtractor) EnterViewstmt(ctx *parser.ViewstmtContext) {l.chunks.Views[schemaQualifiedName] = chunk}+func (l *sdlChunkExtractor) EnterCommentstmt(ctx *parser.CommentstmtContext) {+ // Extract the comment text (can be sconst or NULL)+ // We store the entire AST node, not just the comment text++ // Check for COLUMN comment: COMMENT ON COLUMN any_name IS comment_text+ if ctx.COLUMN() != nil {+ if ctx.Any_name() != nil {+ // Extract schema.table.column from any_name+ anyName := pgparser.NormalizePostgreSQLAnyName(ctx.Any_name())+ if len(anyName) >= 3 {+ // Format: schema.table.column+ schemaName := anyName[0]+ tableName := anyName[1]+ columnName := anyName[2]+ tableIdentifier := schemaName + "." + tableName++ // Ensure the map for this table exists+ if l.chunks.ColumnComments[tableIdentifier] == nil {+ l.chunks.ColumnComments[tableIdentifier] = make(map[string]antlr.ParserRuleContext)+ }+ l.chunks.ColumnComments[tableIdentifier][columnName] = ctx+ } else if len(anyName) == 2 {+ // Format: table.column (assume public schema)+ tableName := anyName[0]+ columnName := anyName[1]+ tableIdentifier := "public." + tableName++ if l.chunks.ColumnComments[tableIdentifier] == nil {+ l.chunks.ColumnComments[tableIdentifier] = make(map[string]antlr.ParserRuleContext)+ }+ l.chunks.ColumnComments[tableIdentifier][columnName] = ctx+ }+ }+ return+ }++ // Check for FUNCTION comment: COMMENT ON FUNCTION function_with_argtypes IS comment_text+ if ctx.FUNCTION() != nil {+ if ctx.Function_with_argtypes() != nil {+ funcWithArgsCtx := ctx.Function_with_argtypes()++ // Extract function name from func_name+ var funcNameParts []string+ if funcWithArgsCtx.Func_name() != nil {+ funcNameParts = pgparser.NormalizePostgreSQLFuncName(funcWithArgsCtx.Func_name())+ } else if funcWithArgsCtx.Colid() != nil {+ // Single identifier case+ funcNameParts = []string{pgparser.NormalizePostgreSQLColid(funcWithArgsCtx.Colid())}+ } else {+ // Fallback: use the whole text+ funcNameParts = []string{funcWithArgsCtx.GetText()}+ }++ // Determine schema name+ var schemaName string+ var funcName string+ if len(funcNameParts) >= 2 {+ schemaName = funcNameParts[0]+ funcName = funcNameParts[1]+ } else if len(funcNameParts) == 1 {+ schemaName = "public"+ funcName = funcNameParts[0]+ } else {+ return+ }++ // Try to find matching function by name prefix (schema.funcname)+ // This is necessary because CREATE FUNCTION uses parameter names and normalized types,+ // while COMMENT ON FUNCTION may use different type representations+ funcNamePrefix := schemaName + "." + funcName+ var matchedChunk *schema.SDLChunk+ for identifier, chunk := range l.chunks.Functions {+ if strings.HasPrefix(identifier, funcNamePrefix+"(") || identifier == funcNamePrefix {+ matchedChunk = chunk+ break+ }+ }++ if matchedChunk != nil {+ // Add comment to the existing function chunk+ matchedChunk.CommentStatements = append(matchedChunk.CommentStatements, ctx)+ }+ // If no match found, we don't create a new chunk because the function+ // should already exist from CREATE FUNCTION statement+ }+ return+ }++ // Check for object_type_any_name: TABLE, SEQUENCE, VIEW, INDEX, etc.+ if ctx.Object_type_any_name() != nil && ctx.Any_name() != nil {+ objectType := ctx.Object_type_any_name().GetText()+ anyName := pgparser.NormalizePostgreSQLAnyName(ctx.Any_name())++ var identifier string+ if len(anyName) >= 2 {+ // Format: schema.object+ identifier = strings.Join(anyName, ".")+ } else if len(anyName) == 1 {+ // Format: object (assume public schema)+ identifier = "public." + anyName[0]+ } else {+ return+ }++ // Route to appropriate chunk map based on object type+ switch strings.ToUpper(objectType) {+ case "TABLE":+ if chunk, exists := l.chunks.Tables[identifier]; exists {+ chunk.CommentStatements = append(chunk.CommentStatements, ctx)+ } else {+ chunk := &schema.SDLChunk{+ Identifier: identifier,+ ASTNode: nil,+ CommentStatements: []antlr.ParserRuleContext{ctx},+ }+ l.chunks.Tables[identifier] = chunk+ }+ case "VIEW":+ if chunk, exists := l.chunks.Views[identifier]; exists {+ chunk.CommentStatements = append(chunk.CommentStatements, ctx)+ } else {+ chunk := &schema.SDLChunk{+ Identifier: identifier,+ ASTNode: nil,+ CommentStatements: []antlr.ParserRuleContext{ctx},+ }+ l.chunks.Views[identifier] = chunk+ }+ case "SEQUENCE":+ if chunk, exists := l.chunks.Sequences[identifier]; exists {+ chunk.CommentStatements = append(chunk.CommentStatements, ctx)+ } else {+ chunk := &schema.SDLChunk{+ Identifier: identifier,+ ASTNode: nil,+ CommentStatements: []antlr.ParserRuleContext{ctx},+ }+ l.chunks.Sequences[identifier] = chunk+ }+ case "INDEX":+ if chunk, exists := l.chunks.Indexes[identifier]; exists {+ chunk.CommentStatements = append(chunk.CommentStatements, ctx)+ } else {+ chunk := &schema.SDLChunk{+ Identifier: identifier,+ ASTNode: nil,+ CommentStatements: []antlr.ParserRuleContext{ctx},+ }+ l.chunks.Indexes[identifier] = chunk+ }+ default:+ // Unsupported object type for comment tracking (e.g., MATERIALIZED VIEW, FOREIGN TABLE)+ // We skip these for now+ }+ return+ }++ // Check for object_type_name: SCHEMA, DATABASE, etc.+ if ctx.Object_type_name() != nil && ctx.Name() != nil {+ objectType := ctx.Object_type_name().GetText()+ name := pgparser.NormalizePostgreSQLName(ctx.Name())++ if strings.ToUpper(objectType) == "SCHEMA" {+ if chunk, exists := l.chunks.Schemas[name]; exists {+ chunk.CommentStatements = append(chunk.CommentStatements, ctx)+ } else {+ chunk := &schema.SDLChunk{+ Identifier: name,+ ASTNode: nil,+ CommentStatements: []antlr.ParserRuleContext{ctx},+ }+ l.chunks.Schemas[name] = chunk+ }+ }+ return+ }++ // Check for object_type_name_on_any_name: TRIGGER, RULE, POLICY (we don't track these currently)+ // These are table-level objects that we may want to support in the future+}+// processTableChanges processes changes to tables by comparing SDL chunks// nolint:unparamfunc processTableChanges(currentChunks, previousChunks *schema.SDLChunks, currentSchema, previousSchema *model.DatabaseSchema, currentDBSDLChunks *currentDatabaseSDLChunks, diff *schema.MetadataDiff) error {@@ -327,9 +518,9 @@ func processTableChanges(currentChunks, previousChunks *schema.SDLChunks, currenschemaName, tableName := parseIdentifier(currentChunk.Identifier)if previousChunk, exists := previousChunks.Tables[currentChunk.Identifier]; exists {- // Table exists in both - check if modified- currentText := currentChunk.GetText()- previousText := previousChunk.GetText()+ // Table exists in both - check if modified (excluding comments)+ currentText := currentChunk.GetTextWithoutComments()+ previousText := previousChunk.GetTextWithoutComments()if currentText != previousText {// Apply usability check: skip diff if current chunk matches database metadata SDLif currentDBSDLChunks.shouldSkipChunkDiffForUsability(currentText, currentChunk.Identifier) {@@ -1481,9 +1672,9 @@ func processViewChanges(currentChunks, previousChunks *schema.SDLChunks, current// Process current views to find created and modified viewsfor _, currentChunk := range currentChunks.Views {if previousChunk, exists := previousChunks.Views[currentChunk.Identifier]; exists {- // View exists in both - check if modified by comparing text first- currentText := currentChunk.GetText()- previousText := previousChunk.GetText()+ // View exists in both - check if modified by comparing text first (excluding comments)+ currentText := currentChunk.GetTextWithoutComments()+ previousText := previousChunk.GetTextWithoutComments()if currentText != previousText {// Apply usability check: skip diff if current chunk matches database metadata SDLif currentDBSDLChunks.shouldSkipChunkDiffForUsability(currentText, currentChunk.Identifier) {@@ -1550,9 +1741,9 @@ func processFunctionChanges(currentChunks, previousChunks *schema.SDLChunks, cur// Process current functions to find created and modified functionsfor _, currentChunk := range currentChunks.Functions {if previousChunk, exists := previousChunks.Functions[currentChunk.Identifier]; exists {- // Function exists in both - check if modified by comparing text first- currentText := currentChunk.GetText()- previousText := previousChunk.GetText()+ // Function exists in both - check if modified by comparing text first (excluding comments)+ currentText := currentChunk.GetTextWithoutComments()+ previousText := previousChunk.GetTextWithoutComments()if currentText != previousText {// Apply usability check: skip diff if current chunk matches database metadata SDLif currentDBSDLChunks.shouldSkipChunkDiffForUsability(currentText, currentChunk.Identifier) {@@ -1611,9 +1802,9 @@ func processSequenceChanges(currentChunks, previousChunks *schema.SDLChunks, cur// Process current sequences to find created and modified sequencesfor _, currentChunk := range currentChunks.Sequences {if previousChunk, exists := previousChunks.Sequences[currentChunk.Identifier]; exists {- // Sequence exists in both - check if modified- currentText := currentChunk.GetText()- previousText := previousChunk.GetText()+ // Sequence exists in both - check if modified (excluding comments)+ currentText := currentChunk.GetTextWithoutComments()+ previousText := previousChunk.GetTextWithoutComments()if currentText != previousText {// Apply usability check: skip diff if current chunk matches database metadata SDLif currentDBSDLChunks.shouldSkipChunkDiffForUsability(currentText, currentChunk.Identifier) {@@ -4023,3 +4214,251 @@ func (e *sequenceExtractor) EnterCreateseqstmt(ctx *parser.CreateseqstmtContext)*e.result = ctx}}++// processCommentChanges processes comment changes for all database objects+// It must be called after all object changes have been processed to determine+// which objects were created or dropped (those should not generate comment diffs)+func processCommentChanges(currentChunks, previousChunks *schema.SDLChunks, diff *schema.MetadataDiff) {+ // Build sets of created and dropped objects to avoid generating comment diffs for them+ createdObjects := buildCreatedObjectsSet(diff)+ droppedObjects := buildDroppedObjectsSet(diff)++ // Process object-level comments+ processObjectComments(currentChunks.Tables, previousChunks.Tables, schema.CommentObjectTypeTable, createdObjects, droppedObjects, diff)+ processObjectComments(currentChunks.Views, previousChunks.Views, schema.CommentObjectTypeView, createdObjects, droppedObjects, diff)+ processObjectComments(currentChunks.Functions, previousChunks.Functions, schema.CommentObjectTypeFunction, createdObjects, droppedObjects, diff)+ processObjectComments(currentChunks.Sequences, previousChunks.Sequences, schema.CommentObjectTypeSequence, createdObjects, droppedObjects, diff)+ processObjectComments(currentChunks.Indexes, previousChunks.Indexes, schema.CommentObjectTypeIndex, createdObjects, droppedObjects, diff)+ processObjectComments(currentChunks.Schemas, previousChunks.Schemas, schema.CommentObjectTypeSchema, createdObjects, droppedObjects, diff)++ // Process column comments+ processColumnComments(currentChunks, previousChunks, createdObjects, droppedObjects, diff)+}++// buildCreatedObjectsSet builds a set of object identifiers that were created+func buildCreatedObjectsSet(diff *schema.MetadataDiff) map[string]bool {+ created := make(map[string]bool)++ for _, tableDiff := range diff.TableChanges {+ if tableDiff.Action == schema.MetadataDiffActionCreate {+ identifier := tableDiff.SchemaName + "." + tableDiff.TableName+ created[identifier] = true+ }+ }++ for _, viewDiff := range diff.ViewChanges {+ if viewDiff.Action == schema.MetadataDiffActionCreate {+ identifier := viewDiff.SchemaName + "." + viewDiff.ViewName+ created[identifier] = true+ }+ }++ for _, funcDiff := range diff.FunctionChanges {+ if funcDiff.Action == schema.MetadataDiffActionCreate {+ identifier := funcDiff.SchemaName + "." + funcDiff.FunctionName+ created[identifier] = true+ }+ }++ for _, seqDiff := range diff.SequenceChanges {+ if seqDiff.Action == schema.MetadataDiffActionCreate {+ identifier := seqDiff.SchemaName + "." + seqDiff.SequenceName+ created[identifier] = true+ }+ }++ return created+}++// buildDroppedObjectsSet builds a set of object identifiers that were dropped+func buildDroppedObjectsSet(diff *schema.MetadataDiff) map[string]bool {+ dropped := make(map[string]bool)++ for _, tableDiff := range diff.TableChanges {+ if tableDiff.Action == schema.MetadataDiffActionDrop {+ identifier := tableDiff.SchemaName + "." + tableDiff.TableName+ dropped[identifier] = true+ }+ }++ for _, viewDiff := range diff.ViewChanges {+ if viewDiff.Action == schema.MetadataDiffActionDrop {+ identifier := viewDiff.SchemaName + "." + viewDiff.ViewName+ dropped[identifier] = true+ }+ }++ for _, funcDiff := range diff.FunctionChanges {+ if funcDiff.Action == schema.MetadataDiffActionDrop {+ identifier := funcDiff.SchemaName + "." + funcDiff.FunctionName+ dropped[identifier] = true+ }+ }++ for _, seqDiff := range diff.SequenceChanges {+ if seqDiff.Action == schema.MetadataDiffActionDrop {+ identifier := seqDiff.SchemaName + "." + seqDiff.SequenceName+ dropped[identifier] = true+ }+ }++ return dropped+}++// processObjectComments processes comment changes for a specific object type+// droppedObjects is intentionally unused because we only process objects in currentMap,+// and dropped objects won't appear there.+func processObjectComments(currentMap, previousMap map[string]*schema.SDLChunk, objectType schema.CommentObjectType, createdObjects, _ map[string]bool, diff *schema.MetadataDiff) {+ // Process all objects in current chunks+ for identifier, currentChunk := range currentMap {+ // Skip if object was created (comment will be in CREATE statement)+ if createdObjects[identifier] {+ continue+ }++ previousChunk := previousMap[identifier]+ if previousChunk == nil {+ // Object doesn't exist in previous - this shouldn't happen as it should be in createdObjects+ continue+ }++ // Extract comment text from both chunks+ currentCommentText := extractCommentTextFromChunk(currentChunk)+ previousCommentText := extractCommentTextFromChunk(previousChunk)++ // If comments are different, generate a CommentDiff+ if currentCommentText != previousCommentText {+ schemaName, objectName := parseIdentifier(identifier)+ action := schema.MetadataDiffActionAlter+ if previousCommentText == "" {+ action = schema.MetadataDiffActionCreate+ }++ diff.CommentChanges = append(diff.CommentChanges, &schema.CommentDiff{+ Action: action,+ ObjectType: objectType,+ SchemaName: schemaName,+ ObjectName: objectName,+ OldComment: previousCommentText,+ NewComment: currentCommentText,+ OldASTNode: getFirstCommentNode(previousChunk.CommentStatements),+ NewASTNode: getFirstCommentNode(currentChunk.CommentStatements),+ })+ }+ }+}++// processColumnComments processes column comment changes+func processColumnComments(currentChunks, previousChunks *schema.SDLChunks, createdObjects, droppedObjects map[string]bool, diff *schema.MetadataDiff) {+ // Process all tables in current chunks+ allTableIdentifiers := make(map[string]bool)+ for identifier := range currentChunks.ColumnComments {+ allTableIdentifiers[identifier] = true+ }+ for identifier := range previousChunks.ColumnComments {+ allTableIdentifiers[identifier] = true+ }++ for tableIdentifier := range allTableIdentifiers {+ // Skip if table was created or dropped+ if createdObjects[tableIdentifier] || droppedObjects[tableIdentifier] {+ continue+ }++ currentColumns := currentChunks.ColumnComments[tableIdentifier]+ previousColumns := previousChunks.ColumnComments[tableIdentifier]++ // Find all column names+ allColumnNames := make(map[string]bool)+ for columnName := range currentColumns {+ allColumnNames[columnName] = true+ }+ for columnName := range previousColumns {+ allColumnNames[columnName] = true+ }++ // Compare each column's comment+ for columnName := range allColumnNames {+ currentNode := currentColumns[columnName]+ previousNode := previousColumns[columnName]++ currentCommentText := extractCommentTextFromNode(currentNode)+ previousCommentText := extractCommentTextFromNode(previousNode)++ if currentCommentText != previousCommentText {+ schemaName, tableName := parseIdentifier(tableIdentifier)+ action := schema.MetadataDiffActionAlter+ if previousCommentText == "" {+ action = schema.MetadataDiffActionCreate+ }++ diff.CommentChanges = append(diff.CommentChanges, &schema.CommentDiff{+ Action: action,+ ObjectType: schema.CommentObjectTypeColumn,+ SchemaName: schemaName,+ ObjectName: tableName,+ ColumnName: columnName,+ OldComment: previousCommentText,+ NewComment: currentCommentText,+ OldASTNode: previousNode,+ NewASTNode: currentNode,+ })+ }+ }+ }+}++// extractCommentTextFromChunk extracts the comment text from a chunk's comment statements+func extractCommentTextFromChunk(chunk *schema.SDLChunk) string {+ if chunk == nil || len(chunk.CommentStatements) == 0 {+ return ""+ }++ // Get the first comment statement (there should typically only be one)+ return extractCommentTextFromNode(chunk.CommentStatements[0])+}++// extractCommentTextFromNode extracts comment text from a COMMENT ON statement AST node+func extractCommentTextFromNode(node antlr.ParserRuleContext) string {+ if node == nil {+ return ""+ }++ // Try to cast to CommentstmtContext+ commentStmt, ok := node.(*parser.CommentstmtContext)+ if !ok {+ return ""+ }++ // Get the comment_text+ if commentStmt.Comment_text() == nil {+ return ""+ }++ commentTextCtx := commentStmt.Comment_text()++ // Check if it's NULL_P+ if commentTextCtx.NULL_P() != nil {+ return "" // NULL comment means no comment+ }++ // Get the sconst (string constant)+ if commentTextCtx.Sconst() != nil {+ text := commentTextCtx.Sconst().GetText()+ // Remove surrounding quotes+ if len(text) >= 2 && text[0] == '\'' && text[len(text)-1] == '\'' {+ return text[1 : len(text)-1]+ }+ return text+ }++ return ""+}++// getFirstCommentNode returns the first comment AST node from a list, or nil if empty+func getFirstCommentNode(nodes []antlr.ParserRuleContext) antlr.ParserRuleContext {+ if len(nodes) == 0 {+ return nil+ }+ return nodes[0]+}diff --git a/backend/plugin/schema/pg/get_sdl_diff_comment_test.go b/backend/plugin/schema/pg/get_sdl_diff_comment_test.gonew file mode 100644index 0000000000..eb4ba301d7--- /dev/null+++ b/backend/plugin/schema/pg/get_sdl_diff_comment_test.go@@ -0,0 +1,663 @@+package pg++import (+ "testing"++ "github.com/stretchr/testify/require"++ "github.com/bytebase/bytebase/backend/plugin/schema"+)++// TestCommentDiff_TableComment tests table-level comment changes+func TestCommentDiff_TableComment(t *testing.T) {+ tests := []struct {+ name string+ previousSDL string+ currentSDL string+ wantCommentDiffCount int+ wantAction schema.MetadataDiffAction+ wantObjectType schema.CommentObjectType+ wantOldComment string+ wantNewComment string+ }{+ {+ name: "add table comment",+ previousSDL: `+CREATE TABLE users (+ id INT PRIMARY KEY,+ name TEXT+);`,+ currentSDL: `+CREATE TABLE users (+ id INT PRIMARY KEY,+ name TEXT+);++COMMENT ON TABLE users IS 'User information table';`,+ wantCommentDiffCount: 1,+ wantAction: schema.MetadataDiffActionCreate,+ wantObjectType: schema.CommentObjectTypeTable,+ wantOldComment: "",+ wantNewComment: "User information table",+ },+ {+ name: "modify table comment",+ previousSDL: `+CREATE TABLE users (+ id INT PRIMARY KEY,+ name TEXT+);++COMMENT ON TABLE users IS 'Old comment';`,+ currentSDL: `+CREATE TABLE users (+ id INT PRIMARY KEY,+ name TEXT+);++COMMENT ON TABLE users IS 'New comment';`,+ wantCommentDiffCount: 1,+ wantAction: schema.MetadataDiffActionAlter,+ wantObjectType: schema.CommentObjectTypeTable,+ wantOldComment: "Old comment",+ wantNewComment: "New comment",+ },+ {+ name: "remove table comment",+ previousSDL: `+CREATE TABLE users (+ id INT PRIMARY KEY,+ name TEXT+);++COMMENT ON TABLE users IS 'User table';`,+ currentSDL: `+CREATE TABLE users (+ id INT PRIMARY KEY,+ name TEXT+);++COMMENT ON TABLE users IS NULL;`,+ wantCommentDiffCount: 1,+ wantAction: schema.MetadataDiffActionAlter,+ wantObjectType: schema.CommentObjectTypeTable,+ wantOldComment: "User table",+ wantNewComment: "",+ },+ {+ name: "no comment change",+ previousSDL: `+CREATE TABLE users (+ id INT PRIMARY KEY,+ name TEXT+);++COMMENT ON TABLE users IS 'Same comment';`,+ currentSDL: `+CREATE TABLE users (+ id INT PRIMARY KEY,+ name TEXT+);++COMMENT ON TABLE users IS 'Same comment';`,+ wantCommentDiffCount: 0,+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil, nil)+ require.NoError(t, err)+ require.NotNil(t, diff)++ require.Equal(t, tt.wantCommentDiffCount, len(diff.CommentChanges), "unexpected number of comment diffs")++ if tt.wantCommentDiffCount > 0 {+ commentDiff := diff.CommentChanges[0]+ require.Equal(t, tt.wantAction, commentDiff.Action, "unexpected action")+ require.Equal(t, tt.wantObjectType, commentDiff.ObjectType, "unexpected object type")+ require.Equal(t, tt.wantOldComment, commentDiff.OldComment, "unexpected old comment")+ require.Equal(t, tt.wantNewComment, commentDiff.NewComment, "unexpected new comment")+ require.Equal(t, "public", commentDiff.SchemaName, "unexpected schema name")+ require.Equal(t, "users", commentDiff.ObjectName, "unexpected object name")+ }+ })+ }+}++// TestCommentDiff_ColumnComment tests column-level comment changes+func TestCommentDiff_ColumnComment(t *testing.T) {+ tests := []struct {+ name string+ previousSDL string+ currentSDL string+ wantCommentDiffCount int+ wantAction schema.MetadataDiffAction+ wantColumnName string+ wantOldComment string+ wantNewComment string+ }{+ {+ name: "add column comment",+ previousSDL: `+CREATE TABLE users (+ id INT PRIMARY KEY,+ name TEXT+);`,+ currentSDL: `+CREATE TABLE users (+ id INT PRIMARY KEY,+ name TEXT+);++COMMENT ON COLUMN users.name IS 'User full name';`,+ wantCommentDiffCount: 1,+ wantAction: schema.MetadataDiffActionCreate,+ wantColumnName: "name",+ wantOldComment: "",+ wantNewComment: "User full name",+ },+ {+ name: "modify column comment",+ previousSDL: `+CREATE TABLE users (+ id INT PRIMARY KEY,+ name TEXT+);++COMMENT ON COLUMN users.name IS 'Old name';`,+ currentSDL: `+CREATE TABLE users (+ id INT PRIMARY KEY,+ name TEXT+);++COMMENT ON COLUMN users.name IS 'New name';`,+ wantCommentDiffCount: 1,+ wantAction: schema.MetadataDiffActionAlter,+ wantColumnName: "name",+ wantOldComment: "Old name",+ wantNewComment: "New name",+ },+ {+ name: "multiple column comments",+ previousSDL: `+CREATE TABLE users (+ id INT PRIMARY KEY,+ name TEXT,+ email TEXT+);++COMMENT ON COLUMN users.name IS 'User name';`,+ currentSDL: `+CREATE TABLE users (+ id INT PRIMARY KEY,+ name TEXT,+ email TEXT+);++COMMENT ON COLUMN users.name IS 'Full name';+COMMENT ON COLUMN users.email IS 'Email address';`,+ wantCommentDiffCount: 2,+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil, nil)+ require.NoError(t, err)+ require.NotNil(t, diff)++ require.Equal(t, tt.wantCommentDiffCount, len(diff.CommentChanges), "unexpected number of comment diffs")++ if tt.wantCommentDiffCount == 1 {+ commentDiff := diff.CommentChanges[0]+ require.Equal(t, tt.wantAction, commentDiff.Action, "unexpected action")+ require.Equal(t, schema.CommentObjectTypeColumn, commentDiff.ObjectType, "unexpected object type")+ require.Equal(t, tt.wantColumnName, commentDiff.ColumnName, "unexpected column name")+ require.Equal(t, tt.wantOldComment, commentDiff.OldComment, "unexpected old comment")+ require.Equal(t, tt.wantNewComment, commentDiff.NewComment, "unexpected new comment")+ }+ })+ }+}++// TestCommentDiff_ViewComment tests view-level comment changes+func TestCommentDiff_ViewComment(t *testing.T) {+ tests := []struct {+ name string+ previousSDL string+ currentSDL string+ wantCommentDiffCount int+ wantNewComment string+ }{+ {+ name: "add view comment",+ previousSDL: `+CREATE VIEW user_view AS SELECT * FROM users;`,+ currentSDL: `+CREATE VIEW user_view AS SELECT * FROM users;++COMMENT ON VIEW user_view IS 'User view description';`,+ wantCommentDiffCount: 1,+ wantNewComment: "User view description",+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil, nil)+ require.NoError(t, err)+ require.NotNil(t, diff)++ require.Equal(t, tt.wantCommentDiffCount, len(diff.CommentChanges), "unexpected number of comment diffs")++ if tt.wantCommentDiffCount > 0 {+ commentDiff := diff.CommentChanges[0]+ require.Equal(t, schema.CommentObjectTypeView, commentDiff.ObjectType, "unexpected object type")+ require.Equal(t, tt.wantNewComment, commentDiff.NewComment, "unexpected new comment")+ }+ })+ }+}++// TestCommentDiff_SequenceComment tests sequence-level comment changes+func TestCommentDiff_SequenceComment(t *testing.T) {+ tests := []struct {+ name string+ previousSDL string+ currentSDL string+ wantCommentDiffCount int+ wantNewComment string+ }{+ {+ name: "add sequence comment",+ previousSDL: `+CREATE SEQUENCE user_id_seq;`,+ currentSDL: `+CREATE SEQUENCE user_id_seq;++COMMENT ON SEQUENCE user_id_seq IS 'User ID sequence';`,+ wantCommentDiffCount: 1,+ wantNewComment: "User ID sequence",+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil, nil)+ require.NoError(t, err)+ require.NotNil(t, diff)++ require.Equal(t, tt.wantCommentDiffCount, len(diff.CommentChanges), "unexpected number of comment diffs")++ if tt.wantCommentDiffCount > 0 {+ commentDiff := diff.CommentChanges[0]+ require.Equal(t, schema.CommentObjectTypeSequence, commentDiff.ObjectType, "unexpected object type")+ require.Equal(t, tt.wantNewComment, commentDiff.NewComment, "unexpected new comment")+ }+ })+ }+}++// TestCommentDiff_FunctionComment tests function-level comment changes+func TestCommentDiff_FunctionComment(t *testing.T) {+ tests := []struct {+ name string+ previousSDL string+ currentSDL string+ wantCommentDiffCount int+ wantNewComment string+ }{+ {+ name: "add function comment",+ previousSDL: `+CREATE FUNCTION add_numbers(a INT, b INT) RETURNS INT AS $$+BEGIN+ RETURN a + b;+END;+$$ LANGUAGE plpgsql;`,+ currentSDL: `+CREATE FUNCTION add_numbers(a INT, b INT) RETURNS INT AS $$+BEGIN+ RETURN a + b;+END;+$$ LANGUAGE plpgsql;++COMMENT ON FUNCTION add_numbers(INT, INT) IS 'Adds two numbers';`,+ wantCommentDiffCount: 1,+ wantNewComment: "Adds two numbers",+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil, nil)+ require.NoError(t, err)+ require.NotNil(t, diff)++ require.Equal(t, tt.wantCommentDiffCount, len(diff.CommentChanges), "unexpected number of comment diffs")++ if tt.wantCommentDiffCount > 0 {+ commentDiff := diff.CommentChanges[0]+ require.Equal(t, schema.CommentObjectTypeFunction, commentDiff.ObjectType, "unexpected object type")+ require.Equal(t, tt.wantNewComment, commentDiff.NewComment, "unexpected new comment")+ }+ })+ }+}++// TestCommentDiff_ObjectCreateWithComment tests that creating an object with comment doesn't generate CommentDiff+func TestCommentDiff_ObjectCreateWithComment(t *testing.T) {+ tests := []struct {+ name string+ previousSDL string+ currentSDL string+ wantTableDiffCount int+ wantCommentDiffCount int+ }{+ {+ name: "create table with comment",+ previousSDL: ``,+ currentSDL: `+CREATE TABLE users (+ id INT PRIMARY KEY,+ name TEXT+);++COMMENT ON TABLE users IS 'User table';`,+ wantTableDiffCount: 1,+ wantCommentDiffCount: 0, // No comment diff because table is created+ },+ {+ name: "create table with column comment",+ previousSDL: ``,+ currentSDL: `+CREATE TABLE users (+ id INT PRIMARY KEY,+ name TEXT+);++COMMENT ON COLUMN users.name IS 'User name';`,+ wantTableDiffCount: 1,+ wantCommentDiffCount: 0, // No comment diff because table is created+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil, nil)+ require.NoError(t, err)+ require.NotNil(t, diff)++ require.Equal(t, tt.wantTableDiffCount, len(diff.TableChanges), "unexpected number of table diffs")+ require.Equal(t, tt.wantCommentDiffCount, len(diff.CommentChanges), "unexpected number of comment diffs")++ if tt.wantTableDiffCount > 0 {+ require.Equal(t, schema.MetadataDiffActionCreate, diff.TableChanges[0].Action, "expected CREATE action")+ }+ })+ }+}++// TestCommentDiff_ObjectDropWithComment tests that dropping an object with comment doesn't generate CommentDiff+func TestCommentDiff_ObjectDropWithComment(t *testing.T) {+ tests := []struct {+ name string+ previousSDL string+ currentSDL string+ wantTableDiffCount int+ wantCommentDiffCount int+ }{+ {+ name: "drop table with comment",+ previousSDL: `+CREATE TABLE users (+ id INT PRIMARY KEY,+ name TEXT+);++COMMENT ON TABLE users IS 'User table';`,+ currentSDL: ``,+ wantTableDiffCount: 1,+ wantCommentDiffCount: 0, // No comment diff because table is dropped+ },+ {+ name: "drop table with column comment",+ previousSDL: `+CREATE TABLE users (+ id INT PRIMARY KEY,+ name TEXT+);++COMMENT ON COLUMN users.name IS 'User name';`,+ currentSDL: ``,+ wantTableDiffCount: 1,+ wantCommentDiffCount: 0, // No comment diff because table is dropped+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil, nil)+ require.NoError(t, err)+ require.NotNil(t, diff)++ require.Equal(t, tt.wantTableDiffCount, len(diff.TableChanges), "unexpected number of table diffs")+ require.Equal(t, tt.wantCommentDiffCount, len(diff.CommentChanges), "unexpected number of comment diffs")++ if tt.wantTableDiffCount > 0 {+ require.Equal(t, schema.MetadataDiffActionDrop, diff.TableChanges[0].Action, "expected DROP action")+ }+ })+ }+}++// TestCommentDiff_CommentOnlyChange tests that comment-only changes don't trigger object diffs+func TestCommentDiff_CommentOnlyChange(t *testing.T) {+ tests := []struct {+ name string+ previousSDL string+ currentSDL string+ wantTableDiffCount int+ wantCommentDiffCount int+ }{+ {+ name: "only table comment changes",+ previousSDL: `+CREATE TABLE users (+ id INT PRIMARY KEY,+ name TEXT+);++COMMENT ON TABLE users IS 'Old comment';`,+ currentSDL: `+CREATE TABLE users (+ id INT PRIMARY KEY,+ name TEXT+);++COMMENT ON TABLE users IS 'New comment';`,+ wantTableDiffCount: 0, // No table diff, only comment changed+ wantCommentDiffCount: 1,+ },+ {+ name: "only column comment changes",+ previousSDL: `+CREATE TABLE users (+ id INT PRIMARY KEY,+ name TEXT+);++COMMENT ON COLUMN users.name IS 'Old name';`,+ currentSDL: `+CREATE TABLE users (+ id INT PRIMARY KEY,+ name TEXT+);++COMMENT ON COLUMN users.name IS 'New name';`,+ wantTableDiffCount: 0, // No table diff, only comment changed+ wantCommentDiffCount: 1,+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil, nil)+ require.NoError(t, err)+ require.NotNil(t, diff)++ require.Equal(t, tt.wantTableDiffCount, len(diff.TableChanges), "unexpected number of table diffs")+ require.Equal(t, tt.wantCommentDiffCount, len(diff.CommentChanges), "unexpected number of comment diffs")+ })+ }+}++// TestCommentDiff_TableChangeWithCommentChange tests table changes with comment changes+func TestCommentDiff_TableChangeWithCommentChange(t *testing.T) {+ tests := []struct {+ name string+ previousSDL string+ currentSDL string+ wantTableDiffCount int+ wantCommentDiffCount int+ }{+ {+ name: "add column and change comment",+ previousSDL: `+CREATE TABLE users (+ id INT PRIMARY KEY+);++COMMENT ON TABLE users IS 'Old comment';`,+ currentSDL: `+CREATE TABLE users (+ id INT PRIMARY KEY,+ name TEXT+);++COMMENT ON TABLE users IS 'New comment';`,+ wantTableDiffCount: 1, // Table changed (column added)+ wantCommentDiffCount: 1, // Comment also changed+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil, nil)+ require.NoError(t, err)+ require.NotNil(t, diff)++ require.Equal(t, tt.wantTableDiffCount, len(diff.TableChanges), "unexpected number of table diffs")+ require.Equal(t, tt.wantCommentDiffCount, len(diff.CommentChanges), "unexpected number of comment diffs")+ })+ }+}++// TestCommentDiff_SpecialCharacters tests comment handling with special characters+func TestCommentDiff_SpecialCharacters(t *testing.T) {+ tests := []struct {+ name string+ previousSDL string+ currentSDL string+ wantNewComment string+ }{+ {+ name: "comment with single quote",+ previousSDL: `+CREATE TABLE users (+ id INT PRIMARY KEY+);`,+ currentSDL: `+CREATE TABLE users (+ id INT PRIMARY KEY+);++COMMENT ON TABLE users IS 'User''s table';`,+ wantNewComment: "User''s table", // Escaped single quote+ },+ {+ name: "comment with newlines and special chars",+ previousSDL: `+CREATE TABLE users (+ id INT PRIMARY KEY+);`,+ currentSDL: `+CREATE TABLE users (+ id INT PRIMARY KEY+);++COMMENT ON TABLE users IS 'Line 1+Line 2';`,+ wantNewComment: "Line 1\nLine 2",+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil, nil)+ require.NoError(t, err)+ require.NotNil(t, diff)++ require.Equal(t, 1, len(diff.CommentChanges), "unexpected number of comment diffs")+ require.Equal(t, tt.wantNewComment, diff.CommentChanges[0].NewComment, "unexpected new comment")+ })+ }+}++// TestCommentDiff_SchemaQualified tests schema-qualified object names+func TestCommentDiff_SchemaQualified(t *testing.T) {+ tests := []struct {+ name string+ previousSDL string+ currentSDL string+ wantSchemaName string+ wantObjectName string+ }{+ {+ name: "schema qualified table",+ previousSDL: `+CREATE TABLE myschema.users (+ id INT PRIMARY KEY+);`,+ currentSDL: `+CREATE TABLE myschema.users (+ id INT PRIMARY KEY+);++COMMENT ON TABLE myschema.users IS 'Users table';`,+ wantSchemaName: "myschema",+ wantObjectName: "users",+ },+ {+ name: "schema qualified column",+ previousSDL: `+CREATE TABLE myschema.users (+ id INT PRIMARY KEY,+ name TEXT+);`,+ currentSDL: `+CREATE TABLE myschema.users (+ id INT PRIMARY KEY,+ name TEXT+);++COMMENT ON COLUMN myschema.users.name IS 'User name';`,+ wantSchemaName: "myschema",+ wantObjectName: "users",+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil, nil)+ require.NoError(t, err)+ require.NotNil(t, diff)++ require.Equal(t, 1, len(diff.CommentChanges), "unexpected number of comment diffs")+ require.Equal(t, tt.wantSchemaName, diff.CommentChanges[0].SchemaName, "unexpected schema name")+ require.Equal(t, tt.wantObjectName, diff.CommentChanges[0].ObjectName, "unexpected object name")+ })+ }+}diff --git a/backend/plugin/schema/sdl_differ.go b/backend/plugin/schema/sdl_differ.goindex 9a55f5c14d..a55a9543c1 100644--- a/backend/plugin/schema/sdl_differ.go+++ b/backend/plugin/schema/sdl_differ.go@@ -5,13 +5,15 @@ import ()type SDLChunk struct {- Identifier string- ASTNode antlr.ParserRuleContext // The parsed AST node for this chunk (e.g., CREATE SEQUENCE)- AlterStatements []antlr.ParserRuleContext // Additional ALTER statements for this object (e.g., ALTER SEQUENCE OWNED BY)+ Identifier string+ ASTNode antlr.ParserRuleContext // The parsed AST node for this chunk (e.g., CREATE SEQUENCE)+ AlterStatements []antlr.ParserRuleContext // Additional ALTER statements for this object (e.g., ALTER SEQUENCE OWNED BY)+ CommentStatements []antlr.ParserRuleContext // COMMENT ON statements for this object}// GetText extracts text from the AST node using its own token stream// For chunks with ALTER statements, it combines the main statement with all ALTER statements+// For chunks with COMMENT statements, it combines them as wellfunc (c *SDLChunk) GetText() string {if c.ASTNode == nil {return ""@@ -20,12 +22,52 @@ func (c *SDLChunk) GetText() string {// Extract text from the main AST node (e.g., CREATE SEQUENCE)mainText := extractTextFromNode(c.ASTNode)+ // Collect all parts: main, ALTER statements, and COMMENT statements+ parts := []string{mainText}++ // Add ALTER statements+ for _, alterNode := range c.AlterStatements {+ alterText := extractTextFromNode(alterNode)+ if alterText != "" {+ parts = append(parts, alterText)+ }+ }++ // Add COMMENT statements+ for _, commentNode := range c.CommentStatements {+ commentText := extractTextFromNode(commentNode)+ if commentText != "" {+ parts = append(parts, commentText)+ }+ }++ // Join with double newline to match SDL format+ result := ""+ for i, part := range parts {+ if i > 0 {+ result += "\n\n"+ }+ result += part+ }+ return result+}++// GetTextWithoutComments extracts text excluding COMMENT ON statements+// This is used for comparing object definitions without considering comments+func (c *SDLChunk) GetTextWithoutComments() string {+ if c.ASTNode == nil {+ return ""+ }++ // Extract text from the main AST node+ mainText := extractTextFromNode(c.ASTNode)+// If there are no ALTER statements, return just the main textif len(c.AlterStatements) == 0 {return mainText}- // Combine main statement with ALTER statements+ // Combine main statement with ALTER statements (excluding COMMENT statements)parts := []string{mainText}for _, alterNode := range c.AlterStatements {alterText := extractTextFromNode(alterNode)@@ -80,6 +122,13 @@ type SDLChunks struct {Functions map[string]*SDLChunk // key: function name/identifierIndexes map[string]*SDLChunk // key: index name/identifierSequences map[string]*SDLChunk // key: sequence name/identifier+ Schemas map[string]*SDLChunk // key: schema name/identifier++ // Column comments: map[schemaName.tableName][columnName] -> COMMENT ON COLUMN AST node+ ColumnComments map[string]map[string]antlr.ParserRuleContext++ // Index comments for table-level indexes: map[schemaName.tableName][indexName] -> COMMENT ON INDEX AST node+ IndexComments map[string]map[string]antlr.ParserRuleContext}type SDLDiff struct {