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: ignore comment when drop column (#17860)
rebelice5271b888b81b
diff --git a/backend/plugin/schema/pg/generate_migration.go b/backend/plugin/schema/pg/generate_migration.goindex 4409db7770..992c3c86ff 100644--- a/backend/plugin/schema/pg/generate_migration.go+++ b/backend/plugin/schema/pg/generate_migration.go@@ -3108,6 +3108,38 @@ func generateCommentChangesFromSDL(buf *strings.Builder, diff *schema.MetadataDireturn nil}+ // Build a set of tables being dropped+ // When a table is dropped, its comments are automatically removed+ tablesBeingDropped := make(map[string]bool)+ for _, tableDiff := range diff.TableChanges {+ if tableDiff.Action == schema.MetadataDiffActionDrop {+ tableKey := tableDiff.SchemaName + "." + tableDiff.TableName+ tablesBeingDropped[tableKey] = true+ }+ }++ // Build a set of columns being dropped+ // When a column is dropped, its comment is automatically removed+ columnsBeingDropped := make(map[string]bool)+ for _, tableDiff := range diff.TableChanges {+ if tableDiff.Action == schema.MetadataDiffActionAlter {+ for _, colDiff := range tableDiff.ColumnChanges {+ if colDiff.Action == schema.MetadataDiffActionDrop {+ var columnName string+ if colDiff.OldColumn != nil {+ columnName = colDiff.OldColumn.Name+ } else if colDiff.OldASTNode != nil {+ columnName = extractColumnNameFromAST(colDiff.OldASTNode)+ }+ if columnName != "" {+ columnKey := tableDiff.SchemaName + "." + tableDiff.TableName + "." + columnName+ columnsBeingDropped[columnKey] = true+ }+ }+ }+ }+ }+for _, commentDiff := range diff.CommentChanges {// Extract the new comment text from the AST nodenewComment := extractCommentFromDiff(commentDiff)@@ -3117,9 +3149,19 @@ func generateCommentChangesFromSDL(buf *strings.Builder, diff *schema.MetadataDiwriteCommentOnSchema(buf, commentDiff.SchemaName, newComment)case schema.CommentObjectTypeTable:+ // Skip comment generation for tables being dropped+ tableKey := commentDiff.SchemaName + "." + commentDiff.ObjectName+ if tablesBeingDropped[tableKey] {+ continue+ }writeCommentOnTable(buf, commentDiff.SchemaName, commentDiff.ObjectName, newComment)case schema.CommentObjectTypeColumn:+ // Skip comment generation for columns being dropped+ columnKey := commentDiff.SchemaName + "." + commentDiff.ObjectName + "." + commentDiff.ColumnName+ if columnsBeingDropped[columnKey] {+ continue+ }writeCommentOnColumn(buf, commentDiff.SchemaName, commentDiff.ObjectName, commentDiff.ColumnName, newComment)case schema.CommentObjectTypeView:diff --git a/backend/plugin/schema/pg/procedure_sdl_diff_test.go b/backend/plugin/schema/pg/procedure_sdl_diff_test.goindex c9161ba479..065e9f7fb8 100644--- a/backend/plugin/schema/pg/procedure_sdl_diff_test.go+++ b/backend/plugin/schema/pg/procedure_sdl_diff_test.go@@ -366,3 +366,126 @@ func TestProcedureSDLDiff_CommentOnProcedure(t *testing.T) {})}}++func TestDropColumnWithComment_NoCommentGeneration(t *testing.T) {+ tests := []struct {+ name string+ previousSDL string+ currentSDL string+ shouldContain []string+ shouldNotContain []string+ }{+ {+ name: "Drop column with comment - should not generate COMMENT ON COLUMN",+ previousSDL: `+ CREATE TABLE "public"."test_table" (+ "id" serial,+ "name" character varying(100) NOT NULL,+ "test_column" integer,+ CONSTRAINT "test_table_pkey" PRIMARY KEY (id)+ );++ COMMENT ON TABLE "public"."test_table" IS 'A table for testing purposes';+ COMMENT ON COLUMN "public"."test_table"."test_column" IS 'A test column for various uses';+ `,+ currentSDL: `+ CREATE TABLE "public"."test_table" (+ "id" serial,+ "name" character varying(100) NOT NULL,+ CONSTRAINT "test_table_pkey" PRIMARY KEY (id)+ );++ COMMENT ON TABLE "public"."test_table" IS 'A table for testing purposes';+ `,+ shouldContain: []string{+ `ALTER TABLE "public"."test_table" DROP COLUMN IF EXISTS "test_column"`,+ },+ shouldNotContain: []string{+ `COMMENT ON COLUMN "public"."test_table"."test_column"`,+ },+ },+ {+ name: "Drop table with comment - should not generate COMMENT ON TABLE",+ previousSDL: `+ CREATE TABLE "public"."test_table" (+ "id" serial,+ "name" character varying(100) NOT NULL,+ CONSTRAINT "test_table_pkey" PRIMARY KEY (id)+ );++ COMMENT ON TABLE "public"."test_table" IS 'A table for testing purposes';+ `,+ currentSDL: ``,+ shouldContain: []string{+ `DROP TABLE IF EXISTS "public"."test_table"`,+ },+ shouldNotContain: []string{+ `COMMENT ON TABLE "public"."test_table"`,+ },+ },+ {+ name: "Drop multiple columns with comments",+ previousSDL: `+ CREATE TABLE "public"."test_table" (+ "id" serial,+ "name" character varying(100) NOT NULL,+ "created_at" timestamp(6) without time zone DEFAULT CURRENT_TIMESTAMP,+ "is_active" boolean DEFAULT true,+ "description" text,+ "test_column" integer,+ CONSTRAINT "test_table_pkey" PRIMARY KEY (id)+ );++ COMMENT ON TABLE "public"."test_table" IS 'A table for testing purposes';+ COMMENT ON COLUMN "public"."test_table"."test_column" IS 'A test column for various uses';+ COMMENT ON COLUMN "public"."test_table"."description" IS 'Description field';+ `,+ currentSDL: `+ CREATE TABLE "public"."test_table" (+ "id" serial,+ "name" character varying(100) NOT NULL,+ "is_active" boolean DEFAULT true,+ CONSTRAINT "test_table_pkey" PRIMARY KEY (id)+ );++ COMMENT ON TABLE "public"."test_table" IS 'A table for testing purposes';+ `,+ shouldContain: []string{+ `ALTER TABLE "public"."test_table" DROP COLUMN IF EXISTS "test_column"`,+ `ALTER TABLE "public"."test_table" DROP COLUMN IF EXISTS "created_at"`,+ `ALTER TABLE "public"."test_table" DROP COLUMN IF EXISTS "description"`,+ },+ shouldNotContain: []string{+ `COMMENT ON COLUMN "public"."test_table"."test_column"`,+ `COMMENT ON COLUMN "public"."test_table"."description"`,+ `COMMENT ON COLUMN "public"."test_table"."created_at"`,+ },+ },+ }++ 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)++ // Generate migration SQL+ migrationSQL, err := generateMigration(diff)+ require.NoError(t, err)++ t.Logf("Generated migration SQL:\n%s", migrationSQL)++ // Verify expected strings are present+ for _, expected := range tt.shouldContain {+ assert.Contains(t, migrationSQL, expected,+ "Migration SQL should contain %q", expected)+ }++ // Verify unwanted strings are not present+ for _, unwanted := range tt.shouldNotContain {+ assert.NotContains(t, migrationSQL, unwanted,+ "Migration SQL should NOT contain %q", unwanted)+ }+ })+ }+}