Skip to content

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

chore: handle procedure comment (#17837)

rebelice73d60f16b887
diff --git a/backend/plugin/schema/pg/generate_migration.go b/backend/plugin/schema/pg/generate_migration.go
index 804489b119..dd396a4e3a 100644
--- a/backend/plugin/schema/pg/generate_migration.go
+++ b/backend/plugin/schema/pg/generate_migration.go
@@ -1392,6 +1392,12 @@ func isFunctionProcedure(astNode any, definition string) bool {
// If FUNCTION() returns non-nil, it's a FUNCTION
return ctx.PROCEDURE() != nil
}
+ // Check if it's a CommentstmtContext (COMMENT ON FUNCTION/PROCEDURE)
+ if ctx, ok := astNode.(*pgparser.CommentstmtContext); ok {
+ // If PROCEDURE() returns non-nil, it's a COMMENT ON PROCEDURE
+ // If FUNCTION() returns non-nil (and PROCEDURE is nil), it's a COMMENT ON FUNCTION
+ return ctx.PROCEDURE() != nil
+ }
}
// Fall back to checking definition string (for metadata mode or when AST is not available)
@@ -3027,6 +3033,11 @@ func generateCommentChangesFromSDL(buf *strings.Builder, diff *schema.MetadataDi
}
}
}
+ // 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
+ }
writeCommentOnFunction(buf, commentDiff.SchemaName, commentDiff.ObjectName, newComment, functionASTNode, functionDefinition)
case schema.CommentObjectTypeSequence:
diff --git a/backend/plugin/schema/pg/procedure_sdl_diff_test.go b/backend/plugin/schema/pg/procedure_sdl_diff_test.go
index 81452c27fe..c9161ba479 100644
--- a/backend/plugin/schema/pg/procedure_sdl_diff_test.go
+++ b/backend/plugin/schema/pg/procedure_sdl_diff_test.go
@@ -231,3 +231,138 @@ func TestProcedureSDLDiff(t *testing.T) {
})
}
}
+
+func TestProcedureSDLDiff_CommentOnProcedure(t *testing.T) {
+ tests := []struct {
+ name string
+ previousSDL string
+ currentSDL string
+ expectedSQL string
+ shouldContain []string
+ shouldNotContain []string
+ expectedCommentChange int
+ }{
+ {
+ name: "Create procedure with COMMENT ON PROCEDURE",
+ previousSDL: ``,
+ currentSDL: `
+ CREATE PROCEDURE 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';
+ `,
+ shouldContain: []string{
+ "CREATE PROCEDURE",
+ "COMMENT ON PROCEDURE",
+ `"public".new_procedure()`,
+ },
+ shouldNotContain: []string{
+ "COMMENT ON FUNCTION",
+ },
+ expectedCommentChange: 1,
+ },
+ {
+ name: "Create procedure with COMMENT ON PROCEDURE - ensure FUNCTION is not used",
+ previousSDL: `
+ CREATE TABLE logs (
+ id SERIAL PRIMARY KEY,
+ message TEXT
+ );
+ `,
+ currentSDL: `
+ CREATE TABLE logs (
+ id SERIAL PRIMARY KEY,
+ message TEXT
+ );
+
+ CREATE PROCEDURE log_message(msg TEXT)
+ LANGUAGE plpgsql
+ AS $$
+ BEGIN
+ INSERT INTO logs (message) VALUES (msg);
+ END;
+ $$;
+
+ COMMENT ON PROCEDURE "public"."log_message"(msg text) IS 'Logs a message to the logs table';
+ `,
+ shouldContain: []string{
+ "CREATE PROCEDURE",
+ "COMMENT ON PROCEDURE",
+ `log_message`,
+ },
+ shouldNotContain: []string{
+ "COMMENT ON FUNCTION",
+ },
+ expectedCommentChange: 1,
+ },
+ {
+ name: "Mixed - procedure with comment and function with comment",
+ previousSDL: ``,
+ currentSDL: `
+ CREATE FUNCTION test_function()
+ RETURNS void
+ LANGUAGE plpgsql
+ AS $$
+ BEGIN
+ RAISE NOTICE 'Test function executed';
+ END;
+ $$;
+
+ COMMENT ON FUNCTION "public".test_function() IS 'A test function that raises a notice';
+
+ CREATE PROCEDURE test_procedure()
+ LANGUAGE plpgsql
+ AS $$
+ BEGIN
+ RAISE NOTICE 'Test procedure executed';
+ END;
+ $$;
+
+ COMMENT ON PROCEDURE "public"."test_procedure"() IS 'A test procedure that raises a notice';
+ `,
+ shouldContain: []string{
+ "COMMENT ON FUNCTION",
+ "test_function",
+ "COMMENT ON PROCEDURE",
+ "test_procedure",
+ },
+ shouldNotContain: []string{},
+ expectedCommentChange: 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)
+
+ // Verify comment changes
+ assert.Equal(t, tt.expectedCommentChange, len(diff.CommentChanges),
+ "Expected %d comment changes, got %d", tt.expectedCommentChange, len(diff.CommentChanges))
+
+ // 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)
+ }
+ })
+ }
+}