Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: remove procedure comment correctly (#17884)

rebelicedfd37df82664
diff --git a/backend/plugin/schema/pg/generate_migration.go b/backend/plugin/schema/pg/generate_migration.go
index 992c3c86ff..f6a03c0113 100644
--- a/backend/plugin/schema/pg/generate_migration.go
+++ b/backend/plugin/schema/pg/generate_migration.go
@@ -3174,6 +3174,7 @@ func generateCommentChangesFromSDL(buf *strings.Builder, diff *schema.MetadataDi
var functionASTNode any
functionKey := commentDiff.SchemaName + "." + commentDiff.ObjectName
for _, funcDiff := range diff.FunctionChanges {
+ // Check both NewFunction and OldFunction to handle comment removal cases
if funcDiff.NewFunction != nil {
funcKey := funcDiff.SchemaName + "." + funcDiff.NewFunction.Signature
if funcKey == functionKey {
@@ -3182,11 +3183,27 @@ func generateCommentChangesFromSDL(buf *strings.Builder, diff *schema.MetadataDi
break
}
}
+ if funcDiff.OldFunction != nil {
+ funcKey := funcDiff.SchemaName + "." + funcDiff.OldFunction.Signature
+ if funcKey == functionKey {
+ // Only use OldFunction if we haven't found NewFunction
+ if functionDefinition == "" {
+ functionDefinition = funcDiff.OldFunction.Definition
+ functionASTNode = funcDiff.OldASTNode
+ }
+ break
+ }
+ }
}
// If we didn't find the function AST node from FunctionChanges, use the comment AST node
// to determine if it's a FUNCTION or PROCEDURE
- if functionASTNode == nil && commentDiff.NewASTNode != nil {
- functionASTNode = commentDiff.NewASTNode
+ // Check both NewASTNode (for adding comments) and OldASTNode (for removing comments)
+ if functionASTNode == nil {
+ if commentDiff.NewASTNode != nil {
+ functionASTNode = commentDiff.NewASTNode
+ } else if commentDiff.OldASTNode != nil {
+ functionASTNode = commentDiff.OldASTNode
+ }
}
writeCommentOnFunction(buf, commentDiff.SchemaName, commentDiff.ObjectName, newComment, functionASTNode, functionDefinition)
diff --git a/backend/plugin/schema/pg/procedure_sdl_diff_test.go b/backend/plugin/schema/pg/procedure_sdl_diff_test.go
index 065e9f7fb8..b7e4da130a 100644
--- a/backend/plugin/schema/pg/procedure_sdl_diff_test.go
+++ b/backend/plugin/schema/pg/procedure_sdl_diff_test.go
@@ -489,3 +489,101 @@ func TestDropColumnWithComment_NoCommentGeneration(t *testing.T) {
})
}
}
+func TestRemoveProcedureComment_ShouldUsePROCEDURE(t *testing.T) {
+ tests := []struct {
+ name string
+ previousSDL string
+ currentSDL string
+ shouldContain []string
+ shouldNotContain []string
+ }{
+ {
+ name: "Remove comment from procedure - should use COMMENT ON PROCEDURE, not FUNCTION",
+ previousSDL: `
+CREATE PROCEDURE "public"."new_procedure"()
+LANGUAGE plpgsql
+AS $$
+BEGIN
+ RAISE NOTICE 'New procedure executed';
+END;
+$$;
+
+COMMENT ON PROCEDURE "public"."new_procedure"() IS 'A new procedure that raises a notice';
+`,
+ currentSDL: `
+CREATE PROCEDURE "public"."new_procedure"()
+LANGUAGE plpgsql
+AS $$
+BEGIN
+ RAISE NOTICE 'New procedure executed';
+END;
+$$;
+`,
+ shouldContain: []string{
+ "COMMENT ON PROCEDURE",
+ `"public".new_procedure() IS NULL`,
+ },
+ shouldNotContain: []string{
+ "COMMENT ON FUNCTION",
+ },
+ },
+ {
+ name: "Update procedure comment - should use COMMENT ON PROCEDURE",
+ previousSDL: `
+CREATE PROCEDURE "public"."log_message"(msg text)
+LANGUAGE plpgsql
+AS $$
+BEGIN
+ INSERT INTO logs (message) VALUES (msg);
+END;
+$$;
+
+COMMENT ON PROCEDURE "public"."log_message"(msg text) IS 'Old comment';
+`,
+ currentSDL: `
+CREATE PROCEDURE "public"."log_message"(msg text)
+LANGUAGE plpgsql
+AS $$
+BEGIN
+ INSERT INTO logs (message) VALUES (msg);
+END;
+$$;
+
+COMMENT ON PROCEDURE "public"."log_message"(msg text) IS 'New comment';
+`,
+ shouldContain: []string{
+ "COMMENT ON PROCEDURE",
+ `"public".log_message(msg text) IS 'New comment'`,
+ },
+ shouldNotContain: []string{
+ "COMMENT ON FUNCTION",
+ },
+ },
+ }
+
+ 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)
+ }
+ })
+ }
+}