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 procedure comment (#17829)
rebelice5fdf88188a5c
diff --git a/backend/plugin/schema/pg/generate_migration.go b/backend/plugin/schema/pg/generate_migration.goindex c02e2f2aab..b766ab6200 100644--- a/backend/plugin/schema/pg/generate_migration.go+++ b/backend/plugin/schema/pg/generate_migration.go@@ -810,7 +810,7 @@ func createObjectsInOrder(diff *schema.MetadataDiff, buf *strings.Builder) error}// Add function comment for newly created functionsif funcDiff.Action == schema.MetadataDiffActionCreate && funcDiff.NewFunction != nil && funcDiff.NewFunction.Comment != "" {- writeCommentOnFunction(buf, funcDiff.SchemaName, funcDiff.NewFunction.Signature, funcDiff.NewFunction.Comment)+ writeCommentOnFunction(buf, funcDiff.SchemaName, funcDiff.NewFunction.Signature, funcDiff.NewFunction.Comment, funcDiff.NewFunction.Definition)}}@@ -930,7 +930,7 @@ func createObjectsInOrder(diff *schema.MetadataDiff, buf *strings.Builder) error}// Add function comment for newly created or altered functionsif (funcDiff.Action == schema.MetadataDiffActionCreate || funcDiff.Action == schema.MetadataDiffActionAlter) && funcDiff.NewFunction != nil && funcDiff.NewFunction.Comment != "" {- writeCommentOnFunction(buf, funcDiff.SchemaName, funcDiff.NewFunction.Signature, funcDiff.NewFunction.Comment)+ writeCommentOnFunction(buf, funcDiff.SchemaName, funcDiff.NewFunction.Signature, funcDiff.NewFunction.Comment, funcDiff.NewFunction.Definition)}}}@@ -2617,7 +2617,7 @@ func generateFunctionCommentChanges(buf *strings.Builder, diff *schema.MetadataD// If comments are different, generate COMMENT ON FUNCTION statementif oldComment != newComment {- writeCommentOnFunction(buf, funcDiff.SchemaName, funcDiff.NewFunction.Signature, newComment)+ writeCommentOnFunction(buf, funcDiff.SchemaName, funcDiff.NewFunction.Signature, newComment, funcDiff.NewFunction.Definition)}}}@@ -2803,9 +2803,17 @@ func writeCommentOnMaterializedView(out *strings.Builder, schema, view, comment_, _ = out.WriteString("\n")}-// writeCommentOnFunction writes a COMMENT ON FUNCTION statement-func writeCommentOnFunction(out *strings.Builder, schema, signature, comment string) {- _, _ = out.WriteString(`COMMENT ON FUNCTION "`)+// writeCommentOnFunction writes a COMMENT ON FUNCTION/PROCEDURE statement+func writeCommentOnFunction(out *strings.Builder, schema, signature, comment, definition string) {+ // Determine if this is a PROCEDURE or FUNCTION by checking the definition+ objectType := "FUNCTION"+ if strings.Contains(strings.ToUpper(definition), "CREATE PROCEDURE") {+ objectType = "PROCEDURE"+ }++ _, _ = out.WriteString(`COMMENT ON `)+ _, _ = out.WriteString(objectType)+ _, _ = out.WriteString(` "`)_, _ = out.WriteString(schema)_, _ = out.WriteString(`".`)_, _ = out.WriteString(signature)@@ -2929,7 +2937,19 @@ func generateCommentChangesFromSDL(buf *strings.Builder, diff *schema.MetadataDicase schema.CommentObjectTypeFunction:// For functions, ObjectName contains the function signature- writeCommentOnFunction(buf, commentDiff.SchemaName, commentDiff.ObjectName, newComment)+ // Try to find the function definition to determine if it's a FUNCTION or PROCEDURE+ functionDefinition := ""+ functionKey := commentDiff.SchemaName + "." + commentDiff.ObjectName+ for _, funcDiff := range diff.FunctionChanges {+ if funcDiff.NewFunction != nil {+ funcKey := funcDiff.SchemaName + "." + funcDiff.NewFunction.Signature+ if funcKey == functionKey {+ functionDefinition = funcDiff.NewFunction.Definition+ break+ }+ }+ }+ writeCommentOnFunction(buf, commentDiff.SchemaName, commentDiff.ObjectName, newComment, functionDefinition)case schema.CommentObjectTypeSequence:writeCommentOnSequence(buf, commentDiff.SchemaName, commentDiff.ObjectName, newComment)diff --git a/backend/plugin/schema/pg/get_database_definition.go b/backend/plugin/schema/pg/get_database_definition.goindex fed898ee46..279087c567 100644--- a/backend/plugin/schema/pg/get_database_definition.go+++ b/backend/plugin/schema/pg/get_database_definition.go@@ -2085,7 +2085,13 @@ func writeFunction(out io.Writer, schema string, function *storepb.FunctionMetad}func writeFunctionComment(out io.Writer, schema string, function *storepb.FunctionMetadata) error {- if _, err := io.WriteString(out, `COMMENT ON FUNCTION "`); err != nil {+ // Determine if this is a PROCEDURE or FUNCTION by checking the definition+ objectType := "FUNCTION"+ if strings.Contains(strings.ToUpper(function.Definition), "CREATE PROCEDURE") {+ objectType = "PROCEDURE"+ }++ if _, err := io.WriteString(out, "COMMENT ON "+objectType+" \""); err != nil {return err}@@ -3450,7 +3456,13 @@ func writeViewCommentSDL(out io.Writer, schemaName string, view *storepb.ViewMet}func writeFunctionCommentSDL(out io.Writer, schemaName string, function *storepb.FunctionMetadata) error {- if _, err := io.WriteString(out, `COMMENT ON FUNCTION "`); err != nil {+ // Determine if this is a PROCEDURE or FUNCTION by checking the definition+ objectType := "FUNCTION"+ if strings.Contains(strings.ToUpper(function.Definition), "CREATE PROCEDURE") {+ objectType = "PROCEDURE"+ }++ if _, err := io.WriteString(out, "COMMENT ON "+objectType+" \""); err != nil {return err}if _, err := io.WriteString(out, schemaName); err != nil {diff --git a/backend/plugin/schema/pg/get_database_definition_sdl_test.go b/backend/plugin/schema/pg/get_database_definition_sdl_test.goindex c4454cd058..0310ccfa5a 100644--- a/backend/plugin/schema/pg/get_database_definition_sdl_test.go+++ b/backend/plugin/schema/pg/get_database_definition_sdl_test.go@@ -1934,3 +1934,64 @@ ALTER SEQUENCE "public"."test_sequence2" OWNED BY "public"."test_table"."id";_, err = pgparser.ParsePostgreSQL(result)require.NoError(t, err, "Generated SQL should be parseable by PostgreSQL parser")}++func TestGetDatabaseDefinitionSDLFormat_ProcedureWithComment(t *testing.T) {+ // This test verifies that PROCEDURE comments are correctly generated with+ // COMMENT ON PROCEDURE (not COMMENT ON FUNCTION)+ metadata := &storepb.DatabaseSchemaMetadata{+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Functions: []*storepb.FunctionMetadata{+ {+ Name: "get_user_count",+ Signature: "get_user_count()",+ Definition: `CREATE FUNCTION "public"."get_user_count"() RETURNS integer+ LANGUAGE sql+ AS $$+ SELECT COUNT(*)::integer FROM users;+$$`,+ Comment: "Function to count users",+ },+ {+ Name: "update_user_name",+ Signature: "update_user_name(user_id integer, new_name character varying)",+ Definition: `CREATE PROCEDURE "public"."update_user_name"(IN user_id integer, IN new_name character varying)+ LANGUAGE plpgsql+ AS $$+BEGIN+ UPDATE users+ SET name = new_name+ WHERE id = user_id;+END;+$$`,+ Comment: "Procedure to update user name",+ },+ },+ },+ },+ }++ ctx := schema.GetDefinitionContext{+ SDLFormat: true,+ }++ result, err := GetDatabaseDefinition(ctx, metadata)+ require.NoError(t, err)++ // Verify that FUNCTION has COMMENT ON FUNCTION+ assert.Contains(t, result, `COMMENT ON FUNCTION "public"."get_user_count()" IS 'Function to count users';`,+ "Function comment should use COMMENT ON FUNCTION")++ // Verify that PROCEDURE has COMMENT ON PROCEDURE (not COMMENT ON FUNCTION)+ assert.Contains(t, result, `COMMENT ON PROCEDURE "public"."update_user_name(user_id integer, new_name character varying)" IS 'Procedure to update user name';`,+ "Procedure comment should use COMMENT ON PROCEDURE")++ // Verify that we don't incorrectly use COMMENT ON FUNCTION for the procedure+ assert.NotContains(t, result, `COMMENT ON FUNCTION "public"."update_user_name`,+ "Procedure comment should NOT use COMMENT ON FUNCTION")++ // Validate that the generated SQL can be parsed+ _, err = pgparser.ParsePostgreSQL(result)+ require.NoError(t, err, "Generated SQL should be parseable by PostgreSQL parser")+}diff --git a/backend/plugin/schema/pg/get_sdl_diff.go b/backend/plugin/schema/pg/get_sdl_diff.goindex 038386b70f..15baaff54f 100644--- a/backend/plugin/schema/pg/get_sdl_diff.go+++ b/backend/plugin/schema/pg/get_sdl_diff.go@@ -4078,10 +4078,15 @@ func createFunctionChunk(chunks *schema.SDLChunks, function *storepb.FunctionMetASTNode: astNode,}- // Add COMMENT ON FUNCTION if the function has a comment+ // Add COMMENT ON FUNCTION/PROCEDURE if the function has a commentif function.Comment != "" {+ // Determine if this is a PROCEDURE or FUNCTION by checking the definition+ objectType := "FUNCTION"+ if strings.Contains(strings.ToUpper(function.Definition), "CREATE PROCEDURE") {+ objectType = "PROCEDURE"+ }schemaName, functionName := parseIdentifier(functionKey)- if err := syncObjectCommentStatements(chunk, function.Comment, "FUNCTION", schemaName, functionName); err != nil {+ if err := syncObjectCommentStatements(chunk, function.Comment, objectType, schemaName, functionName); err != nil {return errors.Wrapf(err, "failed to add COMMENT statements for function %s", functionKey)}}@@ -4127,10 +4132,15 @@ func updateFunctionChunkIfNeeded(chunks *schema.SDLChunks, currentFunction, prevchunk.ASTNode = astNode}- // Synchronize COMMENT ON FUNCTION statements only if comment has changed+ // Synchronize COMMENT ON FUNCTION/PROCEDURE statements only if comment has changedif currentFunction.Comment != previousFunction.Comment {+ // Determine if this is a PROCEDURE or FUNCTION by checking the definition+ objectType := "FUNCTION"+ if strings.Contains(strings.ToUpper(currentFunction.Definition), "CREATE PROCEDURE") {+ objectType = "PROCEDURE"+ }schemaName, functionName := parseIdentifier(functionKey)- if err := syncObjectCommentStatements(chunk, currentFunction.Comment, "FUNCTION", schemaName, functionName); err != nil {+ if err := syncObjectCommentStatements(chunk, currentFunction.Comment, objectType, schemaName, functionName); err != nil {return errors.Wrapf(err, "failed to sync COMMENT statements for function %s", functionKey)}}