Skip to content

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

fix: fix bugs for get database metadata (#17984)

rebelice58edf9432db5
diff --git a/backend/plugin/schema/pg/get_database_metadata.go b/backend/plugin/schema/pg/get_database_metadata.go
index 6ff10a2508..db5b13f690 100644
--- a/backend/plugin/schema/pg/get_database_metadata.go
+++ b/backend/plugin/schema/pg/get_database_metadata.go
@@ -20,6 +20,7 @@ import (
func init() {
schema.RegisterGetDatabaseMetadata(storepb.Engine_POSTGRES, GetDatabaseMetadata)
+ schema.RegisterGetDatabaseMetadata(storepb.Engine_COCKROACHDB, GetDatabaseMetadata)
}
// GetDatabaseMetadata parses the SQL schema text and returns the database metadata.
@@ -325,22 +326,8 @@ func (e *metadataExtractor) extractSchemaAndTableName(ctx parser.IQualified_name
return e.currentSchema, ""
}
- // Try direct text extraction for reliable parsing
- rawText := ctx.GetText()
- if rawText != "" {
- // Handle schema.table format
- if strings.Contains(rawText, ".") {
- parts := strings.Split(rawText, ".")
- if len(parts) == 2 {
- return parts[0], parts[1]
- }
- } else {
- // Just a table name, use current schema
- return e.currentSchema, rawText
- }
- }
-
- // Fallback to normalize function (though it might be problematic)
+ // Use NormalizePostgreSQLQualifiedName which properly handles quoted identifiers
+ // This function removes quotes and handles case sensitivity correctly
parts := pgparser.NormalizePostgreSQLQualifiedName(ctx)
if len(parts) == 1 {
return e.currentSchema, parts[0]
@@ -445,11 +432,25 @@ func (e *metadataExtractor) createSerialSequenceAndDefault(column *storepb.Colum
// Generate sequence name following PostgreSQL convention: tablename_columnname_seq
sequenceName := fmt.Sprintf("%s_%s_seq", table.Name, column.Name)
- // Create the sequence metadata
+ // Determine sequence data type based on column type
+ // SERIAL (integer) → bigint sequence (PostgreSQL standard)
+ // BIGSERIAL (bigint) → bigint sequence
+ // SMALLSERIAL (smallint) → bigint sequence
+ // All sequences in PostgreSQL are bigint by default
+ dataType := "bigint"
+
+ // Create the sequence metadata with proper defaults matching PostgreSQL behavior
sequence := &storepb.SequenceMetadata{
- Name: sequenceName,
- Start: "1",
- Increment: "1",
+ Name: sequenceName,
+ DataType: dataType,
+ Start: "1",
+ Increment: "1",
+ MinValue: "1",
+ MaxValue: "9223372036854775807", // bigint max value
+ Cycle: false,
+ CacheSize: "1",
+ OwnerTable: table.Name,
+ OwnerColumn: column.Name,
}
// Add sequence to the global sequences map using fully qualified name
@@ -1360,7 +1361,7 @@ func (e *metadataExtractor) EnterAltertablestmt(ctx *parser.AltertablestmtContex
return
}
- // Check if this is an ADD CONSTRAINT command
+ // Check if this is an ALTER TABLE command with alter_table_cmds
if alterTableCmdList := ctx.Alter_table_cmds(); alterTableCmdList != nil {
e.handleAlterTableCommands(ctx, alterTableCmdList)
}
@@ -1382,15 +1383,83 @@ func (e *metadataExtractor) handleAlterTableCommands(ctx *parser.AltertablestmtC
continue
}
+ // Debug: check what type of command this is
+ hasAlter := alterTableCmd.ALTER() != nil
+ hasAdd := alterTableCmd.ADD_P() != nil
+ hasGenerated := alterTableCmd.GENERATED() != nil
+ hasIdentity := alterTableCmd.IDENTITY_P() != nil
+
// Check if this is an ADD CONSTRAINT command
if alterTableCmd.ADD_P() != nil && alterTableCmd.Tableconstraint() != nil {
e.extractTableConstraint(alterTableCmd.Tableconstraint(), table, schemaName)
+ continue
+ }
+
+ // Check if this is an ALTER COLUMN ADD GENERATED AS IDENTITY command
+ // According to g4: ALTER opt_column? colid ADD_P GENERATED generated_when AS IDENTITY_P
+ // The COLUMN keyword is optional (opt_column?)
+ if hasAlter && hasAdd && hasGenerated && hasIdentity {
+ e.handleAlterColumn(alterTableCmd, table, schemaName)
}
}
}
}
}
+// handleAlterColumn processes ALTER TABLE ALTER COLUMN commands
+func (e *metadataExtractor) handleAlterColumn(alterTableCmd parser.IAlter_table_cmdContext, table *storepb.TableMetadata, schemaName string) {
+ // Get the column name from colid (based on g4: ALTER opt_column? colid ADD_P GENERATED...)
+ columnName := ""
+ allColids := alterTableCmd.AllColid()
+ if len(allColids) > 0 {
+ // The column name is the first colid
+ columnName = pgparser.NormalizePostgreSQLColid(allColids[0])
+ }
+
+ // Debug log
+ _ = columnName // TODO: remove after debug
+
+ if columnName == "" {
+ return
+ }
+
+ // Find the column in the table
+ var targetColumn *storepb.ColumnMetadata
+ for _, col := range table.Columns {
+ if col.Name == columnName {
+ targetColumn = col
+ break
+ }
+ }
+
+ // If column doesn't exist, we might need to create it
+ // But for now, we'll just skip if the column doesn't exist
+ if targetColumn == nil {
+ return
+ }
+
+ // Check if this is ADD GENERATED AS IDENTITY (from g4: ALTER opt_column? colid ADD_P GENERATED generated_when AS IDENTITY_P)
+ if alterTableCmd.ADD_P() != nil && alterTableCmd.GENERATED() != nil && alterTableCmd.IDENTITY_P() != nil {
+ // Set identity generation mode
+ if generatedWhen := alterTableCmd.Generated_when(); generatedWhen != nil {
+ if generatedWhen.ALWAYS() != nil {
+ targetColumn.IdentityGeneration = storepb.ColumnMetadata_ALWAYS
+ } else if generatedWhen.BY() != nil && generatedWhen.DEFAULT() != nil {
+ targetColumn.IdentityGeneration = storepb.ColumnMetadata_BY_DEFAULT
+ }
+ }
+
+ // Get sequence options from AST
+ var seqOptList parser.ISeqoptlistContext
+ if optParenSeqList := alterTableCmd.Optparenthesizedseqoptlist(); optParenSeqList != nil {
+ seqOptList = optParenSeqList.Seqoptlist()
+ }
+
+ // Create the identity sequence with options from AST
+ e.createIdentitySequenceFromAST(table, targetColumn, schemaName, seqOptList)
+ }
+}
+
// handleAttachPartition processes ALTER TABLE ATTACH PARTITION statements
func (e *metadataExtractor) handleAttachPartition(ctx *parser.AltertablestmtContext, partitionCmd parser.IPartition_cmdContext) {
// Get the main table name
@@ -1527,6 +1596,264 @@ func (e *metadataExtractor) EnterCreateseqstmt(ctx *parser.CreateseqstmtContext)
schemaMetadata.Sequences = append(schemaMetadata.Sequences, sequence)
}
+// EnterAlterseqstmt is called when entering an ALTER SEQUENCE statement
+func (e *metadataExtractor) EnterAlterseqstmt(ctx *parser.AlterseqstmtContext) {
+ if e.err != nil {
+ return
+ }
+
+ if ctx.Qualified_name() == nil {
+ return
+ }
+
+ schemaName, sequenceName := e.extractSchemaAndTableName(ctx.Qualified_name())
+ sequenceKey := fmt.Sprintf("%s.%s", schemaName, sequenceName)
+
+ // Find the existing sequence or create one if it doesn't exist
+ sequence, exists := e.sequences[sequenceKey]
+ if !exists {
+ // If sequence doesn't exist yet, create it with defaults
+ schemaMetadata := e.getOrCreateSchema(schemaName)
+ sequence = &storepb.SequenceMetadata{
+ Name: sequenceName,
+ DataType: "bigint",
+ Start: "1",
+ Increment: "1",
+ MinValue: "1",
+ MaxValue: "9223372036854775807",
+ Cycle: false,
+ CacheSize: "1",
+ }
+ e.sequences[sequenceKey] = sequence
+ if schemaMetadata.Sequences == nil {
+ schemaMetadata.Sequences = []*storepb.SequenceMetadata{}
+ }
+ schemaMetadata.Sequences = append(schemaMetadata.Sequences, sequence)
+ }
+
+ // Process sequence options from ALTER SEQUENCE
+ if seqOptList := ctx.Seqoptlist(); seqOptList != nil {
+ for _, seqOptElem := range seqOptList.AllSeqoptelem() {
+ if seqOptElem == nil {
+ continue
+ }
+ e.extractSequenceOption(seqOptElem, sequence)
+ }
+ }
+}
+
+// EnterCommentstmt is called when entering a COMMENT statement
+func (e *metadataExtractor) EnterCommentstmt(ctx *parser.CommentstmtContext) {
+ if e.err != nil {
+ return
+ }
+
+ // Extract comment text (common for all comment types)
+ var comment string
+ if commentText := ctx.Comment_text(); commentText != nil {
+ if commentText.NULL_P() != nil {
+ // COMMENT ... IS NULL means remove comment
+ comment = ""
+ } else if commentText.Sconst() != nil {
+ // Extract the string constant, removing quotes
+ commentStr := commentText.GetText()
+ if len(commentStr) >= 2 && commentStr[0] == '\'' && commentStr[len(commentStr)-1] == '\'' {
+ // Remove quotes and unescape
+ comment = commentStr[1 : len(commentStr)-1]
+ // Unescape single quotes ('' -> ')
+ comment = strings.ReplaceAll(comment, "''", "'")
+ }
+ }
+ }
+
+ // Handle COMMENT ON COLUMN
+ if ctx.COLUMN() != nil && ctx.Any_name() != nil {
+ parts := pgparser.NormalizePostgreSQLAnyName(ctx.Any_name())
+ if len(parts) < 2 {
+ return
+ }
+
+ var schemaName, tableName, columnName string
+ if len(parts) == 2 {
+ schemaName = e.currentSchema
+ tableName = parts[0]
+ columnName = parts[1]
+ } else if len(parts) == 3 {
+ schemaName = parts[0]
+ tableName = parts[1]
+ columnName = parts[2]
+ } else {
+ return
+ }
+
+ schema := e.getOrCreateSchema(schemaName)
+ for _, table := range schema.Tables {
+ if table.Name == tableName {
+ for i, col := range table.Columns {
+ if col.Name == columnName {
+ table.Columns[i].Comment = comment
+ return
+ }
+ }
+ }
+ }
+ return
+ }
+
+ // Handle COMMENT ON TABLE/SEQUENCE/VIEW etc (object_type_any_name)
+ if ctx.Object_type_any_name() != nil && ctx.Any_name() != nil {
+ parts := pgparser.NormalizePostgreSQLAnyName(ctx.Any_name())
+ if len(parts) == 0 {
+ return
+ }
+
+ var schemaName, objectName string
+ if len(parts) == 1 {
+ schemaName = e.currentSchema
+ objectName = parts[0]
+ } else if len(parts) >= 2 {
+ schemaName = parts[0]
+ objectName = parts[1]
+ }
+
+ schema := e.getOrCreateSchema(schemaName)
+ objectType := ctx.Object_type_any_name()
+
+ // COMMENT ON TABLE
+ if objectType.TABLE() != nil {
+ for _, table := range schema.Tables {
+ if table.Name == objectName {
+ table.Comment = comment
+ return
+ }
+ }
+ }
+
+ // COMMENT ON SEQUENCE
+ if objectType.SEQUENCE() != nil {
+ for _, seq := range schema.Sequences {
+ if seq.Name == objectName {
+ seq.Comment = comment
+ return
+ }
+ }
+ }
+
+ // COMMENT ON VIEW
+ if objectType.VIEW() != nil {
+ for _, view := range schema.Views {
+ if view.Name == objectName {
+ view.Comment = comment
+ return
+ }
+ }
+ }
+
+ // COMMENT ON MATERIALIZED VIEW
+ if objectType.MATERIALIZED() != nil && objectType.VIEW() != nil {
+ for _, view := range schema.MaterializedViews {
+ if view.Name == objectName {
+ view.Comment = comment
+ return
+ }
+ }
+ }
+
+ // COMMENT ON INDEX
+ if objectType.INDEX() != nil {
+ for _, table := range schema.Tables {
+ for i, index := range table.Indexes {
+ if index.Name == objectName {
+ table.Indexes[i].Comment = comment
+ return
+ }
+ }
+ }
+ }
+
+ return
+ }
+
+ // Handle COMMENT ON SCHEMA
+ if ctx.Object_type_name() != nil && ctx.Name() != nil {
+ // COMMENT ON SCHEMA name IS comment_text
+ schemaName := pgparser.NormalizePostgreSQLName(ctx.Name())
+ if schemaName != "" {
+ schema := e.getOrCreateSchema(schemaName)
+ schema.Comment = comment
+ }
+ return
+ }
+
+ // Handle COMMENT ON FUNCTION/PROCEDURE
+ if (ctx.FUNCTION() != nil || ctx.PROCEDURE() != nil) && ctx.Function_with_argtypes() != nil {
+ funcWithArgs := ctx.Function_with_argtypes()
+ if funcWithArgs.Func_name() == nil {
+ return
+ }
+
+ // Extract function name
+ parts := pgparser.NormalizePostgreSQLFuncName(funcWithArgs.Func_name())
+ if len(parts) == 0 {
+ return
+ }
+
+ var schemaName, funcName string
+ if len(parts) == 1 {
+ schemaName = e.currentSchema
+ funcName = parts[0]
+ } else if len(parts) >= 2 {
+ schemaName = parts[0]
+ funcName = parts[1]
+ }
+
+ schema := e.getOrCreateSchema(schemaName)
+ for i, function := range schema.Functions {
+ if function.Name == funcName {
+ schema.Functions[i].Comment = comment
+ return
+ }
+ }
+ return
+ }
+
+ // Handle COMMENT ON TYPE (including ENUM types)
+ if ctx.TYPE_P() != nil && ctx.AllTypename() != nil && len(ctx.AllTypename()) > 0 {
+ // Extract type name from typename
+ typename := ctx.AllTypename()[0]
+ var schemaName, typeName string
+
+ // Try to get qualified name from typename
+ if typename.Qualified_name() != nil {
+ schemaName, typeName = e.extractSchemaAndTableName(typename.Qualified_name())
+ } else if typename.Simpletypename() != nil {
+ // Simple type name - get the text representation
+ typeText := typename.GetParser().GetTokenStream().GetTextFromRuleContext(typename)
+ // typeText might be "myschema.status" or just "status"
+ // We need to split it if it contains a dot
+ parts := strings.Split(typeText, ".")
+ if len(parts) == 2 {
+ schemaName = parts[0]
+ typeName = parts[1]
+ } else {
+ typeName = typeText
+ schemaName = e.currentSchema
+ }
+ }
+
+ if typeName != "" {
+ schema := e.getOrCreateSchema(schemaName)
+ for i, enumType := range schema.EnumTypes {
+ if enumType.Name == typeName {
+ schema.EnumTypes[i].Comment = comment
+ return
+ }
+ }
+ }
+ return
+ }
+}
+
// EnterViewstmt is called when entering a create view statement
func (e *metadataExtractor) EnterViewstmt(ctx *parser.ViewstmtContext) {
if e.err != nil {
@@ -1568,18 +1895,19 @@ func (e *metadataExtractor) EnterCreatefunctionstmt(ctx *parser.Createfunctionst
return
}
- // Extract function name directly from the text
- // The normalization function seems to be failing, so we'll extract directly
- funcName := funcNameCtx.GetText()
- schemaName := e.currentSchema
+ // Use NormalizePostgreSQLFuncName which properly handles quoted identifiers
+ parts := pgparser.NormalizePostgreSQLFuncName(funcNameCtx)
+ if len(parts) == 0 {
+ return
+ }
- // Handle schema.function syntax
- if strings.Contains(funcName, ".") {
- parts := strings.Split(funcName, ".")
- if len(parts) == 2 {
- schemaName = parts[0]
- funcName = parts[1]
- }
+ var schemaName, funcName string
+ if len(parts) == 1 {
+ schemaName = e.currentSchema
+ funcName = parts[0]
+ } else if len(parts) >= 2 {
+ schemaName = parts[0]
+ funcName = parts[1]
}
if funcName == "" {
@@ -1655,36 +1983,36 @@ func (e *metadataExtractor) EnterCreatetrigstmt(ctx *parser.CreatetrigstmtContex
return
}
- // Extract trigger name
+ // Extract trigger name using normalization to properly handle quoted identifiers
triggerNameCtx := ctx.Name()
if triggerNameCtx == nil {
return
}
- triggerName := triggerNameCtx.GetText()
+ triggerName := pgparser.NormalizePostgreSQLName(triggerNameCtx)
if triggerName == "" {
return
}
- // Extract table name that the trigger is on
+ // Extract table name that the trigger is on using normalization to properly handle quoted identifiers
qualifiedNameCtx := ctx.Qualified_name()
if qualifiedNameCtx == nil {
return
}
- tableName := qualifiedNameCtx.GetText()
- if tableName == "" {
- return
+ // Use NormalizePostgreSQLQualifiedName to handle quotes correctly
+ parts := pgparser.NormalizePostgreSQLQualifiedName(qualifiedNameCtx)
+ var schemaName, tableName string
+ if len(parts) == 1 {
+ schemaName = e.currentSchema
+ tableName = parts[0]
+ } else if len(parts) == 2 {
+ schemaName = parts[0]
+ tableName = parts[1]
}
- // Parse schema.table if needed
- schemaName := e.currentSchema
- if strings.Contains(tableName, ".") {
- parts := strings.Split(tableName, ".")
- if len(parts) == 2 {
- schemaName = parts[0]
- tableName = parts[1]
- }
+ if tableName == "" {
+ return
}
// Build trigger metadata
@@ -2295,6 +2623,18 @@ func (*metadataExtractor) extractSequenceOption(ctx parser.ISeqoptelemContext, s
case ctx.NO() != nil && ctx.CYCLE() != nil:
// NO CYCLE
sequence.Cycle = false
+ case ctx.OWNED() != nil && ctx.BY() != nil && ctx.Any_name() != nil:
+ // OWNED BY table.column
+ parts := pgparser.NormalizePostgreSQLAnyName(ctx.Any_name())
+ if len(parts) == 2 {
+ // Format: table.column
+ sequence.OwnerTable = parts[0]
+ sequence.OwnerColumn = parts[1]
+ } else if len(parts) == 3 {
+ // Format: schema.table.column (we only care about table.column)
+ sequence.OwnerTable = parts[1]
+ sequence.OwnerColumn = parts[2]
+ }
default:
// Other sequence options
}
@@ -2355,6 +2695,81 @@ func (e *metadataExtractor) createIdentitySequence(table *storepb.TableMetadata,
e.sequences[sequenceKey] = sequence
}
+// createIdentitySequenceFromAST creates an identity sequence from ALTER COLUMN ADD IDENTITY using AST
+func (e *metadataExtractor) createIdentitySequenceFromAST(table *storepb.TableMetadata, column *storepb.ColumnMetadata, schemaName string, seqOptList parser.ISeqoptlistContext) {
+ // Default sequence name
+ sequenceName := fmt.Sprintf("%s_%s_seq", table.Name, column.Name)
+
+ // Determine sequence data type and limits based on column type
+ // For identity columns, use positive ranges starting from 1
+ var dataType, minValue, maxValue string
+ switch strings.ToUpper(column.Type) {
+ case "SMALLINT", "INT2":
+ dataType = "smallint"
+ minValue = "1"
+ maxValue = "32767"
+ case "INTEGER", "INT", "INT4":
+ dataType = "integer"
+ minValue = "1"
+ maxValue = "2147483647"
+ case "BIGINT", "INT8":
+ dataType = "bigint"
+ minValue = "1"
+ maxValue = "9223372036854775807"
+ default:
+ // Default to bigint for unknown types
+ dataType = "bigint"
+ minValue = "1"
+ maxValue = "9223372036854775807"
+ }
+
+ // Create the sequence metadata with defaults
+ sequence := &storepb.SequenceMetadata{
+ Name: sequenceName,
+ DataType: dataType,
+ Start: "1",
+ Increment: "1",
+ MinValue: minValue,
+ MaxValue: maxValue,
+ Cycle: false,
+ CacheSize: "1",
+ OwnerTable: table.Name,
+ OwnerColumn: column.Name,
+ }
+
+ // Override with specified options from ALTER COLUMN statement (from AST)
+ if seqOptList != nil {
+ for _, seqElem := range seqOptList.AllSeqoptelem() {
+ if seqElem == nil {
+ continue
+ }
+ // Check for SEQUENCE NAME option to override default name
+ if seqElem.SEQUENCE() != nil && seqElem.NAME_P() != nil {
+ if anyName := seqElem.Any_name(); anyName != nil {
+ _, seqName := e.extractSchemaAndEnumName(anyName)
+ if seqName != "" {
+ sequenceName = seqName
+ sequence.Name = sequenceName
+ }
+ }
+ }
+ // Extract other sequence options
+ e.extractSequenceOption(seqElem, sequence)
+ }
+ }
+
+ // Add the sequence to the schema
+ schema := e.getOrCreateSchema(schemaName)
+ if schema.Sequences == nil {
+ schema.Sequences = []*storepb.SequenceMetadata{}
+ }
+ schema.Sequences = append(schema.Sequences, sequence)
+
+ // Store in the sequences map for reference
+ sequenceKey := fmt.Sprintf("%s.%s", schemaName, sequenceName)
+ e.sequences[sequenceKey] = sequence
+}
+
// extractSchemaAndEnumName extracts schema and enum name from Any_name context
func (e *metadataExtractor) extractSchemaAndEnumName(ctx parser.IAny_nameContext) (string, string) {
if ctx == nil {
diff --git a/backend/plugin/schema/pg/pg_alter_parsing_test.go b/backend/plugin/schema/pg/pg_alter_parsing_test.go
new file mode 100644
index 0000000000..5954cd9043
--- /dev/null
+++ b/backend/plugin/schema/pg/pg_alter_parsing_test.go
@@ -0,0 +1,153 @@
+package pg
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/require"
+
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ pgparser "github.com/bytebase/bytebase/backend/plugin/parser/pg"
+ "github.com/bytebase/bytebase/backend/plugin/schema"
+ "github.com/bytebase/bytebase/backend/store/model"
+)
+
+// TestAlterColumnAddIdentityParsing tests that ALTER COLUMN ADD IDENTITY is correctly parsed
+func TestAlterColumnAddIdentityParsing(t *testing.T) {
+ ddl := `ALTER TABLE orders ALTER COLUMN id ADD GENERATED BY DEFAULT AS IDENTITY;`
+
+ parseResult, err := pgparser.ParsePostgreSQL(ddl)
+ require.NoError(t, err)
+ require.NotNil(t, parseResult.Tree, "Parse tree should exist")
+}
+
+// TestAlterColumnAddIdentityMetadata tests ALTER COLUMN ADD IDENTITY creates sequences
+func TestAlterColumnAddIdentityMetadata(t *testing.T) {
+ ddl := `
+ CREATE TABLE products (
+ id integer NOT NULL
+ );
+
+ ALTER TABLE products ALTER COLUMN id ADD GENERATED BY DEFAULT AS IDENTITY (
+ SEQUENCE NAME products_id_seq
+ START WITH 1
+ INCREMENT BY 1
+ );
+ `
+
+ metadata, err := GetDatabaseMetadata(ddl)
+ require.NoError(t, err)
+ require.NotNil(t, metadata)
+
+ require.NotEmpty(t, metadata.Schemas, "Should have schemas")
+ schema := metadata.Schemas[0]
+
+ require.Len(t, schema.Tables, 1, "Should have one table")
+ table := schema.Tables[0]
+ require.Equal(t, "products", table.Name)
+
+ require.Len(t, table.Columns, 1, "Should have one column")
+ column := table.Columns[0]
+ require.Equal(t, "id", column.Name)
+ require.Equal(t, storepb.ColumnMetadata_BY_DEFAULT, column.IdentityGeneration)
+
+ require.Len(t, schema.Sequences, 1, "Should have one sequence")
+ seq := schema.Sequences[0]
+ require.Equal(t, "products_id_seq", seq.Name)
+ require.Equal(t, "products", seq.OwnerTable)
+ require.Equal(t, "id", seq.OwnerColumn)
+}
+
+// TestAlterColumnIdentityEquivalence tests that two equivalent schemas don't generate diffs
+func TestAlterColumnIdentityEquivalence(t *testing.T) {
+ // User pasted schema (ALTER COLUMN ADD IDENTITY format)
+ userSchema := `
+ CREATE TABLE "public"."orders" (
+ "id" integer NOT NULL,
+ "amount" numeric
+ );
+
+ ALTER TABLE "public"."orders" ALTER COLUMN "id" ADD GENERATED BY DEFAULT AS IDENTITY (
+ SEQUENCE NAME "public"."orders_id_seq"
+ START WITH 1
+ INCREMENT BY 1
+ MINVALUE 1
+ MAXVALUE 2147483647
+ CACHE 1
+ );
+ `
+
+ // Database current state (inline IDENTITY format)
+ dbSchema := `
+ CREATE TABLE orders (
+ id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ amount NUMERIC
+ );
+ `
+
+ userMetadata, err := GetDatabaseMetadata(userSchema)
+ require.NoError(t, err)
+ require.NotNil(t, userMetadata)
+
+ dbMetadata, err := GetDatabaseMetadata(dbSchema)
+ require.NoError(t, err)
+ require.NotNil(t, dbMetadata)
+
+ // Simulate database sync adding LastValue
+ for _, seq := range dbMetadata.Schemas[0].Sequences {
+ seq.LastValue = "1"
+ }
+
+ // Compare schemas
+ userDBSchema := model.NewDatabaseSchema(userMetadata, nil, nil, storepb.Engine_POSTGRES, false)
+ dbDBSchema := model.NewDatabaseSchema(dbMetadata, nil, nil, storepb.Engine_POSTGRES, false)
+
+ diff, err := schema.GetDatabaseSchemaDiff(storepb.Engine_POSTGRES, dbDBSchema, userDBSchema)
+ require.NoError(t, err)
+
+ // Key assertion: no sequence changes should be detected
+ require.Empty(t, diff.SequenceChanges, "Should not detect sequence changes for equivalent schemas")
+}
+
+// TestAlterSequenceOwnedBy tests ALTER SEQUENCE OWNED BY parsing
+func TestAlterSequenceOwnedBy(t *testing.T) {
+ tests := []struct {
+ name string
+ schema string
+ expectOwnerTable string
+ expectOwnerColumn string
+ }{
+ {
+ name: "ALTER SEQUENCE with OWNED BY",
+ schema: `
+ CREATE SEQUENCE user_seq;
+ CREATE TABLE users (id INT);
+ ALTER SEQUENCE user_seq OWNED BY users.id;
+ `,
+ expectOwnerTable: "users",
+ expectOwnerColumn: "id",
+ },
+ {
+ name: "CREATE SEQUENCE without OWNED BY",
+ schema: `
+ CREATE SEQUENCE user_seq;
+ `,
+ expectOwnerTable: "",
+ expectOwnerColumn: "",
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ metadata, err := GetDatabaseMetadata(tt.schema)
+ require.NoError(t, err)
+ require.NotNil(t, metadata)
+
+ require.NotEmpty(t, metadata.Schemas)
+ require.NotEmpty(t, metadata.Schemas[0].Sequences)
+
+ seq := metadata.Schemas[0].Sequences[0]
+ require.Equal(t, tt.expectOwnerTable, seq.OwnerTable, "OwnerTable mismatch")
+ require.Equal(t, tt.expectOwnerColumn, seq.OwnerColumn, "OwnerColumn mismatch")
+ })
+ }
+}
diff --git a/backend/plugin/schema/pg/pg_comment_parsing_test.go b/backend/plugin/schema/pg/pg_comment_parsing_test.go
new file mode 100644
index 0000000000..65e2a54375
--- /dev/null
+++ b/backend/plugin/schema/pg/pg_comment_parsing_test.go
@@ -0,0 +1,301 @@
+package pg
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/require"
+
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/plugin/schema"
+ "github.com/bytebase/bytebase/backend/store/model"
+)
+
+// TestCommentOnTable tests COMMENT ON TABLE parsing
+func TestCommentOnTable(t *testing.T) {
+ // User pasted schema
+ userSchema := `
+ CREATE TABLE "public"."invoices" (
+ "id" integer NOT NULL,
+ "total" numeric
+ );
+
+ COMMENT ON TABLE "public"."invoices" IS 'Invoice records';
+ `
+
+ // Database current state
+ dbSchema := `
+ CREATE TABLE invoices (
+ id INT NOT NULL,
+ total NUMERIC
+ );
+
+ COMMENT ON TABLE invoices IS 'Invoice records';
+ `
+
+ userMetadata, err := GetDatabaseMetadata(userSchema)
+ require.NoError(t, err)
+ require.NotNil(t, userMetadata)
+
+ dbMetadata, err := GetDatabaseMetadata(dbSchema)
+ require.NoError(t, err)
+ require.NotNil(t, dbMetadata)
+
+ // Verify table comments are parsed
+ require.NotEmpty(t, userMetadata.Schemas)
+ require.NotEmpty(t, userMetadata.Schemas[0].Tables)
+ require.Equal(t, "Invoice records", userMetadata.Schemas[0].Tables[0].Comment)
+
+ require.NotEmpty(t, dbMetadata.Schemas)
+ require.NotEmpty(t, dbMetadata.Schemas[0].Tables)
+ require.Equal(t, "Invoice records", dbMetadata.Schemas[0].Tables[0].Comment)
+
+ // Compare schemas
+ userDBSchema := model.NewDatabaseSchema(userMetadata, nil, nil, storepb.Engine_POSTGRES, false)
+ dbDBSchema := model.NewDatabaseSchema(dbMetadata, nil, nil, storepb.Engine_POSTGRES, false)
+
+ diff, err := schema.GetDatabaseSchemaDiff(storepb.Engine_POSTGRES, dbDBSchema, userDBSchema)
+ require.NoError(t, err)
+
+ migration, err := generateMigration(diff)
+ require.NoError(t, err)
+
+ // Should not generate COMMENT statements when comments are identical
+ require.NotContains(t, migration, "COMMENT ON TABLE", "Should not generate COMMENT statements for unchanged comments")
+}
+
+// TestCommentOnColumn tests COMMENT ON COLUMN parsing including Chinese characters
+func TestCommentOnColumn(t *testing.T) {
+ // User pasted schema with column comments
+ userSchema := `
+ CREATE TABLE "public"."orders" (
+ "id" integer NOT NULL,
+ "old_plan_id" text,
+ "new_plan_id" text
+ );
+
+ ALTER TABLE "public"."orders" ALTER COLUMN "id" ADD GENERATED BY DEFAULT AS IDENTITY (
+ SEQUENCE NAME "public"."orders_id_seq"
+ START WITH 1
+ INCREMENT BY 1
+ );
+
+ COMMENT ON COLUMN "public"."orders"."old_plan_id" IS 'Previous plan identifier';
+ COMMENT ON COLUMN "public"."orders"."new_plan_id" IS 'Current plan identifier';
+ `
+
+ // Database current state with same comments
+ dbSchema := `
+ CREATE TABLE orders (
+ id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ old_plan_id TEXT,
+ new_plan_id TEXT
+ );
+
+ COMMENT ON COLUMN orders.old_plan_id IS 'Previous plan identifier';
+ COMMENT ON COLUMN orders.new_plan_id IS 'Current plan identifier';
+ `
+
+ userMetadata, err := GetDatabaseMetadata(userSchema)
+ require.NoError(t, err)
+
+ dbMetadata, err := GetDatabaseMetadata(dbSchema)
+ require.NoError(t, err)
+
+ // Verify column comments are parsed
+ userTable := userMetadata.Schemas[0].Tables[0]
+ require.Len(t, userTable.Columns, 3)
+ require.Equal(t, "", userTable.Columns[0].Comment)
+ require.Equal(t, "Previous plan identifier", userTable.Columns[1].Comment)
+ require.Equal(t, "Current plan identifier", userTable.Columns[2].Comment)
+
+ dbTable := dbMetadata.Schemas[0].Tables[0]
+ require.Len(t, dbTable.Columns, 3)
+ require.Equal(t, "", dbTable.Columns[0].Comment)
+ require.Equal(t, "Previous plan identifier", dbTable.Columns[1].Comment)
+ require.Equal(t, "Current plan identifier", dbTable.Columns[2].Comment)
+
+ // Compare schemas
+ userDBSchema := model.NewDatabaseSchema(userMetadata, nil, nil, storepb.Engine_POSTGRES, false)
+ dbDBSchema := model.NewDatabaseSchema(dbMetadata, nil, nil, storepb.Engine_POSTGRES, false)
+
+ diff, err := schema.GetDatabaseSchemaDiff(storepb.Engine_POSTGRES, dbDBSchema, userDBSchema)
+ require.NoError(t, err)
+
+ migration, err := generateMigration(diff)
+ require.NoError(t, err)
+
+ // Should not generate COMMENT statements when comments are identical
+ require.NotContains(t, migration, "COMMENT ON COLUMN", "Should not generate COMMENT statements for unchanged comments")
+}
+
+// TestMultilineComment tests multiline comment parsing
+func TestMultilineComment(t *testing.T) {
+ userSchema := `
+ CREATE TABLE "public"."transactions" (
+ "source_type" text
+ );
+
+ COMMENT ON COLUMN "public"."transactions"."source_type" IS 'Transaction source types:
+0: Mobile app
+1: Web portal
+2: API integration
+3: Partner system
+4: Admin panel';
+ `
+
+ dbSchema := `
+ CREATE TABLE transactions (
+ source_type TEXT
+ );
+
+ COMMENT ON COLUMN transactions.source_type IS 'Transaction source types:
+0: Mobile app
+1: Web portal
+2: API integration
+3: Partner system
+4: Admin panel';
+ `
+
+ userMetadata, err := GetDatabaseMetadata(userSchema)
+ require.NoError(t, err)
+
+ dbMetadata, err := GetDatabaseMetadata(dbSchema)
+ require.NoError(t, err)
+
+ // Verify multiline comments are preserved
+ userCol := userMetadata.Schemas[0].Tables[0].Columns[0]
+ dbCol := dbMetadata.Schemas[0].Tables[0].Columns[0]
+
+ require.Contains(t, userCol.Comment, "\n", "Should preserve newlines")
+ require.Equal(t, userCol.Comment, dbCol.Comment, "Comments should be identical")
+
+ // Compare schemas
+ userDBSchema := model.NewDatabaseSchema(userMetadata, nil, nil, storepb.Engine_POSTGRES, false)
+ dbDBSchema := model.NewDatabaseSchema(dbMetadata, nil, nil, storepb.Engine_POSTGRES, false)
+
+ diff, err := schema.GetDatabaseSchemaDiff(storepb.Engine_POSTGRES, dbDBSchema, userDBSchema)
+ require.NoError(t, err)
+
+ migration, err := generateMigration(diff)
+ require.NoError(t, err)
+
+ // Should not generate COMMENT statements
+ require.Empty(t, migration, "Should not generate migration for identical comments")
+}
+
+// TestAllCommentTypes tests all supported comment types
+func TestAllCommentTypes(t *testing.T) {
+ schema := `
+ CREATE SCHEMA app;
+ COMMENT ON SCHEMA app IS 'Application schema';
+
+ CREATE TABLE app.users (
+ id INT,
+ username TEXT
+ );
+ COMMENT ON TABLE app.users IS 'User accounts';
+ COMMENT ON COLUMN app.users.id IS 'User identifier';
+ COMMENT ON COLUMN app.users.username IS 'Login username';
+
+ CREATE INDEX idx_users_username ON app.users(username);
+ COMMENT ON INDEX app.idx_users_username IS 'Index for username lookups';
+
+ CREATE VIEW app.active_users AS SELECT * FROM app.users;
+ COMMENT ON VIEW app.active_users IS 'Currently active users';
+
+ CREATE SEQUENCE app.user_id_seq;
+ COMMENT ON SEQUENCE app.user_id_seq IS 'User ID sequence generator';
+
+ CREATE TYPE app.user_status AS ENUM ('active', 'inactive', 'suspended');
+ COMMENT ON TYPE app.user_status IS 'User account status';
+
+ CREATE FUNCTION app.get_user(user_id INT) RETURNS TEXT AS $$
+ BEGIN
+ RETURN 'user';
+ END;
+ $$ LANGUAGE plpgsql;
+ COMMENT ON FUNCTION app.get_user(INT) IS 'Retrieve user information';
+ `
+
+ metadata, err := GetDatabaseMetadata(schema)
+ require.NoError(t, err)
+ require.NotNil(t, metadata)
+
+ // Find the app schema
+ var appSchema *storepb.SchemaMetadata
+ for _, s := range metadata.Schemas {
+ if s.Name == "app" {
+ appSchema = s
+ break
+ }
+ }
+ require.NotNil(t, appSchema, "Should find app schema")
+
+ // Verify all comment types
+ require.Equal(t, "Application schema", appSchema.Comment)
+ require.NotEmpty(t, appSchema.Tables)
+ require.Equal(t, "User accounts", appSchema.Tables[0].Comment)
+ require.NotEmpty(t, appSchema.Tables[0].Columns)
+ require.Equal(t, "User identifier", appSchema.Tables[0].Columns[0].Comment)
+ require.NotEmpty(t, appSchema.Tables[0].Indexes)
+ require.Equal(t, "Index for username lookups", appSchema.Tables[0].Indexes[0].Comment)
+ require.NotEmpty(t, appSchema.Views)
+ require.Equal(t, "Currently active users", appSchema.Views[0].Comment)
+ require.NotEmpty(t, appSchema.Sequences)
+ require.Equal(t, "User ID sequence generator", appSchema.Sequences[0].Comment)
+ require.NotEmpty(t, appSchema.EnumTypes)
+ require.Equal(t, "User account status", appSchema.EnumTypes[0].Comment)
+ require.NotEmpty(t, appSchema.Functions)
+ require.Equal(t, "Retrieve user information", appSchema.Functions[0].Comment)
+}
+
+// TestCommentEquivalence tests that identical comments don't generate migrations
+func TestCommentEquivalence(t *testing.T) {
+ sourceSchema := `
+ CREATE TABLE products (
+ id INT,
+ name TEXT
+ );
+ COMMENT ON TABLE products IS 'Product catalog';
+ COMMENT ON COLUMN products.id IS 'Product ID';
+
+ CREATE INDEX idx_name ON products(name);
+ COMMENT ON INDEX idx_name IS 'Product name index';
+
+ CREATE SEQUENCE product_seq;
+ COMMENT ON SEQUENCE product_seq IS 'Product sequence';
+ `
+
+ targetSchema := `
+ CREATE TABLE products (
+ id INT,
+ name TEXT
+ );
+ COMMENT ON TABLE products IS 'Product catalog';
+ COMMENT ON COLUMN products.id IS 'Product ID';
+
+ CREATE INDEX idx_name ON products(name);
+ COMMENT ON INDEX idx_name IS 'Product name index';
+
+ CREATE SEQUENCE product_seq;
+ COMMENT ON SEQUENCE product_seq IS 'Product sequence';
+ `
+
+ sourceMetadata, err := GetDatabaseMetadata(sourceSchema)
+ require.NoError(t, err)
+
+ targetMetadata, err := GetDatabaseMetadata(targetSchema)
+ require.NoError(t, err)
+
+ sourceDBSchema := model.NewDatabaseSchema(sourceMetadata, nil, nil, storepb.Engine_POSTGRES, false)
+ targetDBSchema := model.NewDatabaseSchema(targetMetadata, nil, nil, storepb.Engine_POSTGRES, false)
+
+ diff, err := schema.GetDatabaseSchemaDiff(storepb.Engine_POSTGRES, sourceDBSchema, targetDBSchema)
+ require.NoError(t, err)
+
+ migration, err := generateMigration(diff)
+ require.NoError(t, err)
+
+ // Key assertion: no COMMENT statements should be generated
+ require.NotContains(t, migration, "COMMENT ON", "Should not generate COMMENT statements for identical comments")
+}
diff --git a/backend/plugin/schema/pg/sdl_comprehensive_test.go b/backend/plugin/schema/pg/sdl_comprehensive_test.go
deleted file mode 100644
index aa452e6a22..0000000000
--- a/backend/plugin/schema/pg/sdl_comprehensive_test.go
+++ /dev/null
@@ -1,391 +0,0 @@
-package pg
-
-import (
- "context"
- "database/sql"
- "encoding/json"
- "fmt"
- "os"
- "path/filepath"
- "strings"
- "testing"
- "time"
-
- "github.com/jackc/pgx/v5"
- "github.com/jackc/pgx/v5/stdlib"
- "github.com/pkg/errors"
- "github.com/stretchr/testify/require"
- "github.com/testcontainers/testcontainers-go"
- "github.com/testcontainers/testcontainers-go/modules/postgres"
- "github.com/testcontainers/testcontainers-go/wait"
-)
-
-// TestScenario represents a complete test scenario with all required files
-type TestScenario struct {
- Name string
- Category string
- Description string
- InitialSchema string
- ExpectedSchema string
- ExpectedDiff string
- ExpectedMigration string
-}
-
-// TestSDLComprehensiveValidation runs comprehensive SDL validation tests organized by categories
-func TestSDLComprehensiveValidation(t *testing.T) {
- ctx := context.Background()
-
- // Start PostgreSQL container
- pgContainer, err := postgres.Run(ctx,
- "postgres:16-alpine",
- postgres.WithDatabase("test_db"),
- postgres.WithUsername("postgres"),
- postgres.WithPassword("test"),
- testcontainers.WithWaitStrategy(
- wait.ForLog("database system is ready to accept connections").
- WithOccurrence(2).
- WithStartupTimeout(5*time.Minute),
- ),
- )
- require.NoError(t, err)
- defer func() {
- if err := pgContainer.Terminate(ctx); err != nil {
- t.Logf("failed to terminate container: %s", err)
- }
- }()
-
- // Get connection string
- connectionString, err := pgContainer.ConnectionString(ctx, "sslmode=disable")
- require.NoError(t, err)
-
- // Connect to the database
- connConfig, err := pgx.ParseConfig(connectionString)
- require.NoError(t, err)
- db := stdlib.OpenDB(*connConfig)
- defer db.Close()
-
- // Load test scenarios from organized files
- testScenarios, err := loadTestScenarios()
- require.NoError(t, err)
- require.NotEmpty(t, testScenarios, "No test scenarios found")
-
- // Group scenarios by category for better organization
- categorizedTests := make(map[string][]TestScenario)
- for _, scenario := range testScenarios {
- categorizedTests[scenario.Category] = append(categorizedTests[scenario.Category], scenario)
- }
-
- // Run tests by category
- for category, scenarios := range categorizedTests {
- t.Run(category, func(t *testing.T) {
- for _, scenario := range scenarios {
- t.Run(scenario.Name, func(t *testing.T) {
- runSDLTestScenario(ctx, t, connConfig, db, scenario)
- })
- }
- })
- }
-}
-
-// loadTestScenarios loads all test scenarios from the sdl_testdata directory
-func loadTestScenarios() ([]TestScenario, error) {
- var scenarios []TestScenario
-
- testDataDir := "sdl_testdata"
- if _, err := os.Stat(testDataDir); os.IsNotExist(err) {
- return nil, errors.Errorf("test data directory %s does not exist", testDataDir)
- }
-
- err := filepath.Walk(testDataDir, func(path string, info os.FileInfo, err error) error {
- if err != nil {
- return err
- }
-
- // Look for test_description.md files to identify complete test scenarios
- if info.Name() == "test_description.md" {
- scenario, err := loadTestScenario(filepath.Dir(path))
- if err != nil {
- return errors.Wrapf(err, "failed to load scenario from %s", path)
- }
- scenarios = append(scenarios, scenario)
- }
- return nil
- })
-
- if err != nil {
- return nil, err
- }
-
- // If no scenarios found in files, return error
- if len(scenarios) == 0 {
- return nil, errors.New("no test scenarios found in test data directory")
- }
-
- return scenarios, nil
-}
-
-// loadTestScenario loads a single test scenario from a directory
-func loadTestScenario(scenarioDir string) (TestScenario, error) {
- var scenario TestScenario
-
- // Extract category and name from path
- relativePath, err := filepath.Rel("sdl_testdata", scenarioDir)
- if err != nil {
- return scenario, err
- }
-
- pathParts := strings.Split(relativePath, string(filepath.Separator))
- scenario.Category = pathParts[0]
- scenario.Name = strings.Join(pathParts[1:], "_")
-
- // Load description
- descPath := filepath.Join(scenarioDir, "test_description.md")
- if descData, err := os.ReadFile(descPath); err == nil {
- scenario.Description = string(descData)
- }
-
- // Load initial schema
- initialPath := filepath.Join(scenarioDir, "initial_schema.sql")
- if initialData, err := os.ReadFile(initialPath); err == nil {
- scenario.InitialSchema = string(initialData)
- }
-
- // Load expected schema
- expectedPath := filepath.Join(scenarioDir, "expected_schema.sql")
- if expectedData, err := os.ReadFile(expectedPath); err == nil {
- scenario.ExpectedSchema = string(expectedData)
- } else {
- return scenario, errors.New("expected_schema.sql is required")
- }
-
- // Load expected diff (optional)
- diffPath := filepath.Join(scenarioDir, "diff.json")
- if diffData, err := os.ReadFile(diffPath); err == nil {
- scenario.ExpectedDiff = string(diffData)
- }
-
- // Load expected migration (optional)
- migrationPath := filepath.Join(scenarioDir, "ddl.sql")
- if migrationData, err := os.ReadFile(migrationPath); err == nil {
- scenario.ExpectedMigration = string(migrationData)
- }
-
- return scenario, nil
-}
-
-// runSDLTestScenario executes a single SDL test scenario
-func runSDLTestScenario(ctx context.Context, t *testing.T, connConfig *pgx.ConnConfig, mainDB *sql.DB, scenario TestScenario) {
- t.Logf("Running SDL test scenario: %s - %s", scenario.Category, scenario.Name)
- if scenario.Description != "" {
- t.Logf("Description: %s", strings.TrimSpace(scenario.Description))
- }
-
- // Create a unique database for this test (PostgreSQL limit is 63 chars)
- category := strings.ReplaceAll(scenario.Category, "/", "_")
- name := strings.ReplaceAll(scenario.Name, "/", "_")
-
- // Shorten common prefixes to avoid length issues
- category = strings.ReplaceAll(category, "table_operations", "tbl_ops")
- name = strings.ReplaceAll(name, "column_operations_", "col_")
- name = strings.ReplaceAll(name, "add_column_", "add_")
- name = strings.ReplaceAll(name, "constraints", "cons")
- name = strings.ReplaceAll(name, "foreign_keys", "fk")
- name = strings.ReplaceAll(name, "indexes", "idx")
-
- dbName := fmt.Sprintf("sdl_test_%s_%s", category, name)
- dbName = strings.ReplaceAll(dbName, "-", "_") // Replace hyphens with underscores
- dbName = strings.ToLower(dbName)
-
- // Ensure length doesn't exceed PostgreSQL's 63 character limit
- if len(dbName) > 63 {
- dbName = dbName[:63]
- }
-
- _, err := mainDB.Exec(fmt.Sprintf("CREATE DATABASE %s", dbName))
- require.NoError(t, err)
- defer func() {
- _, _ = mainDB.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %s", dbName))
- }()
-
- // Connect to the test database
- testConnConfig := *connConfig
- testConnConfig.Database = dbName
- testDB := stdlib.OpenDB(testConnConfig)
- defer testDB.Close()
-
- // Execute the enhanced SDL validation process
- err = executeEnhancedSDLValidation(ctx, t, &testConnConfig, testDB, dbName, scenario)
- require.NoError(t, err, "SDL validation failed for scenario: %s/%s", scenario.Category, scenario.Name)
-}
-
-// executeEnhancedSDLValidation implements the enhanced 5-step SDL validation with diff comparison
-func executeEnhancedSDLValidation(ctx context.Context, t *testing.T, connConfig *pgx.ConnConfig, testDB *sql.DB, dbName string, scenario TestScenario) error {
- t.Log("=== Step 1: Apply initial schema to database ===")
- if strings.TrimSpace(scenario.InitialSchema) != "" {
- _, err := testDB.Exec(scenario.InitialSchema)
- if err != nil {
- return errors.Wrapf(err, "failed to apply initial schema")
- }
- t.Logf("✓ Applied initial schema (%d characters)", len(scenario.InitialSchema))
- } else {
- t.Log("✓ Starting with empty database")
- }
-
- t.Log("=== Step 2: Sync to get current schema metadata (Schema B) ===")
- schemaB, err := getSyncMetadataForSDL(ctx, connConfig, dbName)
- if err != nil {
- return errors.Wrapf(err, "failed to sync schema B")
- }
- t.Log("✓ Synced current schema metadata")
-
- t.Log("=== Step 3: Get expected schema metadata and generate migration DDL ===")
- schemaCMetadata, err := GetDatabaseMetadata(scenario.ExpectedSchema)
- if err != nil {
- return errors.Wrapf(err, "failed to get expected schema C metadata")
- }
- t.Log("✓ Parsed expected schema metadata")
-
- // Generate migration DDL from schema B to schema C using the existing function
- migrationDDL, schemaDiff, err := generateMigrationDDLFromMetadata(schemaB, schemaCMetadata)
- require.NoError(t, err, "Failed to generate migration DDL from metadata")
-
- // Use validateOrSaveTestFiles to generate/validate diff.json and ddl.sql files
- testName := strings.ReplaceAll(t.Name(), "/", "_")
- err = validateOrSaveTestFiles(t, schemaDiff, testName, migrationDDL)
- if err != nil {
- return errors.Wrapf(err, "test file validation/creation failed")
- }
-
- // Compare with expected diff if provided
- if scenario.ExpectedDiff != "" {
- err := validateSchemaDiff(t, schemaDiff, scenario.ExpectedDiff)
- if err != nil {
- t.Logf("WARNING: Schema diff validation failed: %v", err)
- // Note: This is a warning for now, can be made stricter later
- } else {
- t.Log("✓ Generated schema diff matches expected")
- }
- }
-
- t.Logf("✓ Generated migration DDL (%d characters)", len(migrationDDL))
- if migrationDDL != "" {
- t.Logf("Migration DDL preview (first 200 chars):\n%s",
- truncateString(migrationDDL, 200))
- }
-
- // Compare with expected migration DDL if provided
- if scenario.ExpectedMigration != "" {
- normalizedGenerated := normalizeSQLString(migrationDDL)
- normalizedExpected := normalizeSQLString(scenario.ExpectedMigration)
- if normalizedGenerated != normalizedExpected {
- t.Log("Generated migration DDL differs from expected:")
- t.Logf("Expected:\n%s", scenario.ExpectedMigration)
- t.Logf("Generated:\n%s", migrationDDL)
-
- // For comprehensive testing, we can make this a hard requirement
- if strings.TrimSpace(normalizedExpected) != "" {
- require.Equal(t, normalizedExpected, normalizedGenerated,
- "Generated migration DDL does not match expected DDL")
- }
- } else {
- t.Log("✓ Generated migration DDL matches expected")
- }
- }
-
- t.Log("=== Step 4: Apply migration DDL and sync final schema ===")
- if strings.TrimSpace(migrationDDL) != "" {
- _, err := testDB.Exec(migrationDDL)
- if err != nil {
- return errors.Wrapf(err, "failed to apply migration DDL: %s", migrationDDL)
- }
- t.Log("✓ Applied migration DDL")
- } else {
- t.Log("✓ No migration DDL needed (schemas already match)")
- }
-
- // Sync to get final schema D
- schemaD, err := getSyncMetadataForSDL(ctx, connConfig, dbName)
- if err != nil {
- return errors.Wrapf(err, "failed to sync schema D after migration")
- }
- t.Log("✓ Synced final schema metadata")
-
- t.Log("=== Step 5: Validate schema consistency ===")
- err = validateSchemaConsistency(schemaD, schemaCMetadata)
- if err != nil {
- return errors.Wrapf(err, "schema validation failed")
- }
- t.Log("✓ Final schema matches expected schema perfectly")
-
- return nil
-}
-
-// normalizeSQLString normalizes SQL for comparison by removing extra whitespace and standardizing formatting
-func normalizeSQLString(sql string) string {
- // Remove extra whitespace and normalize line endings
- lines := strings.Split(sql, "\n")
- var normalizedLines []string
- for _, line := range lines {
- trimmed := strings.TrimSpace(line)
- if trimmed != "" {
- normalizedLines = append(normalizedLines, trimmed)
- }
- }
- return strings.Join(normalizedLines, "\n")
-}
-
-// truncateString truncates a string to maxLength characters
-func truncateString(s string, maxLength int) string {
- if len(s) <= maxLength {
- return s
- }
- return s[:maxLength] + "..."
-}
-
-// validateSchemaDiff validates that the generated schema diff matches the expected diff
-func validateSchemaDiff(t *testing.T, actualDiff any, expectedDiffJSON string) error {
- // For now, we'll do basic validation by comparing the string representations
- // In the future, this could be enhanced to do structured JSON comparison
-
- actualJSON, err := marshalDiffToJSON(actualDiff)
- if err != nil {
- return errors.Wrap(err, "failed to marshal actual diff to JSON")
- }
-
- // Normalize both JSON strings for comparison
- actualNormalized := normalizeJSONString(actualJSON)
- expectedNormalized := normalizeJSONString(expectedDiffJSON)
-
- if actualNormalized != expectedNormalized {
- t.Log("Schema diff comparison:")
- t.Logf("Expected diff:\n%s", expectedDiffJSON)
- t.Logf("Actual diff:\n%s", actualJSON)
- return errors.New("schema diff does not match expected")
- }
-
- return nil
-}
-
-// marshalDiffToJSON marshals a schema diff object to JSON string
-func marshalDiffToJSON(diff any) (string, error) {
- // Convert the diff object to JSON
- jsonBytes, err := json.MarshalIndent(diff, "", " ")
- if err != nil {
- return "", errors.Wrap(err, "failed to marshal diff to JSON")
- }
- return string(jsonBytes), nil
-}
-
-// normalizeJSONString normalizes JSON for comparison
-func normalizeJSONString(jsonStr string) string {
- // Remove extra whitespace and normalize formatting
- lines := strings.Split(jsonStr, "\n")
- var normalizedLines []string
- for _, line := range lines {
- trimmed := strings.TrimSpace(line)
- if trimmed != "" {
- normalizedLines = append(normalizedLines, trimmed)
- }
- }
- return strings.Join(normalizedLines, "\n")
-}
diff --git a/backend/plugin/schema/pg/sdl_validation_testcontainer_test.go b/backend/plugin/schema/pg/sdl_validation_testcontainer_test.go
index 04a40c551c..d94d1b0cd9 100644
--- a/backend/plugin/schema/pg/sdl_validation_testcontainer_test.go
+++ b/backend/plugin/schema/pg/sdl_validation_testcontainer_test.go
@@ -1672,12 +1672,10 @@ func validateFileContent(filename, expectedContent, fileType string) error {
// getTestDataDirectory converts a test name to the corresponding test data directory path
func getTestDataDirectory(testName string) string {
- // Handle both TestSDLValidationFromTestData_ and TestSDLComprehensiveValidation_ prefixes
+ // Handle TestSDLValidationFromTestData_ prefix
var pathPart string
if strings.HasPrefix(testName, "TestSDLValidationFromTestData_") {
pathPart = strings.TrimPrefix(testName, "TestSDLValidationFromTestData_")
- } else if strings.HasPrefix(testName, "TestSDLComprehensiveValidation_") {
- pathPart = strings.TrimPrefix(testName, "TestSDLComprehensiveValidation_")
} else {
return ""
}