Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: handle comment in sdl drift creation (#17821)
rebelice1ab0e8090389
diff --git a/backend/plugin/schema/pg/comment_migration_test.go b/backend/plugin/schema/pg/comment_migration_test.goindex e727745568..0892165923 100644--- a/backend/plugin/schema/pg/comment_migration_test.go+++ b/backend/plugin/schema/pg/comment_migration_test.go@@ -4,6 +4,9 @@ import ("testing""github.com/stretchr/testify/require"++ storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/store/model")func TestCommentMigration_TableComment(t *testing.T) {@@ -458,7 +461,14 @@ COMMENT ON FUNCTION "public"."calculate_tax"(numeric) IS 'Calculate 10% tax';}func TestViewMigration_NewViewWithComment(t *testing.T) {- currentSDL := `+ tests := []struct {+ name string+ currentSDL string+ previousSDL string+ }{+ {+ name: "new view with comment - table exists in both",+ currentSDL: `CREATE TABLE "public"."users" ("id" integer NOT NULL,"name" text,@@ -468,17 +478,175 @@ CREATE TABLE "public"."users" (CREATE VIEW "public"."active_users" AS SELECT * FROM users WHERE active = true;COMMENT ON VIEW "public"."active_users" IS 'View of all active users';-`-- previousSDL := `+`,+ previousSDL: `CREATE TABLE "public"."users" ("id" integer NOT NULL,"name" text,"active" boolean);+`,+ },+ {+ name: "new view with comment - realistic scenario with JOIN",+ currentSDL: `+CREATE TABLE "public"."test_table" (+ "id" integer NOT NULL,+ "name" text,+ "created_at" timestamp,+ "is_active" boolean,+ "description" text+);++CREATE TABLE "public"."test_r_table" (+ "id" integer NOT NULL,+ "r_name" text,+ "r_value" text+);++CREATE VIEW "public"."test_view" AS+SELECT+ t.id,+ t.name,+ t.created_at,+ t.is_active,+ t.description,+ r.r_name,+ r.r_value+FROM+ "public"."test_table" t+JOIN+ "public"."test_r_table" r ON t.id = r.id;++COMMENT ON VIEW "public"."test_view" IS 'A view that combines test_table and test_r_table for easier';+`,+ previousSDL: `+CREATE TABLE "public"."test_table" (+ "id" integer NOT NULL,+ "name" text,+ "created_at" timestamp,+ "is_active" boolean,+ "description" text+);++CREATE TABLE "public"."test_r_table" (+ "id" integer NOT NULL,+ "r_name" text,+ "r_value" text+);+`,+ },+ }++ 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)++ migrationSQL, err := generateMigration(diff)+ require.NoError(t, err)++ t.Logf("Generated migration SQL:\n%s", migrationSQL)++ // Should contain both CREATE VIEW and COMMENT ON VIEW+ require.Contains(t, migrationSQL, "CREATE VIEW", "Migration should contain CREATE VIEW")+ require.Contains(t, migrationSQL, "COMMENT ON VIEW", "Migration should contain COMMENT ON VIEW")+ })+ }+}++func TestViewMigration_NewViewWithComment_DriftScenario(t *testing.T) {+ // This test simulates the drift scenario where schemas are provided+ // This matches what happens in real execution (schema_update_sdl_executor.go)++ previousSDL := `+CREATE TABLE "public"."test_table" (+ "id" integer NOT NULL,+ "name" text+);++CREATE TABLE "public"."test_r_table" (+ "id" integer NOT NULL,+ "r_name" text,+ "r_value" text+);`- diff, err := GetSDLDiff(currentSDL, previousSDL, nil, nil)+ currentSDL := `+CREATE TABLE "public"."test_table" (+ "id" integer NOT NULL,+ "name" text+);++CREATE TABLE "public"."test_r_table" (+ "id" integer NOT NULL,+ "r_name" text,+ "r_value" text+);++CREATE VIEW "public"."test_view" AS+SELECT+ t.id,+ t.name,+ r.r_name,+ r.r_value+FROM+ "public"."test_table" t+JOIN+ "public"."test_r_table" r ON t.id = r.id;++COMMENT ON VIEW "public"."test_view" IS 'A view that combines test_table and test_r_table';+`++ // Create mock schemas with view metadata+ // Previous schema: no views+ previousSchema := model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "testdb",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ },+ },+ },+ []byte{},+ &storepb.DatabaseConfig{},+ storepb.Engine_POSTGRES,+ false,+ )++ // Current schema: includes view with comment+ currentSchema := model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "testdb",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Views: []*storepb.ViewMetadata{+ {+ Name: "test_view",+ Definition: `SELECT+ t.id,+ t.name,+ r.r_name,+ r.r_value+FROM+ "public"."test_table" t+JOIN+ "public"."test_r_table" r ON t.id = r.id;`,+ Comment: "A view that combines test_table and test_r_table",+ },+ },+ },+ },+ },+ []byte{},+ &storepb.DatabaseConfig{},+ storepb.Engine_POSTGRES,+ false,+ )++ diff, err := GetSDLDiff(currentSDL, previousSDL, currentSchema, previousSchema)require.NoError(t, err)migrationSQL, err := generateMigration(diff)@@ -487,10 +655,10 @@ CREATE TABLE "public"."users" (t.Logf("Generated migration SQL:\n%s", migrationSQL)// Should contain both CREATE VIEW and COMMENT ON VIEW- require.Contains(t, migrationSQL, "CREATE VIEW")- require.Contains(t, migrationSQL, "COMMENT ON VIEW")- require.Contains(t, migrationSQL, "active_users")- require.Contains(t, migrationSQL, "View of all active users")+ require.Contains(t, migrationSQL, "CREATE VIEW", "Migration should contain CREATE VIEW")+ require.Contains(t, migrationSQL, "COMMENT ON VIEW", "Migration should contain COMMENT ON VIEW")+ require.Contains(t, migrationSQL, "test_view", "Migration should reference test_view")+ require.Contains(t, migrationSQL, "A view that combines test_table and test_r_table", "Migration should contain the comment text")}func TestIndexMigration_NewIndexWithComment(t *testing.T) {@@ -741,3 +909,232 @@ COMMENT ON SEQUENCE "public"."product_id_seq" IS 'Product ID sequence';})}}++func TestIndexMigration_NewIndexWithComment_DriftScenario(t *testing.T) {+ // This test simulates the drift scenario where an index with comment+ // is created from database metadata (not in previous SDL)++ previousSDL := `+CREATE TABLE "public"."products" (+ "id" integer NOT NULL,+ "name" text,+ "price" numeric+);+`++ currentSDL := `+CREATE TABLE "public"."products" (+ "id" integer NOT NULL,+ "name" text,+ "price" numeric+);++CREATE INDEX "idx_products_name" ON "public"."products" (name);++COMMENT ON INDEX "public"."idx_products_name" IS 'Index for product name lookup';+`++ // Previous schema: table with columns but no indexes+ previousSchema := model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "testdb",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Tables: []*storepb.TableMetadata{+ {+ Name: "products",+ Columns: []*storepb.ColumnMetadata{+ {+ Name: "id",+ Type: "integer",+ Nullable: false,+ },+ {+ Name: "name",+ Type: "text",+ },+ {+ Name: "price",+ Type: "numeric",+ },+ },+ },+ },+ },+ },+ },+ []byte{},+ &storepb.DatabaseConfig{},+ storepb.Engine_POSTGRES,+ false,+ )++ // Current schema: includes index with comment+ currentSchema := model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "testdb",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Tables: []*storepb.TableMetadata{+ {+ Name: "products",+ Columns: []*storepb.ColumnMetadata{+ {+ Name: "id",+ Type: "integer",+ Nullable: false,+ },+ {+ Name: "name",+ Type: "text",+ },+ {+ Name: "price",+ Type: "numeric",+ },+ },+ Indexes: []*storepb.IndexMetadata{+ {+ Name: "idx_products_name",+ Expressions: []string{"name"},+ Unique: false,+ Comment: "Index for product name lookup",+ },+ },+ },+ },+ },+ },+ },+ []byte{},+ &storepb.DatabaseConfig{},+ storepb.Engine_POSTGRES,+ false,+ )++ diff, err := GetSDLDiff(currentSDL, previousSDL, currentSchema, previousSchema)+ require.NoError(t, err)++ migrationSQL, err := generateMigration(diff)+ require.NoError(t, err)++ t.Logf("Generated migration SQL:\n%s", migrationSQL)++ // Should contain both CREATE INDEX and COMMENT ON INDEX+ require.Contains(t, migrationSQL, "CREATE INDEX", "Migration should contain CREATE INDEX")+ require.Contains(t, migrationSQL, "COMMENT ON INDEX", "Migration should contain COMMENT ON INDEX")+ require.Contains(t, migrationSQL, "idx_products_name", "Migration should reference idx_products_name")+ require.Contains(t, migrationSQL, "Index for product name lookup", "Migration should contain the comment text")+}++func TestSequenceMigration_NewSequenceWithComment_DriftScenario(t *testing.T) {+ // This test simulates the drift scenario where a sequence with comment+ // is created from database metadata (not in previous SDL)++ previousSDL := `+CREATE TABLE "public"."products" (+ "id" integer NOT NULL,+ "name" text+);+`++ currentSDL := `+CREATE TABLE "public"."products" (+ "id" integer NOT NULL,+ "name" text+);++CREATE SEQUENCE "public"."product_id_seq" AS bigint START WITH 100 INCREMENT BY 1;++COMMENT ON SEQUENCE "public"."product_id_seq" IS 'Product ID sequence';+`++ // Previous schema: table but no sequences+ previousSchema := model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "testdb",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Tables: []*storepb.TableMetadata{+ {+ Name: "products",+ Columns: []*storepb.ColumnMetadata{+ {+ Name: "id",+ Type: "integer",+ Nullable: false,+ },+ {+ Name: "name",+ Type: "text",+ },+ },+ },+ },+ },+ },+ },+ []byte{},+ &storepb.DatabaseConfig{},+ storepb.Engine_POSTGRES,+ false,+ )++ // Current schema: table and sequence with comment+ currentSchema := model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "testdb",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Tables: []*storepb.TableMetadata{+ {+ Name: "products",+ Columns: []*storepb.ColumnMetadata{+ {+ Name: "id",+ Type: "integer",+ Nullable: false,+ },+ {+ Name: "name",+ Type: "text",+ },+ },+ },+ },+ Sequences: []*storepb.SequenceMetadata{+ {+ Name: "product_id_seq",+ DataType: "bigint",+ Start: "100",+ Increment: "1",+ Comment: "Product ID sequence",+ },+ },+ },+ },+ },+ []byte{},+ &storepb.DatabaseConfig{},+ storepb.Engine_POSTGRES,+ false,+ )++ diff, err := GetSDLDiff(currentSDL, previousSDL, currentSchema, previousSchema)+ require.NoError(t, err)++ migrationSQL, err := generateMigration(diff)+ require.NoError(t, err)++ t.Logf("Generated migration SQL:\n%s", migrationSQL)++ // Should contain both CREATE SEQUENCE and COMMENT ON SEQUENCE+ require.Contains(t, migrationSQL, "CREATE SEQUENCE", "Migration should contain CREATE SEQUENCE")+ require.Contains(t, migrationSQL, "COMMENT ON SEQUENCE", "Migration should contain COMMENT ON SEQUENCE")+ require.Contains(t, migrationSQL, "product_id_seq", "Migration should reference product_id_seq")+ require.Contains(t, migrationSQL, "Product ID sequence", "Migration should contain the comment text")+}diff --git a/backend/plugin/schema/pg/get_sdl_diff.go b/backend/plugin/schema/pg/get_sdl_diff.goindex 3fd63b9251..038386b70f 100644--- a/backend/plugin/schema/pg/get_sdl_diff.go+++ b/backend/plugin/schema/pg/get_sdl_diff.go@@ -1627,6 +1627,23 @@ func processStandaloneIndexChanges(currentChunks, previousChunks *schema.SDLChunAction: schema.MetadataDiffActionCreate,NewASTNode: currentChunk.ASTNode,})+ // Add COMMENT ON INDEX diffs if they exist in the new version+ if len(currentChunk.CommentStatements) > 0 {+ schemaName, indexName := parseIdentifier(currentChunk.Identifier)+ for _, commentNode := range currentChunk.CommentStatements {+ commentText := extractCommentTextFromNode(commentNode)+ diff.CommentChanges = append(diff.CommentChanges, &schema.CommentDiff{+ Action: schema.MetadataDiffActionCreate,+ ObjectType: schema.CommentObjectTypeIndex,+ SchemaName: schemaName,+ ObjectName: indexName,+ OldComment: "",+ NewComment: commentText,+ OldASTNode: nil,+ NewASTNode: commentNode,+ })+ }+ }}// If text is identical, skip - no changes detected} else {@@ -1794,6 +1811,22 @@ func processViewChanges(currentChunks, previousChunks *schema.SDLChunks, currentOldASTNode: nil,NewASTNode: currentChunk.ASTNode,})+ // Add COMMENT ON VIEW diffs if they exist+ if len(currentChunk.CommentStatements) > 0 {+ for _, commentNode := range currentChunk.CommentStatements {+ commentText := extractCommentTextFromNode(commentNode)+ diff.CommentChanges = append(diff.CommentChanges, &schema.CommentDiff{+ Action: schema.MetadataDiffActionCreate,+ ObjectType: schema.CommentObjectTypeView,+ SchemaName: schemaName,+ ObjectName: viewName,+ OldComment: "",+ NewComment: commentText,+ OldASTNode: nil,+ NewASTNode: commentNode,+ })+ }+ }}// If text is identical, skip - no changes detected} else {@@ -1982,6 +2015,22 @@ func processSequenceChanges(currentChunks, previousChunks *schema.SDLChunks, cur})}}+ // Add COMMENT ON SEQUENCE diffs if they exist in the new version+ if len(currentChunk.CommentStatements) > 0 {+ for _, commentNode := range currentChunk.CommentStatements {+ commentText := extractCommentTextFromNode(commentNode)+ diff.CommentChanges = append(diff.CommentChanges, &schema.CommentDiff{+ Action: schema.MetadataDiffActionCreate,+ ObjectType: schema.CommentObjectTypeSequence,+ SchemaName: schemaName,+ ObjectName: sequenceName,+ OldComment: "",+ NewComment: commentText,+ OldASTNode: nil,+ NewASTNode: commentNode,+ })+ }+ }} else if createChanged {// Only CREATE changed - use drop and recreatediff.SequenceChanges = append(diff.SequenceChanges, &schema.SequenceDiff{@@ -2016,6 +2065,22 @@ func processSequenceChanges(currentChunks, previousChunks *schema.SDLChunks, cur})}}+ // Add COMMENT ON SEQUENCE diffs if they exist in the new version+ if len(currentChunk.CommentStatements) > 0 {+ for _, commentNode := range currentChunk.CommentStatements {+ commentText := extractCommentTextFromNode(commentNode)+ diff.CommentChanges = append(diff.CommentChanges, &schema.CommentDiff{+ Action: schema.MetadataDiffActionCreate,+ ObjectType: schema.CommentObjectTypeSequence,+ SchemaName: schemaName,+ ObjectName: sequenceName,+ OldComment: "",+ NewComment: commentText,+ OldASTNode: nil,+ NewASTNode: commentNode,+ })+ }+ }} else if alterChanged {// Only ALTER changed - generate ALTER statements// This handles ownership changes without recreating the sequence@@ -3673,6 +3738,17 @@ func createIndexChunk(chunks *schema.SDLChunks, extIndex *extendedIndexMetadata,ASTNode: indexASTNode,}+ // Add COMMENT ON INDEX if the index has a comment+ if extIndex.Comment != "" {+ schemaName := extIndex.SchemaName+ if schemaName == "" {+ schemaName = "public"+ }+ if err := syncObjectCommentStatements(chunk, extIndex.Comment, "INDEX", schemaName, extIndex.Name); err != nil {+ return errors.Wrapf(err, "failed to add COMMENT statements for index %s", indexKey)+ }+ }+if chunks.Indexes == nil {chunks.Indexes = make(map[string]*schema.SDLChunk)}@@ -4002,6 +4078,14 @@ func createFunctionChunk(chunks *schema.SDLChunks, function *storepb.FunctionMetASTNode: astNode,}+ // Add COMMENT ON FUNCTION if the function has a comment+ if function.Comment != "" {+ schemaName, functionName := parseIdentifier(functionKey)+ if err := syncObjectCommentStatements(chunk, function.Comment, "FUNCTION", schemaName, functionName); err != nil {+ return errors.Wrapf(err, "failed to add COMMENT statements for function %s", functionKey)+ }+ }+// Ensure Functions map is initializedif chunks.Functions == nil {chunks.Functions = make(map[string]*schema.SDLChunk)@@ -4296,6 +4380,13 @@ func createSequenceChunk(chunks *schema.SDLChunks, sequence *storepb.SequenceMetASTNode: astNode,}+ // Add COMMENT ON SEQUENCE if the sequence has a comment+ if sequence.Comment != "" {+ if err := syncCommentStatements(chunk, sequence, schemaName); err != nil {+ return errors.Wrapf(err, "failed to add COMMENT statements for sequence %s", sequenceKey)+ }+ }+// Ensure Sequences map is initializedif chunks.Sequences == nil {chunks.Sequences = make(map[string]*schema.SDLChunk)@@ -5027,6 +5118,23 @@ func createViewChunk(chunks *schema.SDLChunks, view *storepb.ViewMetadata, viewKASTNode: viewASTNode,}+ // Add comment if the view has one+ if view.Comment != "" {+ commentSQL := generateCommentOnViewSQL(schemaName, view.Name, view.Comment)+ commentParseResult, err := pgparser.ParsePostgreSQL(commentSQL)+ if err == nil && commentParseResult.Tree != nil {+ // Extract COMMENT ON VIEW AST node+ var commentASTNode *parser.CommentstmtContext+ antlr.ParseTreeWalkerDefault.Walk(&commentExtractor{+ result: &commentASTNode,+ }, commentParseResult.Tree)++ if commentASTNode != nil {+ chunk.CommentStatements = []antlr.ParserRuleContext{commentASTNode}+ }+ }+ }+if chunks.Views == nil {chunks.Views = make(map[string]*schema.SDLChunk)}@@ -5125,6 +5233,16 @@ func generateCreateViewSDL(schemaName string, view *storepb.ViewMetadata) stringreturn buf.String()}+// generateCommentOnViewSQL generates a COMMENT ON VIEW statement+func generateCommentOnViewSQL(schemaName, viewName, comment string) string {+ if schemaName == "" {+ schemaName = "public"+ }+ // Escape single quotes in comment+ escapedComment := strings.ReplaceAll(comment, "'", "''")+ return fmt.Sprintf("COMMENT ON VIEW \"%s\".\"%s\" IS '%s';", schemaName, viewName, escapedComment)+}+// viewExtractor is a walker to extract CREATE VIEW AST nodestype viewExtractor struct {parser.BasePostgreSQLParserListener