Skip to content

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

fix: fix some procedure bugs for SDL (#17833)

rebelice8951e0b69175
diff --git a/backend/plugin/schema/pg/function_migration_integration_test.go b/backend/plugin/schema/pg/function_migration_integration_test.go
index 82ba9ad151..a9760c46cb 100644
--- a/backend/plugin/schema/pg/function_migration_integration_test.go
+++ b/backend/plugin/schema/pg/function_migration_integration_test.go
@@ -384,3 +384,255 @@ func TestOverloadedFunctionSignatureHandling(t *testing.T) {
func containsString(s, substr string) bool {
return strings.Contains(s, substr)
}
+
+func TestProcedureDropMigration(t *testing.T) {
+ tests := []struct {
+ name string
+ previousSDL string
+ currentSDL string
+ expectedMigration string
+ }{
+ {
+ name: "Drop procedure should use DROP PROCEDURE",
+ previousSDL: `
+ CREATE PROCEDURE 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;
+ $$;
+ `,
+ currentSDL: ``,
+ expectedMigration: `DROP PROCEDURE IF EXISTS "public"."update_user_name"(user_id integer, new_name character varying);`,
+ },
+ {
+ name: "Drop function should use DROP FUNCTION",
+ previousSDL: `
+ CREATE FUNCTION calculate_tax(amount numeric) RETURNS numeric
+ LANGUAGE plpgsql
+ AS $$
+ BEGIN
+ RETURN amount * 0.1;
+ END;
+ $$;
+ `,
+ currentSDL: ``,
+ expectedMigration: `DROP FUNCTION IF EXISTS "public"."calculate_tax"(amount numeric);`,
+ },
+ }
+
+ 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)
+
+ migrationSQL, err := generateMigration(diff)
+ require.NoError(t, err)
+ assert.Contains(t, migrationSQL, tt.expectedMigration)
+ })
+ }
+}
+
+func TestDropProcedureShouldNotAffectFunctionComment(t *testing.T) {
+ // This test reproduces the issue shown in the screenshot:
+ // When dropping a procedure, the system should not generate
+ // COMMENT ON FUNCTION statements that remove comments from other functions
+ tests := []struct {
+ name string
+ previousSDL string
+ currentSDL string
+ }{
+ {
+ name: "Drop procedure without comment should not affect function comment",
+ previousSDL: `
+ CREATE OR REPLACE FUNCTION test_function()
+ RETURNS void
+ LANGUAGE plpgsql
+ AS $function$
+ BEGIN
+ -- Function logic goes here
+ RAISE NOTICE 'Test function executed';
+ END
+ $function$;
+
+ COMMENT ON FUNCTION "public"."test_function()" IS 'A test function that raises a notice';
+
+ CREATE PROCEDURE new_procedure()
+ LANGUAGE plpgsql
+ AS $$
+ BEGIN
+ -- Procedure logic
+ NULL;
+ END;
+ $$;
+ `,
+ currentSDL: `
+ CREATE OR REPLACE FUNCTION test_function()
+ RETURNS void
+ LANGUAGE plpgsql
+ AS $function$
+ BEGIN
+ -- Function logic goes here
+ RAISE NOTICE 'Test function executed';
+ END
+ $function$;
+
+ COMMENT ON FUNCTION "public"."test_function()" IS 'A test function that raises a notice';
+ `,
+ },
+ {
+ name: "Drop procedure with comment should not affect function comment",
+ previousSDL: `
+ CREATE OR REPLACE FUNCTION test_function()
+ RETURNS void
+ LANGUAGE plpgsql
+ AS $function$
+ BEGIN
+ RAISE NOTICE 'Test function executed';
+ END
+ $function$;
+
+ COMMENT ON FUNCTION "public"."test_function()" IS 'A test function that raises a notice';
+
+ CREATE PROCEDURE new_procedure()
+ LANGUAGE plpgsql
+ AS $$
+ BEGIN
+ NULL;
+ END;
+ $$;
+
+ COMMENT ON PROCEDURE "public"."new_procedure()" IS 'A test procedure';
+ `,
+ currentSDL: `
+ CREATE OR REPLACE FUNCTION test_function()
+ RETURNS void
+ LANGUAGE plpgsql
+ AS $function$
+ BEGIN
+ RAISE NOTICE 'Test function executed';
+ END
+ $function$;
+
+ COMMENT ON FUNCTION "public"."test_function()" IS 'A test function that raises a notice';
+ `,
+ },
+ {
+ name: "Drop procedure with quoted identifiers should not affect function comment",
+ // This tests the case where COMMENT statements use quoted identifiers (as generated by the database)
+ previousSDL: `
+ CREATE OR REPLACE FUNCTION test_function()
+ RETURNS void
+ LANGUAGE plpgsql
+ AS $function$
+ BEGIN
+ RAISE NOTICE 'Test function executed';
+ END
+ $function$;
+
+ COMMENT ON FUNCTION "public"."test_function()" IS 'A test function that raises a notice';
+
+ CREATE PROCEDURE new_procedure()
+ LANGUAGE plpgsql
+ AS $$
+ BEGIN
+ NULL;
+ END;
+ $$;
+
+ COMMENT ON PROCEDURE "public"."new_procedure()" IS 'A test procedure';
+ `,
+ currentSDL: `
+ CREATE OR REPLACE FUNCTION test_function()
+ RETURNS void
+ LANGUAGE plpgsql
+ AS $function$
+ BEGIN
+ RAISE NOTICE 'Test function executed';
+ END
+ $function$;
+
+ COMMENT ON FUNCTION "public"."test_function()" IS 'A test function that raises a notice';
+ `,
+ },
+ {
+ name: "Drop procedure with wrongly labeled FUNCTION comment",
+ // This simulates a bug where procedure comment was mistakenly written as COMMENT ON FUNCTION
+ previousSDL: `
+ CREATE OR REPLACE FUNCTION test_function()
+ RETURNS void
+ LANGUAGE plpgsql
+ AS $function$
+ BEGIN
+ RAISE NOTICE 'Test function executed';
+ END
+ $function$;
+
+ COMMENT ON FUNCTION "public"."test_function()" IS 'A test function that raises a notice';
+
+ CREATE PROCEDURE new_procedure()
+ LANGUAGE plpgsql
+ AS $$
+ BEGIN
+ NULL;
+ END;
+ $$;
+
+ COMMENT ON FUNCTION "public"."new_procedure()" IS 'Wrong: should be PROCEDURE not FUNCTION';
+ `,
+ currentSDL: `
+ CREATE OR REPLACE FUNCTION test_function()
+ RETURNS void
+ LANGUAGE plpgsql
+ AS $function$
+ BEGIN
+ RAISE NOTICE 'Test function executed';
+ END
+ $function$;
+
+ COMMENT ON FUNCTION "public"."test_function()" IS 'A test function that raises a notice';
+ `,
+ },
+ }
+
+ 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)
+
+ // Log all changes
+ t.Logf("Function changes: %d", len(diff.FunctionChanges))
+ for i, fc := range diff.FunctionChanges {
+ t.Logf(" [%d] Action=%v, Schema=%s, Function=%s", i, fc.Action, fc.SchemaName, fc.FunctionName)
+ }
+ t.Logf("Comment changes: %d", len(diff.CommentChanges))
+ for i, cc := range diff.CommentChanges {
+ t.Logf(" [%d] Action=%v, Type=%v, Schema=%s, Object=%s, OldComment=%q, NewComment=%q",
+ i, cc.Action, cc.ObjectType, cc.SchemaName, cc.ObjectName, cc.OldComment, cc.NewComment)
+ }
+
+ migrationSQL, err := generateMigration(diff)
+ require.NoError(t, err)
+
+ t.Logf("Generated migration SQL:\n%s", migrationSQL)
+
+ // Should drop the procedure
+ assert.Contains(t, migrationSQL, "DROP PROCEDURE IF EXISTS")
+ assert.Contains(t, migrationSQL, "new_procedure")
+
+ // Should NOT touch the function's comment
+ // The migration should NOT contain any COMMENT statement about test_function
+ if strings.Contains(migrationSQL, "test_function") && strings.Contains(migrationSQL, "COMMENT") {
+ // If there's a COMMENT statement about test_function, it should not be setting it to NULL
+ assert.NotContains(t, migrationSQL, "COMMENT ON FUNCTION \"public\".test_function() IS NULL",
+ "Migration should not remove test_function's comment")
+ assert.NotContains(t, migrationSQL, "COMMENT ON FUNCTION \"public\".\"test_function()\" IS NULL",
+ "Migration should not remove test_function's comment")
+ t.Errorf("Found unexpected COMMENT statement about test_function in migration: %s", migrationSQL)
+ }
+ })
+ }
+}
diff --git a/backend/plugin/schema/pg/generate_migration.go b/backend/plugin/schema/pg/generate_migration.go
index b766ab6200..804489b119 100644
--- a/backend/plugin/schema/pg/generate_migration.go
+++ b/backend/plugin/schema/pg/generate_migration.go
@@ -256,7 +256,8 @@ func dropObjectsInOrder(diff *schema.MetadataDiff, buf *strings.Builder) {
// Drop functions
for _, funcDiff := range functionMap {
- writeDropFunction(buf, funcDiff.SchemaName, funcDiff.FunctionName)
+ definition := getFunctionDefinitionForDrop(funcDiff)
+ writeDropFunction(buf, funcDiff.SchemaName, funcDiff.FunctionName, funcDiff.OldASTNode, definition)
}
// Drop tables
@@ -365,7 +366,8 @@ func dropObjectsInOrder(diff *schema.MetadataDiff, buf *strings.Builder) {
} else if mvDiff, ok := materializedViewMap[objID]; ok {
writeDropMaterializedView(buf, mvDiff.SchemaName, mvDiff.MaterializedViewName)
} else if funcDiff, ok := functionMap[objID]; ok {
- writeDropFunction(buf, funcDiff.SchemaName, funcDiff.FunctionName)
+ definition := getFunctionDefinitionForDrop(funcDiff)
+ writeDropFunction(buf, funcDiff.SchemaName, funcDiff.FunctionName, funcDiff.OldASTNode, definition)
} else if tableDiff, ok := tableMap[objID]; ok {
// Drop foreign keys before table
if tableDiff.OldTable != nil {
@@ -503,7 +505,8 @@ func dropObjectsInOrder(diff *schema.MetadataDiff, buf *strings.Builder) {
// Drop functions
for _, fn := range schemaDiff.OldSchema.Functions {
- writeDropFunction(buf, schemaDiff.SchemaName, fn.Name)
+ // In metadata mode, no AST node is available, so pass nil
+ writeDropFunction(buf, schemaDiff.SchemaName, fn.Name, nil, fn.Definition)
}
// Drop tables (this will handle foreign keys internally)
@@ -810,7 +813,7 @@ func createObjectsInOrder(diff *schema.MetadataDiff, buf *strings.Builder) error
}
// Add function comment for newly created functions
if funcDiff.Action == schema.MetadataDiffActionCreate && funcDiff.NewFunction != nil && funcDiff.NewFunction.Comment != "" {
- writeCommentOnFunction(buf, funcDiff.SchemaName, funcDiff.NewFunction.Signature, funcDiff.NewFunction.Comment, funcDiff.NewFunction.Definition)
+ writeCommentOnFunction(buf, funcDiff.SchemaName, funcDiff.NewFunction.Signature, funcDiff.NewFunction.Comment, funcDiff.NewASTNode, funcDiff.NewFunction.Definition)
}
}
@@ -930,7 +933,7 @@ func createObjectsInOrder(diff *schema.MetadataDiff, buf *strings.Builder) error
}
// Add function comment for newly created or altered functions
if (funcDiff.Action == schema.MetadataDiffActionCreate || funcDiff.Action == schema.MetadataDiffActionAlter) && funcDiff.NewFunction != nil && funcDiff.NewFunction.Comment != "" {
- writeCommentOnFunction(buf, funcDiff.SchemaName, funcDiff.NewFunction.Signature, funcDiff.NewFunction.Comment, funcDiff.NewFunction.Definition)
+ writeCommentOnFunction(buf, funcDiff.SchemaName, funcDiff.NewFunction.Signature, funcDiff.NewFunction.Comment, funcDiff.NewASTNode, funcDiff.NewFunction.Definition)
}
}
}
@@ -1378,8 +1381,81 @@ func writeDropForeignKey(out *strings.Builder, schema, table, constraint string)
_, _ = out.WriteString("\n")
}
-func writeDropFunction(out *strings.Builder, schema, function string) {
- _, _ = out.WriteString(`DROP FUNCTION IF EXISTS "`)
+// isFunctionProcedure checks if the AST node or definition represents a PROCEDURE (not a FUNCTION)
+// Returns true if it's a PROCEDURE, false if it's a FUNCTION or cannot be determined
+func isFunctionProcedure(astNode any, definition string) bool {
+ // First, try to determine from AST node (most reliable)
+ if astNode != nil {
+ // Check if it's a CreatefunctionstmtContext with PROCEDURE keyword
+ if ctx, ok := astNode.(*pgparser.CreatefunctionstmtContext); ok {
+ // If PROCEDURE() returns non-nil, it's a PROCEDURE
+ // If FUNCTION() returns non-nil, it's a FUNCTION
+ return ctx.PROCEDURE() != nil
+ }
+ }
+
+ // Fall back to checking definition string (for metadata mode or when AST is not available)
+ if definition != "" {
+ upperDef := strings.ToUpper(definition)
+ // Check for PROCEDURE keyword in CREATE statement
+ return strings.Contains(upperDef, " PROCEDURE ") ||
+ strings.HasPrefix(upperDef, "CREATE PROCEDURE") ||
+ strings.HasPrefix(upperDef, "CREATE OR REPLACE PROCEDURE")
+ }
+
+ return false
+}
+
+// getFunctionDefinitionForDrop extracts the function definition from either metadata or AST node
+func getFunctionDefinitionForDrop(funcDiff *schema.FunctionDiff) string {
+ // Try metadata first (if available)
+ if funcDiff.OldFunction != nil {
+ return funcDiff.OldFunction.Definition
+ }
+
+ // Fall back to extracting from AST node (AST-only mode)
+ if funcDiff.OldASTNode != nil {
+ // Use generic interface to extract text from any parser rule context
+ type parserContext interface {
+ GetParser() antlr.Parser
+ GetStart() antlr.Token
+ GetStop() antlr.Token
+ GetText() string
+ }
+
+ if ruleContext, ok := funcDiff.OldASTNode.(parserContext); ok {
+ // Try to use token stream first (preserves formatting)
+ if parser := ruleContext.GetParser(); parser != nil {
+ if tokenStream := parser.GetTokenStream(); tokenStream != nil {
+ start := ruleContext.GetStart()
+ stop := ruleContext.GetStop()
+ if start != nil && stop != nil {
+ text := tokenStream.GetTextFromTokens(start, stop)
+ if text != "" {
+ return text
+ }
+ }
+ }
+ }
+ // Fallback to GetText() method
+ return ruleContext.GetText()
+ }
+ }
+
+ // No definition available
+ return ""
+}
+
+func writeDropFunction(out *strings.Builder, schema, function string, astNode any, definition string) {
+ // Determine if this is a PROCEDURE or FUNCTION using AST-based detection
+ objectType := "FUNCTION"
+ if isFunctionProcedure(astNode, definition) {
+ objectType = "PROCEDURE"
+ }
+
+ _, _ = out.WriteString(`DROP `)
+ _, _ = out.WriteString(objectType)
+ _, _ = out.WriteString(` IF EXISTS "`)
_, _ = out.WriteString(schema)
_, _ = out.WriteString(`"."`)
@@ -2617,7 +2693,7 @@ func generateFunctionCommentChanges(buf *strings.Builder, diff *schema.MetadataD
// If comments are different, generate COMMENT ON FUNCTION statement
if oldComment != newComment {
- writeCommentOnFunction(buf, funcDiff.SchemaName, funcDiff.NewFunction.Signature, newComment, funcDiff.NewFunction.Definition)
+ writeCommentOnFunction(buf, funcDiff.SchemaName, funcDiff.NewFunction.Signature, newComment, funcDiff.NewASTNode, funcDiff.NewFunction.Definition)
}
}
}
@@ -2804,10 +2880,10 @@ func writeCommentOnMaterializedView(out *strings.Builder, schema, view, comment
}
// 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
+func writeCommentOnFunction(out *strings.Builder, schema, signature, comment string, astNode any, definition string) {
+ // Determine if this is a PROCEDURE or FUNCTION using AST-based detection
objectType := "FUNCTION"
- if strings.Contains(strings.ToUpper(definition), "CREATE PROCEDURE") {
+ if isFunctionProcedure(astNode, definition) {
objectType = "PROCEDURE"
}
@@ -2937,19 +3013,21 @@ func generateCommentChangesFromSDL(buf *strings.Builder, diff *schema.MetadataDi
case schema.CommentObjectTypeFunction:
// For functions, ObjectName contains the function signature
- // Try to find the function definition to determine if it's a FUNCTION or PROCEDURE
- functionDefinition := ""
+ // Try to find the function definition and AST node to determine if it's a FUNCTION or PROCEDURE
+ var functionDefinition string
+ var functionASTNode any
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
+ functionASTNode = funcDiff.NewASTNode
break
}
}
}
- writeCommentOnFunction(buf, commentDiff.SchemaName, commentDiff.ObjectName, newComment, functionDefinition)
+ writeCommentOnFunction(buf, commentDiff.SchemaName, commentDiff.ObjectName, newComment, functionASTNode, 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.go
index 279087c567..9cf5c63643 100644
--- a/backend/plugin/schema/pg/get_database_definition.go
+++ b/backend/plugin/schema/pg/get_database_definition.go
@@ -6,11 +6,15 @@ import (
"slices"
"strings"
+ "github.com/antlr4-go/antlr/v4"
"github.com/pkg/errors"
+ parser "github.com/bytebase/parser/postgresql"
+
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
parserbase "github.com/bytebase/bytebase/backend/plugin/parser/base"
+ pgparser "github.com/bytebase/bytebase/backend/plugin/parser/pg"
"github.com/bytebase/bytebase/backend/plugin/schema"
)
@@ -2084,10 +2088,48 @@ func writeFunction(out io.Writer, schema string, function *storepb.FunctionMetad
return nil
}
+// isDefinitionProcedure checks if the definition string represents a PROCEDURE (not a FUNCTION)
+// Returns true if it's a PROCEDURE, false if it's a FUNCTION
+// This function uses AST-based parsing for robust detection
+func isDefinitionProcedure(definition string) bool {
+ if definition == "" {
+ return false
+ }
+
+ // Parse the definition to get AST
+ parseResult, err := pgparser.ParsePostgreSQL(definition)
+ if err != nil {
+ // If parsing fails, fall back to string-based detection
+ // This should rarely happen for valid definitions
+ upperDef := strings.ToUpper(definition)
+ return strings.Contains(upperDef, " PROCEDURE ") ||
+ strings.HasPrefix(upperDef, "CREATE PROCEDURE") ||
+ strings.HasPrefix(upperDef, "CREATE OR REPLACE PROCEDURE")
+ }
+
+ tree := parseResult.Tree
+ if tree == nil {
+ return false
+ }
+
+ // Walk the AST to find CREATE FUNCTION/PROCEDURE statement
+ var result *parser.CreatefunctionstmtContext
+ extractor := &functionExtractor{result: &result}
+ antlr.NewParseTreeWalker().Walk(extractor, tree)
+
+ if result == nil {
+ return false
+ }
+
+ // Check if it's a PROCEDURE by examining the AST node
+ // CreatefunctionstmtContext has both FUNCTION() and PROCEDURE() methods
+ return result.PROCEDURE() != nil
+}
+
func writeFunctionComment(out io.Writer, schema string, function *storepb.FunctionMetadata) error {
// Determine if this is a PROCEDURE or FUNCTION by checking the definition
objectType := "FUNCTION"
- if strings.Contains(strings.ToUpper(function.Definition), "CREATE PROCEDURE") {
+ if isDefinitionProcedure(function.Definition) {
objectType = "PROCEDURE"
}
@@ -3458,7 +3500,7 @@ func writeViewCommentSDL(out io.Writer, schemaName string, view *storepb.ViewMet
func writeFunctionCommentSDL(out io.Writer, schemaName string, function *storepb.FunctionMetadata) error {
// Determine if this is a PROCEDURE or FUNCTION by checking the definition
objectType := "FUNCTION"
- if strings.Contains(strings.ToUpper(function.Definition), "CREATE PROCEDURE") {
+ if isDefinitionProcedure(function.Definition) {
objectType = "PROCEDURE"
}
@@ -3468,13 +3510,13 @@ func writeFunctionCommentSDL(out io.Writer, schemaName string, function *storepb
if _, err := io.WriteString(out, schemaName); err != nil {
return err
}
- if _, err := io.WriteString(out, `"."`); err != nil {
+ if _, err := io.WriteString(out, `".`); err != nil {
return err
}
if _, err := io.WriteString(out, function.Signature); err != nil {
return err
}
- if _, err := io.WriteString(out, `" IS '`); err != nil {
+ if _, err := io.WriteString(out, ` IS '`); err != nil {
return err
}
if _, err := io.WriteString(out, escapeSingleQuote(function.Comment)); 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.go
index 0310ccfa5a..6b432842a7 100644
--- a/backend/plugin/schema/pg/get_database_definition_sdl_test.go
+++ b/backend/plugin/schema/pg/get_database_definition_sdl_test.go
@@ -1470,7 +1470,7 @@ func TestGetDatabaseDefinitionSDLFormat_WithComments(t *testing.T) {
assert.Contains(t, result, `COMMENT ON VIEW "test_schema"."active_users" IS 'View of active users';`)
// Verify function comment
- assert.Contains(t, result, `COMMENT ON FUNCTION "test_schema"."get_user_count()" IS 'Function to get user count';`)
+ assert.Contains(t, result, `COMMENT ON FUNCTION "test_schema".get_user_count() IS 'Function to get user count';`)
// Verify sequence comment
assert.Contains(t, result, `COMMENT ON SEQUENCE "test_schema"."custom_seq" IS 'Custom sequence for testing';`)
@@ -1628,7 +1628,7 @@ func TestGetMultiFileDatabaseDefinition_WithComments(t *testing.T) {
functionFile, ok := fileMap["schemas/app_schema/functions/count_products.sql"]
require.True(t, ok, "function file should exist")
assert.Contains(t, functionFile, `CREATE FUNCTION app_schema.count_products()`)
- assert.Contains(t, functionFile, `COMMENT ON FUNCTION "app_schema"."count_products()" IS 'Returns product count';`)
+ assert.Contains(t, functionFile, `COMMENT ON FUNCTION "app_schema".count_products() IS 'Returns product count';`)
// Validate that each file's SQL can be parsed
for fileName, content := range fileMap {
@@ -1980,15 +1980,15 @@ $$`,
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';`,
+ 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';`,
+ 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`,
+ 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
diff --git a/backend/plugin/schema/pg/get_sdl_diff.go b/backend/plugin/schema/pg/get_sdl_diff.go
index 15baaff54f..c7526847bb 100644
--- a/backend/plugin/schema/pg/get_sdl_diff.go
+++ b/backend/plugin/schema/pg/get_sdl_diff.go
@@ -391,8 +391,10 @@ func (l *sdlChunkExtractor) EnterCommentstmt(ctx *parser.CommentstmtContext) {
return
}
- // Check for FUNCTION comment: COMMENT ON FUNCTION function_with_argtypes IS comment_text
- if ctx.FUNCTION() != nil {
+ // Check for FUNCTION/PROCEDURE comment
+ // Note: ctx.FUNCTION() is true for both COMMENT ON FUNCTION and COMMENT ON PROCEDURE
+ // We need to check ctx.PROCEDURE() to distinguish between them
+ if ctx.FUNCTION() != nil || ctx.PROCEDURE() != nil {
if ctx.Function_with_argtypes() != nil {
funcWithArgsCtx := ctx.Function_with_argtypes()
@@ -4082,7 +4084,7 @@ func createFunctionChunk(chunks *schema.SDLChunks, function *storepb.FunctionMet
if 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") {
+ if isDefinitionProcedure(function.Definition) {
objectType = "PROCEDURE"
}
schemaName, functionName := parseIdentifier(functionKey)
@@ -4136,7 +4138,7 @@ func updateFunctionChunkIfNeeded(chunks *schema.SDLChunks, currentFunction, prev
if 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") {
+ if isDefinitionProcedure(currentFunction.Definition) {
objectType = "PROCEDURE"
}
schemaName, functionName := parseIdentifier(functionKey)
diff --git a/backend/plugin/schema/pg/is_definition_procedure_test.go b/backend/plugin/schema/pg/is_definition_procedure_test.go
new file mode 100644
index 0000000000..21661aa3b5
--- /dev/null
+++ b/backend/plugin/schema/pg/is_definition_procedure_test.go
@@ -0,0 +1,205 @@
+package pg
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestIsDefinitionProcedure(t *testing.T) {
+ tests := []struct {
+ name string
+ definition string
+ isProcedure bool
+ }{
+ {
+ name: "Simple FUNCTION",
+ definition: `CREATE FUNCTION test_function()
+RETURNS void
+LANGUAGE plpgsql
+AS $$
+BEGIN
+ NULL;
+END;
+$$;`,
+ isProcedure: false,
+ },
+ {
+ name: "Simple PROCEDURE",
+ definition: `CREATE PROCEDURE test_procedure()
+LANGUAGE plpgsql
+AS $$
+BEGIN
+ NULL;
+END;
+$$;`,
+ isProcedure: true,
+ },
+ {
+ name: "CREATE OR REPLACE FUNCTION",
+ definition: `CREATE OR REPLACE FUNCTION test_function()
+RETURNS void
+LANGUAGE plpgsql
+AS $$
+BEGIN
+ NULL;
+END;
+$$;`,
+ isProcedure: false,
+ },
+ {
+ name: "CREATE OR REPLACE PROCEDURE",
+ definition: `CREATE OR REPLACE PROCEDURE test_procedure()
+LANGUAGE plpgsql
+AS $$
+BEGIN
+ NULL;
+END;
+$$;`,
+ isProcedure: true,
+ },
+ {
+ name: "FUNCTION with word 'procedure' in comment - should NOT be confused",
+ definition: `CREATE FUNCTION test_function()
+RETURNS void
+LANGUAGE plpgsql
+AS $$
+BEGIN
+ -- This is not a procedure, it's a function
+ -- The word PROCEDURE appears in this comment
+ RAISE NOTICE 'This function is not a procedure';
+END;
+$$;`,
+ isProcedure: false,
+ },
+ {
+ name: "FUNCTION with word 'procedure' in string literal - should NOT be confused",
+ definition: `CREATE FUNCTION test_function()
+RETURNS text
+LANGUAGE plpgsql
+AS $$
+BEGIN
+ RETURN 'This function is not a PROCEDURE';
+END;
+$$;`,
+ isProcedure: false,
+ },
+ {
+ name: "PROCEDURE with complex body",
+ definition: `CREATE OR REPLACE PROCEDURE update_user_data(
+ user_id INTEGER,
+ new_name VARCHAR
+)
+LANGUAGE plpgsql
+AS $$
+DECLARE
+ old_name VARCHAR;
+BEGIN
+ -- Get old name
+ SELECT name INTO old_name FROM users WHERE id = user_id;
+
+ -- Update the user name
+ UPDATE users SET name = new_name WHERE id = user_id;
+
+ -- Log the change
+ INSERT INTO audit_log (message) VALUES (
+ 'Changed user name from ' || old_name || ' to ' || new_name
+ );
+END;
+$$;`,
+ isProcedure: true,
+ },
+ {
+ name: "FUNCTION with RETURNS TABLE",
+ definition: `CREATE OR REPLACE FUNCTION get_users()
+RETURNS TABLE(id INTEGER, name VARCHAR)
+LANGUAGE plpgsql
+AS $$
+BEGIN
+ RETURN QUERY SELECT users.id, users.name FROM users;
+END;
+$$;`,
+ isProcedure: false,
+ },
+ {
+ name: "Empty definition",
+ definition: "",
+ isProcedure: false,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ result := isDefinitionProcedure(tt.definition)
+ assert.Equal(t, tt.isProcedure, result,
+ "isDefinitionProcedure returned %v, expected %v for:\n%s",
+ result, tt.isProcedure, tt.definition)
+ })
+ }
+}
+
+// TestIsDefinitionProcedureRobustness tests edge cases that would fail with string-based detection
+func TestIsDefinitionProcedureRobustness(t *testing.T) {
+ tests := []struct {
+ name string
+ definition string
+ isProcedure bool
+ reason string
+ }{
+ {
+ name: "FUNCTION with 'CREATE PROCEDURE' in string literal",
+ definition: `CREATE FUNCTION test_function()
+RETURNS text
+LANGUAGE sql
+AS $outer$
+ SELECT 'Example: CREATE PROCEDURE foo() LANGUAGE plpgsql AS $body$ BEGIN NULL; END; $body$;'
+$outer$;`,
+ isProcedure: false,
+ reason: "String-based detection might incorrectly identify this as a PROCEDURE due to 'CREATE PROCEDURE' in the string literal",
+ },
+ {
+ name: "FUNCTION with 'PROCEDURE' in multi-line comment",
+ definition: `CREATE FUNCTION complex_function()
+RETURNS void
+LANGUAGE plpgsql
+AS $$
+BEGIN
+ /*
+ * This function performs complex operations
+ * Note: This is NOT a PROCEDURE
+ * PROCEDURE keyword appears in this comment
+ * But it should still be detected as a FUNCTION
+ */
+ RAISE NOTICE 'Processing...';
+END;
+$$;`,
+ isProcedure: false,
+ reason: "Multi-line comments with PROCEDURE keyword should not confuse the detector",
+ },
+ {
+ name: "PROCEDURE with unusual formatting",
+ definition: `CREATE OR REPLACE PROCEDURE
+test_procedure
+(
+)
+LANGUAGE plpgsql
+AS
+$$
+BEGIN
+ NULL;
+END;
+$$;`,
+ isProcedure: true,
+ reason: "Should handle unusual whitespace and formatting",
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ result := isDefinitionProcedure(tt.definition)
+ assert.Equal(t, tt.isProcedure, result,
+ "Test failed: %s\nGot: %v, Expected: %v",
+ tt.reason, result, tt.isProcedure)
+ })
+ }
+}
diff --git a/backend/plugin/schema/pg/procedure_comment_roundtrip_test.go b/backend/plugin/schema/pg/procedure_comment_roundtrip_test.go
new file mode 100644
index 0000000000..217de91bb0
--- /dev/null
+++ b/backend/plugin/schema/pg/procedure_comment_roundtrip_test.go
@@ -0,0 +1,139 @@
+package pg
+
+import (
+ "strings"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/plugin/schema"
+)
+
+// TestProcedureCommentRoundtrip tests the full cycle:
+// 1. Database metadata with function + procedure (both with comments)
+// 2. Generate SDL from metadata (this is what "bytebase sdl dump" does)
+// 3. Use that SDL as "previous"
+// 4. Create "current" SDL with procedure removed
+// 5. Generate migration
+// 6. Verify function comment is NOT touched
+func TestProcedureCommentRoundtrip(t *testing.T) {
+ ctx := schema.GetDefinitionContext{
+ SDLFormat: true,
+ }
+
+ // Step 1: Create database metadata with both function and procedure (with comments)
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Name: "test_db",
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Functions: []*storepb.FunctionMetadata{
+ {
+ Name: "test_function",
+ Signature: "test_function()",
+ Definition: `CREATE OR REPLACE FUNCTION test_function()
+RETURNS void
+LANGUAGE plpgsql
+AS $function$
+BEGIN
+ RAISE NOTICE 'Test function executed';
+END
+$function$;`,
+ Comment: "A test function that raises a notice",
+ },
+ {
+ Name: "new_procedure",
+ Signature: "new_procedure()",
+ Definition: `CREATE PROCEDURE new_procedure()
+LANGUAGE plpgsql
+AS $$
+BEGIN
+ NULL;
+END;
+$$;`,
+ Comment: "A test procedure",
+ },
+ },
+ },
+ },
+ }
+
+ // Step 2: Generate SDL from metadata (simulating "bytebase sdl dump")
+ previousSDL, err := GetDatabaseDefinition(ctx, metadata)
+ require.NoError(t, err, "Failed to generate SDL from metadata")
+
+ t.Logf("Generated SDL from metadata:\n%s", previousSDL)
+
+ // Verify the generated SDL contains both objects with their comments
+ assert.Contains(t, previousSDL, "CREATE OR REPLACE FUNCTION", "Should contain function")
+ assert.Contains(t, previousSDL, "CREATE PROCEDURE", "Should contain procedure")
+ assert.Contains(t, previousSDL, "COMMENT ON FUNCTION", "Should contain function comment")
+ assert.Contains(t, previousSDL, "COMMENT ON PROCEDURE", "Should contain procedure comment")
+
+ // Step 3: Create current SDL with procedure removed (only function remains)
+ metadataCurrent := &storepb.DatabaseSchemaMetadata{
+ Name: "test_db",
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Functions: []*storepb.FunctionMetadata{
+ {
+ Name: "test_function",
+ Signature: "test_function()",
+ Definition: `CREATE OR REPLACE FUNCTION test_function()
+RETURNS void
+LANGUAGE plpgsql
+AS $function$
+BEGIN
+ RAISE NOTICE 'Test function executed';
+END
+$function$;`,
+ Comment: "A test function that raises a notice",
+ },
+ },
+ },
+ },
+ }
+
+ currentSDL, err := GetDatabaseDefinition(ctx, metadataCurrent)
+ require.NoError(t, err, "Failed to generate current SDL")
+
+ t.Logf("Current SDL (procedure removed):\n%s", currentSDL)
+
+ // Step 4: Generate diff and migration
+ diff, err := GetSDLDiff(currentSDL, previousSDL, nil, nil)
+ require.NoError(t, err, "Failed to generate SDL diff")
+
+ // Log the diff for debugging
+ t.Logf("Function changes: %d", len(diff.FunctionChanges))
+ for i, fc := range diff.FunctionChanges {
+ t.Logf(" [%d] Action=%v, Schema=%s, Function=%s", i, fc.Action, fc.SchemaName, fc.FunctionName)
+ }
+
+ t.Logf("Comment changes: %d", len(diff.CommentChanges))
+ for i, cc := range diff.CommentChanges {
+ t.Logf(" [%d] Action=%v, Type=%v, Schema=%s, Object=%s, OldComment=%q, NewComment=%q",
+ i, cc.Action, cc.ObjectType, cc.SchemaName, cc.ObjectName, cc.OldComment, cc.NewComment)
+ }
+
+ migrationSQL, err := generateMigration(diff)
+ require.NoError(t, err, "Failed to generate migration")
+
+ t.Logf("Generated migration SQL:\n%s", migrationSQL)
+
+ // Step 5: Verify the migration is correct
+ // Should drop the procedure
+ assert.Contains(t, migrationSQL, "DROP PROCEDURE IF EXISTS", "Should drop procedure")
+ assert.Contains(t, migrationSQL, "new_procedure", "Should mention new_procedure")
+
+ // Should NOT touch the function comment
+ // This is the key assertion - we should NOT see any COMMENT statement about test_function
+ if strings.Contains(migrationSQL, "test_function") && strings.Contains(migrationSQL, "COMMENT") {
+ t.Errorf("Migration incorrectly contains COMMENT statement about test_function:\n%s", migrationSQL)
+ }
+
+ // Should NOT contain "COMMENT ON FUNCTION ... IS NULL"
+ assert.NotContains(t, migrationSQL, "IS NULL", "Should not try to remove any comments")
+}