Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix: handle new object with comment (#17816)
rebeliceaab4df87df06
diff --git a/backend/plugin/schema/pg/comment_migration_test.go b/backend/plugin/schema/pg/comment_migration_test.goindex de9f9cb293..e727745568 100644--- a/backend/plugin/schema/pg/comment_migration_test.go+++ b/backend/plugin/schema/pg/comment_migration_test.go@@ -426,3 +426,318 @@ COMMENT ON TABLE "public"."users" IS 'Basic user table';require.Contains(t, migrationSQL, "email")require.Contains(t, migrationSQL, "User table with email")}++func TestFunctionMigration_NewFunctionWithComment(t *testing.T) {+ currentSDL := `+CREATE FUNCTION "public"."calculate_tax"(amount numeric) RETURNS numeric+ LANGUAGE plpgsql+ AS $$+BEGIN+ RETURN amount * 0.1;+END;+$$;++COMMENT ON FUNCTION "public"."calculate_tax"(numeric) IS 'Calculate 10% tax';+`++ previousSDL := ``++ diff, err := GetSDLDiff(currentSDL, 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 FUNCTION and COMMENT ON FUNCTION+ require.Contains(t, migrationSQL, "CREATE FUNCTION")+ require.Contains(t, migrationSQL, "COMMENT ON FUNCTION")+ require.Contains(t, migrationSQL, "calculate_tax")+ require.Contains(t, migrationSQL, "Calculate 10% tax")+}++func TestViewMigration_NewViewWithComment(t *testing.T) {+ currentSDL := `+CREATE TABLE "public"."users" (+ "id" integer NOT NULL,+ "name" text,+ "active" boolean+);++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 := `+CREATE TABLE "public"."users" (+ "id" integer NOT NULL,+ "name" text,+ "active" boolean+);+`++ diff, err := GetSDLDiff(currentSDL, 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")+ require.Contains(t, migrationSQL, "COMMENT ON VIEW")+ require.Contains(t, migrationSQL, "active_users")+ require.Contains(t, migrationSQL, "View of all active users")+}++func TestIndexMigration_NewIndexWithComment(t *testing.T) {+ 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';+`++ previousSDL := `+CREATE TABLE "public"."products" (+ "id" integer NOT NULL,+ "name" text,+ "price" numeric+);+`++ diff, err := GetSDLDiff(currentSDL, 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 INDEX and COMMENT ON INDEX+ require.Contains(t, migrationSQL, "CREATE INDEX")+ require.Contains(t, migrationSQL, "COMMENT ON INDEX")+ require.Contains(t, migrationSQL, "idx_products_name")+ require.Contains(t, migrationSQL, "Index for product name lookup")+}++func TestTableMigration_NewTableWithComment(t *testing.T) {+ currentSDL := `+CREATE TABLE "public"."users" (+ "id" integer NOT NULL,+ "name" text,+ "email" varchar(255)+);++COMMENT ON TABLE "public"."users" IS 'User information table';+`+ previousSDL := ``++ diff, err := GetSDLDiff(currentSDL, 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 TABLE and COMMENT ON TABLE+ require.Contains(t, migrationSQL, "CREATE TABLE")+ require.Contains(t, migrationSQL, "COMMENT ON TABLE")+ require.Contains(t, migrationSQL, "users")+ require.Contains(t, migrationSQL, "User information table")+}++func TestColumnMigration_AddColumnWithComment(t *testing.T) {+ currentSDL := `+CREATE TABLE "public"."users" (+ "id" integer NOT NULL,+ "name" text,+ "email" varchar(255)+);++COMMENT ON COLUMN "public"."users"."email" IS 'User email address';+`+ previousSDL := `+CREATE TABLE "public"."users" (+ "id" integer NOT NULL,+ "name" text+);+`++ diff, err := GetSDLDiff(currentSDL, 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 ADD COLUMN and COMMENT ON COLUMN+ require.Contains(t, migrationSQL, "ADD COLUMN")+ require.Contains(t, migrationSQL, "COMMENT ON COLUMN")+ require.Contains(t, migrationSQL, "email")+ require.Contains(t, migrationSQL, "User email address")+}++func TestColumnMigration_AddMultipleColumnsWithComments(t *testing.T) {+ currentSDL := `+CREATE TABLE "public"."users" (+ "id" integer NOT NULL,+ "name" text,+ "email" varchar(255),+ "phone" varchar(20),+ "age" integer+);++COMMENT ON COLUMN "public"."users"."email" IS 'User email address';+COMMENT ON COLUMN "public"."users"."phone" IS 'User phone number';+`+ previousSDL := `+CREATE TABLE "public"."users" (+ "id" integer NOT NULL,+ "name" text+);+`++ diff, err := GetSDLDiff(currentSDL, 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 ADD COLUMN for all new columns+ require.Contains(t, migrationSQL, "ADD COLUMN")+ require.Contains(t, migrationSQL, "email")+ require.Contains(t, migrationSQL, "phone")+ require.Contains(t, migrationSQL, "age")++ // Should contain COMMENT ON COLUMN for columns with comments+ require.Contains(t, migrationSQL, "COMMENT ON COLUMN")+ require.Contains(t, migrationSQL, "User email address")+ require.Contains(t, migrationSQL, "User phone number")+}++func TestColumnMigration_AddColumnAndModifyExistingComment(t *testing.T) {+ currentSDL := `+CREATE TABLE "public"."users" (+ "id" integer NOT NULL,+ "name" text,+ "email" varchar(255)+);++COMMENT ON COLUMN "public"."users"."name" IS 'Updated: User full name';+COMMENT ON COLUMN "public"."users"."email" IS 'User email address';+`+ previousSDL := `+CREATE TABLE "public"."users" (+ "id" integer NOT NULL,+ "name" text+);++COMMENT ON COLUMN "public"."users"."name" IS 'User name';+`++ diff, err := GetSDLDiff(currentSDL, 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 ADD COLUMN for the new column+ require.Contains(t, migrationSQL, "ADD COLUMN")+ require.Contains(t, migrationSQL, "email")++ // Should contain COMMENT ON COLUMN for both new and modified comments+ require.Contains(t, migrationSQL, "COMMENT ON COLUMN")+ require.Contains(t, migrationSQL, "Updated: User full name")+ require.Contains(t, migrationSQL, "User email address")+}++func TestSequenceMigration_NewSequenceWithOwner(t *testing.T) {+ tests := []struct {+ name string+ currentSDL string+ previousSDL string+ wantSQL string+ }{+ {+ name: "add new sequence with owner",+ currentSDL: `+CREATE TABLE "public"."users" (+ "id" integer NOT NULL,+ "name" text+);++CREATE SEQUENCE "public"."user_id_seq" AS integer START WITH 1 INCREMENT BY 1;++ALTER SEQUENCE "public"."user_id_seq" OWNED BY "public"."users"."id";+`,+ previousSDL: `+CREATE TABLE "public"."users" (+ "id" integer NOT NULL,+ "name" text+);+`,+ wantSQL: `CREATE SEQUENCE "public"."user_id_seq" AS integer START WITH 1 INCREMENT BY 1;++ALTER SEQUENCE "public"."user_id_seq" OWNED BY "public"."users"."id";++`,+ },+ {+ name: "add new sequence with owner and comment",+ 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;++ALTER SEQUENCE "public"."product_id_seq" OWNED BY "public"."products"."id";++COMMENT ON SEQUENCE "public"."product_id_seq" IS 'Product ID sequence';+`,+ previousSDL: `+CREATE TABLE "public"."products" (+ "id" integer NOT NULL,+ "name" text+);+`,+ wantSQL: `CREATE SEQUENCE "public"."product_id_seq" AS bigint START WITH 100 INCREMENT BY 1;++ALTER SEQUENCE "public"."product_id_seq" OWNED BY "public"."products"."id";++COMMENT ON SEQUENCE "public"."product_id_seq" IS 'Product 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)++ migrationSQL, err := generateMigration(diff)+ require.NoError(t, err)++ t.Logf("Generated migration SQL:\n%s", migrationSQL)+ t.Logf("Expected SQL:\n%s", tt.wantSQL)++ require.Equal(t, tt.wantSQL, migrationSQL)+ })+ }+}diff --git a/backend/plugin/schema/pg/get_sdl_diff.go b/backend/plugin/schema/pg/get_sdl_diff.goindex fd99392a4a..3fd63b9251 100644--- a/backend/plugin/schema/pg/get_sdl_diff.go+++ b/backend/plugin/schema/pg/get_sdl_diff.go@@ -617,6 +617,42 @@ func processTableChanges(currentChunks, previousChunks *schema.SDLChunks, currenColumnChanges: []*schema.ColumnDiff{}, // Empty - table creation automatically creates all columns}diff.TableChanges = append(diff.TableChanges, tableDiff)+ // Add COMMENT ON TABLE 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.CommentObjectTypeTable,+ SchemaName: schemaName,+ ObjectName: tableName,+ OldComment: "",+ NewComment: commentText,+ OldASTNode: nil,+ NewASTNode: commentNode,+ })+ }+ }+ // Add COMMENT ON COLUMN diffs if they exist+ tableIdentifier := currentChunk.Identifier+ if columnComments := currentChunks.ColumnComments[tableIdentifier]; len(columnComments) > 0 {+ for columnName, commentNode := range columnComments {+ commentText := extractCommentTextFromNode(commentNode)+ if commentText != "" {+ diff.CommentChanges = append(diff.CommentChanges, &schema.CommentDiff{+ Action: schema.MetadataDiffActionCreate,+ ObjectType: schema.CommentObjectTypeColumn,+ SchemaName: schemaName,+ ObjectName: tableName,+ ColumnName: columnName,+ OldComment: "",+ NewComment: commentText,+ OldASTNode: nil,+ NewASTNode: commentNode,+ })+ }+ }+ }}}}@@ -1600,6 +1636,23 @@ func processStandaloneIndexChanges(currentChunks, previousChunks *schema.SDLChunAction: schema.MetadataDiffActionCreate,NewASTNode: currentChunk.ASTNode,})+ // Add COMMENT ON INDEX diffs if they exist+ 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,+ })+ }+ }}}@@ -1755,6 +1808,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,+ })+ }+ }}}@@ -1815,6 +1884,22 @@ func processFunctionChanges(currentChunks, previousChunks *schema.SDLChunks, curOldASTNode: nil,NewASTNode: currentChunk.ASTNode,})+ // Add COMMENT ON FUNCTION 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.CommentObjectTypeFunction,+ SchemaName: schemaName,+ ObjectName: functionName,+ OldComment: "",+ NewComment: commentText,+ OldASTNode: nil,+ NewASTNode: commentNode,+ })+ }+ }}}@@ -1968,6 +2053,7 @@ func processSequenceChanges(currentChunks, previousChunks *schema.SDLChunks, cur} else {// New sequenceschemaName, sequenceName := parseIdentifier(currentChunk.Identifier)+ // Add CREATE SEQUENCE diffdiff.SequenceChanges = append(diff.SequenceChanges, &schema.SequenceDiff{Action: schema.MetadataDiffActionCreate,SchemaName: schemaName,@@ -1977,6 +2063,36 @@ func processSequenceChanges(currentChunks, previousChunks *schema.SDLChunks, curOldASTNode: nil,NewASTNode: currentChunk.ASTNode,})+ // Add ALTER SEQUENCE OWNED BY diffs if they exist+ if len(currentChunk.AlterStatements) > 0 {+ for _, alterNode := range currentChunk.AlterStatements {+ diff.SequenceChanges = append(diff.SequenceChanges, &schema.SequenceDiff{+ Action: schema.MetadataDiffActionAlter,+ SchemaName: schemaName,+ SequenceName: sequenceName,+ OldSequence: nil,+ NewSequence: nil,+ OldASTNode: nil,+ NewASTNode: alterNode,+ })+ }+ }+ // Add COMMENT ON SEQUENCE 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.CommentObjectTypeSequence,+ SchemaName: schemaName,+ ObjectName: sequenceName,+ OldComment: "",+ NewComment: commentText,+ OldASTNode: nil,+ NewASTNode: commentNode,+ })+ }+ }}}diff --git a/backend/plugin/schema/pg/get_sdl_diff_comment_test.go b/backend/plugin/schema/pg/get_sdl_diff_comment_test.goindex eb4ba301d7..ed54e6df16 100644--- a/backend/plugin/schema/pg/get_sdl_diff_comment_test.go+++ b/backend/plugin/schema/pg/get_sdl_diff_comment_test.go@@ -347,7 +347,7 @@ COMMENT ON FUNCTION add_numbers(INT, INT) IS 'Adds two numbers';`,}}-// TestCommentDiff_ObjectCreateWithComment tests that creating an object with comment doesn't generate CommentDiff+// TestCommentDiff_ObjectCreateWithComment tests that creating an object with comment generates CommentDifffunc TestCommentDiff_ObjectCreateWithComment(t *testing.T) {tests := []struct {name string@@ -367,7 +367,7 @@ CREATE TABLE users (COMMENT ON TABLE users IS 'User table';`,wantTableDiffCount: 1,- wantCommentDiffCount: 0, // No comment diff because table is created+ wantCommentDiffCount: 1, // CommentDiff is generated for the table comment},{name: "create table with column comment",@@ -380,7 +380,7 @@ CREATE TABLE users (COMMENT ON COLUMN users.name IS 'User name';`,wantTableDiffCount: 1,- wantCommentDiffCount: 0, // No comment diff because table is created+ wantCommentDiffCount: 1, // CommentDiff is generated for the column comment},}