Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat: implement fine-grained usability handling and fix constraint processing logic (#17525)
rebelice8b4c83e46ac2
diff --git a/backend/plugin/schema/pg/get_sdl_diff.go b/backend/plugin/schema/pg/get_sdl_diff.goindex 7880bd26aa..507539e164 100644--- a/backend/plugin/schema/pg/get_sdl_diff.go+++ b/backend/plugin/schema/pg/get_sdl_diff.go@@ -1,6 +1,7 @@package pgimport (+ "fmt""strings""github.com/antlr4-go/antlr/v4"@@ -48,6 +49,13 @@ func GetSDLDiff(currentSDLText, previousUserSDLText string, currentSchema, previ}}+ // Pre-compute SDL chunks from current database metadata for performance optimization+ // This avoids repeated calls to convertDatabaseSchemaToSDL and ChunkSDLText during usability checks+ currentDBSDLChunks, err := buildCurrentDatabaseSDLChunks(currentSchema)+ if err != nil {+ return nil, errors.Wrap(err, "failed to build current database SDL chunks")+ }+// Initialize MetadataDiffdiff := &schema.MetadataDiff{DatabaseName: "",@@ -62,22 +70,22 @@ func GetSDLDiff(currentSDLText, previousUserSDLText string, currentSchema, previ}// Process table changes- err = processTableChanges(currentChunks, previousChunks, currentSchema, previousSchema, diff)+ err = processTableChanges(currentChunks, previousChunks, currentSchema, previousSchema, currentDBSDLChunks, diff)if err != nil {return nil, errors.Wrap(err, "failed to process table changes")}// Process index changes (standalone CREATE INDEX statements)- processStandaloneIndexChanges(currentChunks, previousChunks, diff)+ processStandaloneIndexChanges(currentChunks, previousChunks, currentDBSDLChunks, diff)// Process view changes- processViewChanges(currentChunks, previousChunks, diff)+ processViewChanges(currentChunks, previousChunks, currentDBSDLChunks, diff)// Process function changes- processFunctionChanges(currentChunks, previousChunks, diff)+ processFunctionChanges(currentChunks, previousChunks, currentDBSDLChunks, diff)// Process sequence changes- processSequenceChanges(currentChunks, previousChunks, diff)+ processSequenceChanges(currentChunks, previousChunks, currentDBSDLChunks, diff)return diff, nil}@@ -270,20 +278,20 @@ func (l *sdlChunkExtractor) EnterViewstmt(ctx *parser.ViewstmtContext) {// processTableChanges processes changes to tables by comparing SDL chunks// nolint:unparam-func processTableChanges(currentChunks, previousChunks *schema.SDLChunks, currentSchema, previousSchema *model.DatabaseSchema, diff *schema.MetadataDiff) error {- // TODO: currentSchema and previousSchema will be used later for SDL drift detection- _ = currentSchema- _ = previousSchema-+func processTableChanges(currentChunks, previousChunks *schema.SDLChunks, currentSchema, previousSchema *model.DatabaseSchema, currentDBSDLChunks *currentDatabaseSDLChunks, diff *schema.MetadataDiff) error {// Process current table chunks to find created and modified tables- for identifier, currentChunk := range currentChunks.Tables {+ for _, currentChunk := range currentChunks.Tables {schemaName, tableName := parseIdentifier(currentChunk.Identifier)- if previousChunk, exists := previousChunks.Tables[identifier]; exists {+ if previousChunk, exists := previousChunks.Tables[currentChunk.Identifier]; exists {// Table exists in both - check if modifiedcurrentText := currentChunk.GetText()previousText := previousChunk.GetText()if currentText != previousText {+ // Apply usability check: skip diff if current chunk matches database metadata SDL+ if currentDBSDLChunks.shouldSkipChunkDiffForUsability(currentText, currentChunk.Identifier) {+ continue+ }// Table was modified - process column changesoldASTNode, ok := previousChunk.ASTNode.(*parser.CreatestmtContext)if !ok {@@ -294,11 +302,11 @@ func processTableChanges(currentChunks, previousChunks *schema.SDLChunks, currenreturn errors.Errorf("expected CreatestmtContext for current table %s", currentChunk.Identifier)}- columnChanges := processColumnChanges(oldASTNode, newASTNode, currentSchema, previousSchema)- foreignKeyChanges := processForeignKeyChanges(oldASTNode, newASTNode)- checkConstraintChanges := processCheckConstraintChanges(oldASTNode, newASTNode)- primaryKeyChanges := processPrimaryKeyChanges(oldASTNode, newASTNode)- uniqueConstraintChanges := processUniqueConstraintChanges(oldASTNode, newASTNode)+ columnChanges := processColumnChanges(oldASTNode, newASTNode, currentSchema, previousSchema, currentDBSDLChunks, currentChunk.Identifier)+ foreignKeyChanges := processForeignKeyChanges(oldASTNode, newASTNode, currentDBSDLChunks, currentChunk.Identifier)+ checkConstraintChanges := processCheckConstraintChanges(oldASTNode, newASTNode, currentDBSDLChunks, currentChunk.Identifier)+ primaryKeyChanges := processPrimaryKeyChanges(oldASTNode, newASTNode, currentDBSDLChunks, currentChunk.Identifier)+ uniqueConstraintChanges := processUniqueConstraintChanges(oldASTNode, newASTNode, currentDBSDLChunks, currentChunk.Identifier)tableDiff := &schema.TableDiff{Action: schema.MetadataDiffActionAlter,@@ -391,7 +399,7 @@ func processTableChanges(currentChunks, previousChunks *schema.SDLChunks, curren// processColumnChanges analyzes column changes between old and new table definitions// Following the same pattern as processTableChanges: compare text first, then analyze differences// If schemas are nil, operates in AST-only mode without metadata extraction-func processColumnChanges(oldTable, newTable *parser.CreatestmtContext, currentSchema, previousSchema *model.DatabaseSchema) []*schema.ColumnDiff {+func processColumnChanges(oldTable, newTable *parser.CreatestmtContext, currentSchema, previousSchema *model.DatabaseSchema, currentDBSDLChunks *currentDatabaseSDLChunks, tableIdentifier string) []*schema.ColumnDiff {if oldTable == nil || newTable == nil {return []*schema.ColumnDiff{}}@@ -414,6 +422,11 @@ func processColumnChanges(oldTable, newTable *parser.CreatestmtContext, currentScurrentText := getColumnText(newColumnDef.ASTNode)previousText := getColumnText(oldColumnDef.ASTNode)if currentText != previousText {+ // Apply column-level usability check: skip diff if current column matches database metadata SDL+ schemaName, tableName := parseIdentifier(tableIdentifier)+ if currentDBSDLChunks != nil && currentDBSDLChunks.shouldSkipColumnDiffForUsability(currentText, schemaName, tableName, columnName) {+ continue+ }// Column was modified - extract metadata only if not in AST-only modevar oldColumn, newColumn *storepb.ColumnMetadataif !astOnlyMode {@@ -713,7 +726,7 @@ type IndexDefWithAST struct {// processForeignKeyChanges analyzes foreign key constraint changes between old and new table definitions// Following the text-first comparison pattern for performance optimization-func processForeignKeyChanges(oldTable, newTable *parser.CreatestmtContext) []*schema.ForeignKeyDiff {+func processForeignKeyChanges(oldTable, newTable *parser.CreatestmtContext, currentDBSDLChunks *currentDatabaseSDLChunks, tableIdentifier string) []*schema.ForeignKeyDiff {if oldTable == nil || newTable == nil {return []*schema.ForeignKeyDiff{}}@@ -734,43 +747,28 @@ func processForeignKeyChanges(oldTable, newTable *parser.CreatestmtContext) []*svar fkDiffs []*schema.ForeignKeyDiff- // Step 2: Process old foreign keys in reverse order for DROP operations- for i := len(oldFKList) - 1; i >= 0; i-- {- oldFKDef := oldFKList[i]- if newFKDef, exists := newFKMap[oldFKDef.Name]; exists {+ // Step 2: Process current foreign keys to find created and modified foreign keys+ for _, newFKDef := range newFKList {+ if oldFKDef, exists := oldFKMap[newFKDef.Name]; exists {// FK exists in both - check if modified by comparing text firstcurrentText := getForeignKeyText(newFKDef.ASTNode)previousText := getForeignKeyText(oldFKDef.ASTNode)if currentText != previousText {- // FK was modified - store AST nodes only+ // Apply constraint-level usability check: skip diff if current constraint matches database metadata SDL+ schemaName, tableName := parseIdentifier(tableIdentifier)+ if currentDBSDLChunks != nil && currentDBSDLChunks.shouldSkipConstraintDiffForUsability(currentText, schemaName, tableName, newFKDef.Name) {+ continue+ }+ // FK was modified - drop and recreate (PostgreSQL pattern)fkDiffs = append(fkDiffs, &schema.ForeignKeyDiff{Action: schema.MetadataDiffActionDrop,OldASTNode: oldFKDef.ASTNode,})- }- } else {- // Foreign key was dropped - store AST node only- fkDiffs = append(fkDiffs, &schema.ForeignKeyDiff{- Action: schema.MetadataDiffActionDrop,- OldASTNode: oldFKDef.ASTNode,- })- }- }-- // Step 3: Process new foreign keys in order for CREATE operations- for _, newFKDef := range newFKList {- if oldFKDef, exists := oldFKMap[newFKDef.Name]; exists {- // FK exists in both - check if modified by comparing text first- currentText := getForeignKeyText(newFKDef.ASTNode)- previousText := getForeignKeyText(oldFKDef.ASTNode)- if currentText != previousText {- // FK was modified - store AST nodes onlyfkDiffs = append(fkDiffs, &schema.ForeignKeyDiff{Action: schema.MetadataDiffActionCreate,NewASTNode: newFKDef.ASTNode,})}- // If text is identical, skip - no changes detected} else {// New foreign key - store AST node onlyfkDiffs = append(fkDiffs, &schema.ForeignKeyDiff{@@ -780,12 +778,23 @@ func processForeignKeyChanges(oldTable, newTable *parser.CreatestmtContext) []*s}}+ // Step 3: Process old foreign keys to find dropped ones+ for _, oldFKDef := range oldFKList {+ if _, exists := newFKMap[oldFKDef.Name]; !exists {+ // Foreign key was dropped - store AST node only+ fkDiffs = append(fkDiffs, &schema.ForeignKeyDiff{+ Action: schema.MetadataDiffActionDrop,+ OldASTNode: oldFKDef.ASTNode,+ })+ }+ }+return fkDiffs}// processCheckConstraintChanges analyzes check constraint changes between old and new table definitions// Following the text-first comparison pattern for performance optimization-func processCheckConstraintChanges(oldTable, newTable *parser.CreatestmtContext) []*schema.CheckConstraintDiff {+func processCheckConstraintChanges(oldTable, newTable *parser.CreatestmtContext, currentDBSDLChunks *currentDatabaseSDLChunks, tableIdentifier string) []*schema.CheckConstraintDiff {if oldTable == nil || newTable == nil {return []*schema.CheckConstraintDiff{}}@@ -806,44 +815,28 @@ func processCheckConstraintChanges(oldTable, newTable *parser.CreatestmtContext)var checkDiffs []*schema.CheckConstraintDiff- // Step 2: Process old constraints in reverse order for DROP operations- for i := len(oldCheckList) - 1; i >= 0; i-- {- oldCheckDef := oldCheckList[i]- if newCheckDef, exists := newCheckMap[oldCheckDef.Name]; exists {+ // Step 2: Process current check constraints to find created and modified check constraints+ for _, newCheckDef := range newCheckList {+ if oldCheckDef, exists := oldCheckMap[newCheckDef.Name]; exists {// Check constraint exists in both - check if modified by comparing text firstcurrentText := getCheckConstraintText(newCheckDef.ASTNode)previousText := getCheckConstraintText(oldCheckDef.ASTNode)if currentText != previousText {- // Check constraint was modified - store AST nodes only- // Drop and recreate for modifications (PostgreSQL pattern)+ // Apply constraint-level usability check: skip diff if current constraint matches database metadata SDL+ schemaName, tableName := parseIdentifier(tableIdentifier)+ if currentDBSDLChunks != nil && currentDBSDLChunks.shouldSkipConstraintDiffForUsability(currentText, schemaName, tableName, newCheckDef.Name) {+ continue+ }+ // Check constraint was modified - drop and recreate (PostgreSQL pattern)checkDiffs = append(checkDiffs, &schema.CheckConstraintDiff{Action: schema.MetadataDiffActionDrop,OldASTNode: oldCheckDef.ASTNode,})- }- } else {- // Check constraint was dropped - store AST node only- checkDiffs = append(checkDiffs, &schema.CheckConstraintDiff{- Action: schema.MetadataDiffActionDrop,- OldASTNode: oldCheckDef.ASTNode,- })- }- }-- // Step 3: Process new constraints in order for CREATE operations- for _, newCheckDef := range newCheckList {- if oldCheckDef, exists := oldCheckMap[newCheckDef.Name]; exists {- // Check constraint exists in both - check if modified by comparing text first- currentText := getCheckConstraintText(newCheckDef.ASTNode)- previousText := getCheckConstraintText(oldCheckDef.ASTNode)- if currentText != previousText {- // Check constraint was modified - store AST nodes onlycheckDiffs = append(checkDiffs, &schema.CheckConstraintDiff{Action: schema.MetadataDiffActionCreate,NewASTNode: newCheckDef.ASTNode,})}- // If text is identical, skip - no changes detected} else {// New check constraint - store AST node onlycheckDiffs = append(checkDiffs, &schema.CheckConstraintDiff{@@ -853,11 +846,22 @@ func processCheckConstraintChanges(oldTable, newTable *parser.CreatestmtContext)}}+ // Step 3: Process old check constraints to find dropped ones+ for _, oldCheckDef := range oldCheckList {+ if _, exists := newCheckMap[oldCheckDef.Name]; !exists {+ // Check constraint was dropped - store AST node only+ checkDiffs = append(checkDiffs, &schema.CheckConstraintDiff{+ Action: schema.MetadataDiffActionDrop,+ OldASTNode: oldCheckDef.ASTNode,+ })+ }+ }+return checkDiffs}// processPrimaryKeyChanges analyzes primary key constraint changes between old and new table definitions-func processPrimaryKeyChanges(oldTable, newTable *parser.CreatestmtContext) []*schema.PrimaryKeyDiff {+func processPrimaryKeyChanges(oldTable, newTable *parser.CreatestmtContext, currentDBSDLChunks *currentDatabaseSDLChunks, tableIdentifier string) []*schema.PrimaryKeyDiff {if oldTable == nil || newTable == nil {return []*schema.PrimaryKeyDiff{}}@@ -875,6 +879,11 @@ func processPrimaryKeyChanges(oldTable, newTable *parser.CreatestmtContext) []*scurrentText := getIndexText(newPKDef.ASTNode)previousText := getIndexText(oldPKDef.ASTNode)if currentText != previousText {+ // Apply constraint-level usability check: skip diff if current constraint matches database metadata SDL+ schemaName, tableName := parseIdentifier(tableIdentifier)+ if currentDBSDLChunks != nil && currentDBSDLChunks.shouldSkipConstraintDiffForUsability(currentText, schemaName, tableName, pkName) {+ continue+ }// PK was modified - store AST nodes onlypkDiffs = append(pkDiffs, &schema.PrimaryKeyDiff{Action: schema.MetadataDiffActionDrop,@@ -909,7 +918,7 @@ func processPrimaryKeyChanges(oldTable, newTable *parser.CreatestmtContext) []*s}// processUniqueConstraintChanges analyzes unique constraint changes between old and new table definitions-func processUniqueConstraintChanges(oldTable, newTable *parser.CreatestmtContext) []*schema.UniqueConstraintDiff {+func processUniqueConstraintChanges(oldTable, newTable *parser.CreatestmtContext, currentDBSDLChunks *currentDatabaseSDLChunks, tableIdentifier string) []*schema.UniqueConstraintDiff {if oldTable == nil || newTable == nil {return []*schema.UniqueConstraintDiff{}}@@ -930,43 +939,28 @@ func processUniqueConstraintChanges(oldTable, newTable *parser.CreatestmtContextvar ukDiffs []*schema.UniqueConstraintDiff- // Step 2: Process old constraints in reverse order for DROP operations- for i := len(oldUKList) - 1; i >= 0; i-- {- oldUKDef := oldUKList[i]- if newUKDef, exists := newUKMap[oldUKDef.Name]; exists {+ // Step 2: Process current unique constraints to find created and modified unique constraints+ for _, newUKDef := range newUKList {+ if oldUKDef, exists := oldUKMap[newUKDef.Name]; exists {// UK exists in both - check if modified by comparing text firstcurrentText := getIndexText(newUKDef.ASTNode)previousText := getIndexText(oldUKDef.ASTNode)if currentText != previousText {- // UK was modified - store AST nodes only+ // Apply constraint-level usability check: skip diff if current constraint matches database metadata SDL+ schemaName, tableName := parseIdentifier(tableIdentifier)+ if currentDBSDLChunks != nil && currentDBSDLChunks.shouldSkipConstraintDiffForUsability(currentText, schemaName, tableName, newUKDef.Name) {+ continue+ }+ // UK was modified - drop and recreate (PostgreSQL pattern)ukDiffs = append(ukDiffs, &schema.UniqueConstraintDiff{Action: schema.MetadataDiffActionDrop,OldASTNode: oldUKDef.ASTNode,})- }- } else {- // UK was dropped - store AST node only- ukDiffs = append(ukDiffs, &schema.UniqueConstraintDiff{- Action: schema.MetadataDiffActionDrop,- OldASTNode: oldUKDef.ASTNode,- })- }- }-- // Step 3: Process new constraints in order for CREATE operations- for _, newUKDef := range newUKList {- if oldUKDef, exists := oldUKMap[newUKDef.Name]; exists {- // UK exists in both - check if modified by comparing text first- currentText := getIndexText(newUKDef.ASTNode)- previousText := getIndexText(oldUKDef.ASTNode)- if currentText != previousText {- // UK was modified - store AST nodes onlyukDiffs = append(ukDiffs, &schema.UniqueConstraintDiff{Action: schema.MetadataDiffActionCreate,NewASTNode: newUKDef.ASTNode,})}- // If text is identical, skip - no changes detected} else {// New UK - store AST node onlyukDiffs = append(ukDiffs, &schema.UniqueConstraintDiff{@@ -976,6 +970,17 @@ func processUniqueConstraintChanges(oldTable, newTable *parser.CreatestmtContext}}+ // Step 3: Process old unique constraints to find dropped ones+ for _, oldUKDef := range oldUKList {+ if _, exists := newUKMap[oldUKDef.Name]; !exists {+ // UK was dropped - store AST node only+ ukDiffs = append(ukDiffs, &schema.UniqueConstraintDiff{+ Action: schema.MetadataDiffActionDrop,+ OldASTNode: oldUKDef.ASTNode,+ })+ }+ }+return ukDiffs}@@ -1242,7 +1247,7 @@ func getIndexText(constraintAST parser.ITableconstraintContext) string {// processStandaloneIndexChanges analyzes standalone CREATE INDEX statement changes// and adds them to the appropriate table's IndexChanges-func processStandaloneIndexChanges(currentChunks, previousChunks *schema.SDLChunks, diff *schema.MetadataDiff) {+func processStandaloneIndexChanges(currentChunks, previousChunks *schema.SDLChunks, currentDBSDLChunks *currentDatabaseSDLChunks, diff *schema.MetadataDiff) {if currentChunks == nil || previousChunks == nil {return}@@ -1254,17 +1259,21 @@ func processStandaloneIndexChanges(currentChunks, previousChunks *schema.SDLChun}// Step 1: Process current indexes to find created and modified indexes- for indexName, currentChunk := range currentChunks.Indexes {+ for _, currentChunk := range currentChunks.Indexes {tableName := extractTableNameFromIndex(currentChunk.ASTNode)if tableName == "" {continue // Skip if we can't determine the table name}- if previousChunk, exists := previousChunks.Indexes[indexName]; exists {+ if previousChunk, exists := previousChunks.Indexes[currentChunk.Identifier]; exists {// Index exists in both - check if modified by comparing text firstcurrentText := getStandaloneIndexText(currentChunk.ASTNode)previousText := getStandaloneIndexText(previousChunk.ASTNode)if currentText != previousText {+ // Apply usability check: skip diff if current chunk matches database metadata+ if currentDBSDLChunks.shouldSkipChunkDiffForUsability(currentText, currentChunk.Identifier) {+ continue+ }// Index was modified - use drop and recreate pattern (PostgreSQL standard)tableDiff := getOrCreateTableDiff(diff, tableName, affectedTables)tableDiff.IndexChanges = append(tableDiff.IndexChanges, &schema.IndexDiff{@@ -1380,16 +1389,20 @@ func getOrCreateTableDiff(diff *schema.MetadataDiff, tableName string, affectedT// processViewChanges analyzes view changes between current and previous chunks// Following the text-first comparison pattern for performance optimization-func processViewChanges(currentChunks, previousChunks *schema.SDLChunks, diff *schema.MetadataDiff) {+func processViewChanges(currentChunks, previousChunks *schema.SDLChunks, currentDBSDLChunks *currentDatabaseSDLChunks, diff *schema.MetadataDiff) {// Process current views to find created and modified views- for identifier, currentChunk := range currentChunks.Views {- if previousChunk, exists := previousChunks.Views[identifier]; exists {+ for _, currentChunk := range currentChunks.Views {+ if previousChunk, exists := previousChunks.Views[currentChunk.Identifier]; exists {// View exists in both - check if modified by comparing text firstcurrentText := currentChunk.GetText()previousText := previousChunk.GetText()if currentText != previousText {+ // Apply usability check: skip diff if current chunk matches database metadata SDL+ if currentDBSDLChunks.shouldSkipChunkDiffForUsability(currentText, currentChunk.Identifier) {+ continue+ }// View was modified - use drop and recreate pattern (PostgreSQL standard)- schemaName, viewName := parseIdentifier(identifier)+ schemaName, viewName := parseIdentifier(currentChunk.Identifier)diff.ViewChanges = append(diff.ViewChanges, &schema.ViewDiff{Action: schema.MetadataDiffActionDrop,SchemaName: schemaName,@@ -1412,7 +1425,7 @@ func processViewChanges(currentChunks, previousChunks *schema.SDLChunks, diff *s// If text is identical, skip - no changes detected} else {// New view- schemaName, viewName := parseIdentifier(identifier)+ schemaName, viewName := parseIdentifier(currentChunk.Identifier)diff.ViewChanges = append(diff.ViewChanges, &schema.ViewDiff{Action: schema.MetadataDiffActionCreate,SchemaName: schemaName,@@ -1445,16 +1458,20 @@ func processViewChanges(currentChunks, previousChunks *schema.SDLChunks, diff *s// processFunctionChanges analyzes function changes between current and previous chunks// Following the text-first comparison pattern for performance optimization-func processFunctionChanges(currentChunks, previousChunks *schema.SDLChunks, diff *schema.MetadataDiff) {+func processFunctionChanges(currentChunks, previousChunks *schema.SDLChunks, currentDBSDLChunks *currentDatabaseSDLChunks, diff *schema.MetadataDiff) {// Process current functions to find created and modified functions- for identifier, currentChunk := range currentChunks.Functions {- if previousChunk, exists := previousChunks.Functions[identifier]; exists {+ for _, currentChunk := range currentChunks.Functions {+ if previousChunk, exists := previousChunks.Functions[currentChunk.Identifier]; exists {// Function exists in both - check if modified by comparing text firstcurrentText := currentChunk.GetText()previousText := previousChunk.GetText()if currentText != previousText {+ // Apply usability check: skip diff if current chunk matches database metadata SDL+ if currentDBSDLChunks.shouldSkipChunkDiffForUsability(currentText, currentChunk.Identifier) {+ continue+ }// Function was modified - use CREATE OR REPLACE (AST-only mode)- schemaName, functionName := parseIdentifier(identifier)+ schemaName, functionName := parseIdentifier(currentChunk.Identifier)diff.FunctionChanges = append(diff.FunctionChanges, &schema.FunctionDiff{Action: schema.MetadataDiffActionAlter,SchemaName: schemaName,@@ -1468,7 +1485,7 @@ func processFunctionChanges(currentChunks, previousChunks *schema.SDLChunks, dif// If text is identical, skip - no changes detected} else {// New function- schemaName, functionName := parseIdentifier(identifier)+ schemaName, functionName := parseIdentifier(currentChunk.Identifier)diff.FunctionChanges = append(diff.FunctionChanges, &schema.FunctionDiff{Action: schema.MetadataDiffActionCreate,SchemaName: schemaName,@@ -1501,16 +1518,20 @@ func processFunctionChanges(currentChunks, previousChunks *schema.SDLChunks, dif// processSequenceChanges analyzes sequence changes between current and previous chunks// Following the text-first comparison pattern for performance optimization-func processSequenceChanges(currentChunks, previousChunks *schema.SDLChunks, diff *schema.MetadataDiff) {+func processSequenceChanges(currentChunks, previousChunks *schema.SDLChunks, currentDBSDLChunks *currentDatabaseSDLChunks, diff *schema.MetadataDiff) {// Process current sequences to find created and modified sequences- for identifier, currentChunk := range currentChunks.Sequences {- if previousChunk, exists := previousChunks.Sequences[identifier]; exists {+ for _, currentChunk := range currentChunks.Sequences {+ if previousChunk, exists := previousChunks.Sequences[currentChunk.Identifier]; exists {// Sequence exists in both - check if modified by comparing text firstcurrentText := currentChunk.GetText()previousText := previousChunk.GetText()if currentText != previousText {+ // Apply usability check: skip diff if current chunk matches database metadata SDL+ if currentDBSDLChunks.shouldSkipChunkDiffForUsability(currentText, currentChunk.Identifier) {+ continue+ }// Sequence was modified - use drop and recreate pattern (PostgreSQL standard)- schemaName, sequenceName := parseIdentifier(identifier)+ schemaName, sequenceName := parseIdentifier(currentChunk.Identifier)diff.SequenceChanges = append(diff.SequenceChanges, &schema.SequenceDiff{Action: schema.MetadataDiffActionDrop,SchemaName: schemaName,@@ -1533,7 +1554,7 @@ func processSequenceChanges(currentChunks, previousChunks *schema.SDLChunks, dif// If text is identical, skip - no changes detected} else {// New sequence- schemaName, sequenceName := parseIdentifier(identifier)+ schemaName, sequenceName := parseIdentifier(currentChunk.Identifier)diff.SequenceChanges = append(diff.SequenceChanges, &schema.SequenceDiff{Action: schema.MetadataDiffActionCreate,SchemaName: schemaName,@@ -1608,8 +1629,10 @@ func applyMinimalChangesToChunks(previousChunks *schema.SDLChunks, currentSchema// Build existing chunk keys mapping for table operationsexistingChunkKeys := make(map[string]string) // schema.table -> chunk key- for chunkKey := range previousChunks.Tables {- existingChunkKeys[chunkKey] = chunkKey+ for chunkKey, chunk := range previousChunks.Tables {+ if chunk != nil {+ existingChunkKeys[chunk.Identifier] = chunkKey+ }}// Process table additions: add new tables to chunks@@ -1700,32 +1723,51 @@ func applyMinimalChangesToChunks(previousChunks *schema.SDLChunks, currentSchemareturn nil}-// applyTableChangesToChunk applies minimal column and constraint changes to an existing CREATE TABLE chunk using a single ANTLR rewriter+// applyTableChangesToChunk applies minimal column and constraint changes to an existing CREATE TABLE chunk+// by working with the individual chunk's SQL text instead of the full script's tokenStreamfunc applyTableChangesToChunk(chunk *schema.SDLChunk, currentTable, previousTable *storepb.TableMetadata) error {if chunk == nil || chunk.ASTNode == nil || currentTable == nil || previousTable == nil {return nil}- createStmt, ok := chunk.ASTNode.(*parser.CreatestmtContext)- if !ok {- return errors.New("chunk AST node is not a CREATE TABLE statement")+ // Get the original chunk text+ originalChunkText := chunk.GetText()+ if originalChunkText == "" {+ return errors.New("chunk has no text content")+ }++ // Parse the individual chunk text to get a fresh AST with its own tokenStream+ parseResult, err := pgparser.ParsePostgreSQL(originalChunkText)+ if err != nil {+ return errors.Wrapf(err, "failed to parse original chunk text: %s", originalChunkText)+ }++ // Extract the CREATE TABLE AST node from the fresh parse+ var createStmt *parser.CreatestmtContext+ antlr.ParseTreeWalkerDefault.Walk(&createTableExtractor{+ result: &createStmt,+ }, parseResult.Tree)++ if createStmt == nil {+ return errors.New("failed to extract CREATE TABLE AST node from chunk text")}- // Get the token stream and create a single rewriter for both column and constraint changes+ // Get the parser and tokenStream from the fresh parsectxParser := createStmt.GetParser()if ctxParser == nil {- return errors.New("parser not available for AST node")+ return errors.New("parser not available for fresh AST node")}tokenStream := ctxParser.GetTokenStream()if tokenStream == nil {- return errors.New("token stream not available for parser")+ return errors.New("token stream not available for fresh parser")}+ // Create rewriter for the individual chunk's tokenStreamrewriter := antlr.NewTokenStreamRewriter(tokenStream)- // Apply column changes using the shared rewriter- err := applyColumnChanges(rewriter, createStmt, currentTable, previousTable)+ // Apply column changes using the rewriter+ err = applyColumnChanges(rewriter, createStmt, currentTable, previousTable)if err != nil {return errors.Wrapf(err, "failed to apply column changes")}@@ -1736,20 +1778,20 @@ func applyTableChangesToChunk(chunk *schema.SDLChunk, currentTable, previousTablreturn errors.Wrapf(err, "failed to apply constraint changes")}- // Apply all changes and update the chunk with a single parse+ // Get the modified SQL from the rewritermodifiedSQL := rewriter.GetTextDefault()- // Parse the modified SQL to get the new AST (only once for both column and constraint changes)- parseResult, err := pgparser.ParsePostgreSQL(modifiedSQL)+ // Parse the modified SQL to get the final AST+ finalParseResult, err := pgparser.ParsePostgreSQL(modifiedSQL)if err != nil {return errors.Wrapf(err, "failed to parse modified SQL: %s", modifiedSQL)}- // Extract the new CREATE TABLE AST node+ // Extract the final CREATE TABLE AST nodevar newCreateTableNode *parser.CreatestmtContextantlr.ParseTreeWalkerDefault.Walk(&createTableExtractor{result: &newCreateTableNode,- }, parseResult.Tree)+ }, finalParseResult.Tree)if newCreateTableNode == nil {return errors.New("failed to extract CREATE TABLE AST node from modified text")@@ -2049,13 +2091,29 @@ func deleteColumnFromAST(rewriter *antlr.TokenStreamRewriter, columnDef parser.Ireturn errors.New("unable to get column definition tokens")}+ // Find the actual deletion range including commas and whitespace+ deleteStartIndex := startToken.GetTokenIndex()+ deleteEndIndex := stopToken.GetTokenIndex()+// Strategy: Always try to remove the following comma first// This handles cases where there are table constraints after columnsnextCommaIndex := -1+ nextCommaEndIndex := -1for i := stopToken.GetTokenIndex() + 1; i < rewriter.GetTokenStream().Size(); i++ {token := rewriter.GetTokenStream().Get(i)if token.GetTokenType() == parser.PostgreSQLParserCOMMA {nextCommaIndex = i+ // Look for whitespace after the comma to include in deletion+ nextCommaEndIndex = i+ for j := i + 1; j < rewriter.GetTokenStream().Size(); j++ {+ nextToken := rewriter.GetTokenStream().Get(j)+ // Include whitespace and newlines after the comma+ if nextToken.GetChannel() != antlr.TokenDefaultChannel {+ nextCommaEndIndex = j+ } else {+ break+ }+ }break}// Skip whitespace and comments, but stop at other meaningful tokens@@ -2066,17 +2124,28 @@ func deleteColumnFromAST(rewriter *antlr.TokenStreamRewriter, columnDef parser.I}if nextCommaIndex != -1 {- // Found a following comma - remove column and the comma- // This works for all cases: middle columns, columns followed by constraints, etc.- rewriter.DeleteDefault(startToken.GetTokenIndex(), nextCommaIndex)+ // Found a following comma - remove column, comma, and trailing whitespace+ deleteEndIndex = nextCommaEndIndex} else {// No following comma found - this might be the last element// Try to find a preceding comma to removeprevCommaIndex := -1+ prevCommaStartIndex := -1for i := startToken.GetTokenIndex() - 1; i >= 0; i-- {token := rewriter.GetTokenStream().Get(i)if token.GetTokenType() == parser.PostgreSQLParserCOMMA {prevCommaIndex = i+ // Look for whitespace before the comma to include in deletion+ prevCommaStartIndex = i+ for j := i - 1; j >= 0; j-- {+ prevToken := rewriter.GetTokenStream().Get(j)+ // Include whitespace and newlines before the comma+ if prevToken.GetChannel() != antlr.TokenDefaultChannel {+ prevCommaStartIndex = j+ } else {+ break+ }+ }break}// Skip whitespace and comments, but stop at other meaningful tokens@@ -2086,14 +2155,25 @@ func deleteColumnFromAST(rewriter *antlr.TokenStreamRewriter, columnDef parser.I}if prevCommaIndex != -1 {- // Found a preceding comma - remove it along with the column- rewriter.DeleteDefault(prevCommaIndex, stopToken.GetTokenIndex())+ // Found a preceding comma - remove it along with leading whitespace and the column+ deleteStartIndex = prevCommaStartIndex} else {// No comma found (single column case) - just remove the column- rewriter.DeleteDefault(startToken.GetTokenIndex(), stopToken.GetTokenIndex())+ // But also clean up any trailing whitespace that might leave empty lines+ for i := stopToken.GetTokenIndex() + 1; i < rewriter.GetTokenStream().Size(); i++ {+ token := rewriter.GetTokenStream().Get(i)+ if token.GetChannel() != antlr.TokenDefaultChannel {+ deleteEndIndex = i+ } else {+ break+ }+ }}}+ // Perform the deletion with the computed range+ rewriter.DeleteDefault(deleteStartIndex, deleteEndIndex)+return nil}@@ -2193,6 +2273,192 @@ func columnsEqual(a, b *storepb.ColumnMetadata) bool {a.Collation == b.Collation}+// currentDatabaseSDLChunks stores pre-computed SDL chunks from current database metadata+// for performance optimization during usability checks+type currentDatabaseSDLChunks struct {+ chunks map[string]string // maps chunk identifier to normalized SDL text from current database metadata+ columns map[string]string // maps "schema.table.column" to normalized column SDL text+ constraints map[string]string // maps "schema.table.constraint" to normalized constraint SDL text+}++// buildCurrentDatabaseSDLChunks pre-computes SDL chunks from the current database schema+// for usability checks. This avoids repeated expensive calls to convertDatabaseSchemaToSDL+// and ChunkSDLText during diff processing by storing normalized SDL text from current database metadata.+func buildCurrentDatabaseSDLChunks(currentSchema *model.DatabaseSchema) (*currentDatabaseSDLChunks, error) {+ sdlChunks := ¤tDatabaseSDLChunks{+ chunks: make(map[string]string),+ columns: make(map[string]string),+ constraints: make(map[string]string),+ }++ // Only build SDL chunks if current schema is provided+ if currentSchema == nil {+ return sdlChunks, nil+ }++ // Generate SDL from current database metadata+ currentSDL, err := convertDatabaseSchemaToSDL(currentSchema)+ if err != nil {+ return nil, errors.Wrap(err, "failed to convert current schema to SDL")+ }++ // Parse the generated SDL to get chunks+ currentSDLChunks, err := ChunkSDLText(currentSDL)+ if err != nil {+ return nil, errors.Wrap(err, "failed to chunk current schema SDL")+ }++ // Populate SDL chunks with normalized chunk texts from current database metadata+ for identifier, chunk := range currentSDLChunks.Tables {+ sdlChunks.chunks[identifier] = strings.TrimSpace(chunk.GetText())++ // Extract column and constraint SDL texts for fine-grained usability checks+ if err := extractColumnAndConstraintSDLTexts(chunk, identifier, sdlChunks); err != nil {+ // Log error but don't fail the entire operation+ // Fine-grained usability checks will fall back to table-level checks+ continue+ }+ }+ for identifier, chunk := range currentSDLChunks.Views {+ sdlChunks.chunks[identifier] = strings.TrimSpace(chunk.GetText())+ }+ for identifier, chunk := range currentSDLChunks.Functions {+ sdlChunks.chunks[identifier] = strings.TrimSpace(chunk.GetText())+ }+ for identifier, chunk := range currentSDLChunks.Sequences {+ sdlChunks.chunks[identifier] = strings.TrimSpace(chunk.GetText())+ }+ for identifier, chunk := range currentSDLChunks.Indexes {+ sdlChunks.chunks[identifier] = strings.TrimSpace(chunk.GetText())+ }++ return sdlChunks, nil+}++// extractColumnAndConstraintSDLTexts extracts individual column and constraint SDL texts+// from a table chunk for fine-grained usability checks+func extractColumnAndConstraintSDLTexts(chunk *schema.SDLChunk, tableIdentifier string, sdlChunks *currentDatabaseSDLChunks) error {+ if chunk == nil || chunk.ASTNode == nil {+ return nil+ }++ createStmt, ok := chunk.ASTNode.(*parser.CreatestmtContext)+ if !ok {+ return errors.New("chunk AST node is not a CREATE TABLE statement")+ }++ schemaName, tableName := parseIdentifier(tableIdentifier)++ // Extract column definitions with their SDL texts+ columnDefs := extractColumnDefinitionsWithAST(createStmt)+ for columnName, columnDef := range columnDefs.Map {+ columnSDLText := strings.TrimSpace(getColumnText(columnDef.ASTNode))+ columnKey := fmt.Sprintf("%s.%s.%s", schemaName, tableName, columnName)+ sdlChunks.columns[columnKey] = columnSDLText+ }++ // Extract constraint definitions with their SDL texts+ // Primary Key constraints+ pkDefs := extractPrimaryKeyDefinitionsWithAST(createStmt)+ for constraintName, pkDef := range pkDefs {+ constraintSDLText := strings.TrimSpace(getIndexText(pkDef.ASTNode))+ constraintKey := fmt.Sprintf("%s.%s.%s", schemaName, tableName, constraintName)+ sdlChunks.constraints[constraintKey] = constraintSDLText+ }++ // Unique constraints+ uniqueDefs := extractUniqueConstraintDefinitionsInOrder(createStmt)+ for _, uniqueDef := range uniqueDefs {+ constraintSDLText := strings.TrimSpace(getIndexText(uniqueDef.ASTNode))+ constraintKey := fmt.Sprintf("%s.%s.%s", schemaName, tableName, uniqueDef.Name)+ sdlChunks.constraints[constraintKey] = constraintSDLText+ }++ // Check constraints+ checkDefs := extractCheckConstraintDefinitionsWithAST(createStmt)+ for _, checkDef := range checkDefs {+ constraintSDLText := strings.TrimSpace(getCheckConstraintText(checkDef.ASTNode))+ constraintKey := fmt.Sprintf("%s.%s.%s", schemaName, tableName, checkDef.Name)+ sdlChunks.constraints[constraintKey] = constraintSDLText+ }++ // Foreign Key constraints+ fkDefs := extractForeignKeyDefinitionsWithAST(createStmt)+ for _, fkDef := range fkDefs {+ constraintSDLText := strings.TrimSpace(getForeignKeyText(fkDef.ASTNode))+ constraintKey := fmt.Sprintf("%s.%s.%s", schemaName, tableName, fkDef.Name)+ sdlChunks.constraints[constraintKey] = constraintSDLText+ }++ return nil+}++// shouldSkipChunkDiffForUsability checks if a chunk should skip diff comparison+// by comparing against the pre-computed SDL chunks from current database metadata+func (sdlChunks *currentDatabaseSDLChunks) shouldSkipChunkDiffForUsability(chunkText string, chunkIdentifier string) bool {+ if sdlChunks == nil || len(sdlChunks.chunks) == 0 {+ return false+ }++ // Get the corresponding SDL text from current database metadata+ currentDatabaseSDLText, exists := sdlChunks.chunks[chunkIdentifier]+ if !exists {+ return false+ }++ // Normalize current chunk text for comparison+ normalizedChunkText := strings.TrimSpace(chunkText)++ // If chunk text matches current database metadata SDL, skip the diff (no actual change needed)+ return normalizedChunkText == currentDatabaseSDLText+}++// shouldSkipColumnDiffForUsability checks if a column should skip diff comparison+// by comparing against the pre-computed column SDL texts from current database metadata+func (sdlChunks *currentDatabaseSDLChunks) shouldSkipColumnDiffForUsability(columnText string, schemaName, tableName, columnName string) bool {+ if sdlChunks == nil || len(sdlChunks.columns) == 0 {+ return false+ }++ // Build column key+ columnKey := fmt.Sprintf("%s.%s.%s", schemaName, tableName, columnName)++ // Get the corresponding SDL text from current database metadata+ currentDatabaseColumnSDL, exists := sdlChunks.columns[columnKey]+ if !exists {+ return false+ }++ // Normalize current column text for comparison+ normalizedColumnText := strings.TrimSpace(columnText)++ // If column text matches current database metadata SDL, skip the diff (no actual change needed)+ return normalizedColumnText == currentDatabaseColumnSDL+}++// shouldSkipConstraintDiffForUsability checks if a constraint should skip diff comparison+// by comparing against the pre-computed constraint SDL texts from current database metadata+func (sdlChunks *currentDatabaseSDLChunks) shouldSkipConstraintDiffForUsability(constraintText string, schemaName, tableName, constraintName string) bool {+ if sdlChunks == nil || len(sdlChunks.constraints) == 0 {+ return false+ }++ // Build constraint key+ constraintKey := fmt.Sprintf("%s.%s.%s", schemaName, tableName, constraintName)++ // Get the corresponding SDL text from current database metadata+ currentDatabaseConstraintSDL, exists := sdlChunks.constraints[constraintKey]+ if !exists {+ return false+ }++ // Normalize current constraint text for comparison+ normalizedConstraintText := strings.TrimSpace(constraintText)++ // If constraint text matches current database metadata SDL, skip the diff (no actual change needed)+ return normalizedConstraintText == currentDatabaseConstraintSDL+}+// convertDatabaseSchemaToSDL converts a model.DatabaseSchema to SDL format string// This is used in initialization scenarios where previousUserSDLText is emptyfunc convertDatabaseSchemaToSDL(dbSchema *model.DatabaseSchema) (string, error) {@@ -2223,13 +2489,29 @@ func deleteConstraintFromAST(rewriter *antlr.TokenStreamRewriter, constraintASTreturn errors.New("unable to get constraint definition tokens")}+ // Find the actual deletion range including commas and whitespace+ deleteStartIndex := startToken.GetTokenIndex()+ deleteEndIndex := stopToken.GetTokenIndex()+// Strategy: Always try to remove the following comma first// This handles cases where there are more constraints after this onenextCommaIndex := -1+ nextCommaEndIndex := -1for i := stopToken.GetTokenIndex() + 1; i < rewriter.GetTokenStream().Size(); i++ {token := rewriter.GetTokenStream().Get(i)if token.GetTokenType() == parser.PostgreSQLParserCOMMA {nextCommaIndex = i+ // Look for whitespace after the comma to include in deletion+ nextCommaEndIndex = i+ for j := i + 1; j < rewriter.GetTokenStream().Size(); j++ {+ nextToken := rewriter.GetTokenStream().Get(j)+ // Include whitespace and newlines after the comma+ if nextToken.GetChannel() != antlr.TokenDefaultChannel {+ nextCommaEndIndex = j+ } else {+ break+ }+ }break}// Skip whitespace and comments, but stop at other meaningful tokens@@ -2240,16 +2522,28 @@ func deleteConstraintFromAST(rewriter *antlr.TokenStreamRewriter, constraintAST}if nextCommaIndex != -1 {- // Found a following comma - remove constraint and the comma- rewriter.DeleteDefault(startToken.GetTokenIndex(), nextCommaIndex)+ // Found a following comma - remove constraint, comma, and trailing whitespace+ deleteEndIndex = nextCommaEndIndex} else {// No following comma found - this might be the last element// Try to find a preceding comma to removeprevCommaIndex := -1+ prevCommaStartIndex := -1for i := startToken.GetTokenIndex() - 1; i >= 0; i-- {token := rewriter.GetTokenStream().Get(i)if token.GetTokenType() == parser.PostgreSQLParserCOMMA {prevCommaIndex = i+ // Look for whitespace before the comma to include in deletion+ prevCommaStartIndex = i+ for j := i - 1; j >= 0; j-- {+ prevToken := rewriter.GetTokenStream().Get(j)+ // Include whitespace and newlines before the comma+ if prevToken.GetChannel() != antlr.TokenDefaultChannel {+ prevCommaStartIndex = j+ } else {+ break+ }+ }break}// Skip whitespace and comments, but stop at other meaningful tokens@@ -2259,14 +2553,25 @@ func deleteConstraintFromAST(rewriter *antlr.TokenStreamRewriter, constraintAST}if prevCommaIndex != -1 {- // Found a preceding comma - remove it along with the constraint- rewriter.DeleteDefault(prevCommaIndex, stopToken.GetTokenIndex())+ // Found a preceding comma - remove it along with leading whitespace and the constraint+ deleteStartIndex = prevCommaStartIndex} else {// No comma found (single constraint case) - just remove the constraint- rewriter.DeleteDefault(startToken.GetTokenIndex(), stopToken.GetTokenIndex())+ // But also clean up any trailing whitespace that might leave empty lines+ for i := stopToken.GetTokenIndex() + 1; i < rewriter.GetTokenStream().Size(); i++ {+ token := rewriter.GetTokenStream().Get(i)+ if token.GetChannel() != antlr.TokenDefaultChannel {+ deleteEndIndex = i+ } else {+ break+ }+ }}}+ // Perform the deletion with the computed range+ rewriter.DeleteDefault(deleteStartIndex, deleteEndIndex)+return nil}diff --git a/backend/plugin/schema/pg/get_sdl_diff_test.go b/backend/plugin/schema/pg/get_sdl_diff_test.goindex 32e79a9f79..5f42351ee5 100644--- a/backend/plugin/schema/pg/get_sdl_diff_test.go+++ b/backend/plugin/schema/pg/get_sdl_diff_test.go@@ -1,12 +1,17 @@package pgimport (+ "fmt""strings""testing"+ "github.com/stretchr/testify/assert""github.com/stretchr/testify/require"+ parser "github.com/bytebase/parser/postgresql"+storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/plugin/schema""github.com/bytebase/bytebase/backend/store/model")@@ -482,3 +487,1240 @@ func TestApplyMinimalChangesToChunks(t *testing.T) {})}}++// TestGetSDLDiff_UsabilityHandling tests the usability feature that skips diffs+// when current SDL chunk matches the SDL generated from current database metadata+func TestGetSDLDiff_UsabilityHandling(t *testing.T) {+ tests := []struct {+ name string+ currentSDLText string+ previousUserSDLText string+ currentSchema *model.DatabaseSchema+ expectedTableChanges int+ expectedViewChanges int+ expectedFunctionChanges int+ expectedSequenceChanges int+ description string+ }{+ {+ name: "table_format_difference_but_same_structure",+ currentSDLText: `CREATE TABLE "public"."users" (+ "id" integer DEFAULT nextval('users_id_seq'::regclass) NOT NULL,+ "name" text NOT NULL,+ CONSTRAINT "users_pkey" PRIMARY KEY (id)+);`,+ previousUserSDLText: `CREATE TABLE public.users (+ id SERIAL PRIMARY KEY,+ name TEXT NOT NULL+);`,+ currentSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Tables: []*storepb.TableMetadata{+ {+ Name: "users",+ Columns: []*storepb.ColumnMetadata{+ {+ Name: "id",+ Type: "integer",+ Nullable: false,+ Default: "nextval('users_id_seq'::regclass)",+ },+ {+ Name: "name",+ Type: "text",+ Nullable: false,+ },+ },+ Indexes: []*storepb.IndexMetadata{+ {+ Name: "users_pkey",+ Unique: true,+ Primary: true,+ KeyLength: []int64{},+ Expressions: []string{"id"},+ },+ },+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ expectedTableChanges: 0, // Should skip because current SDL matches database metadata+ expectedViewChanges: 0,+ expectedFunctionChanges: 0,+ expectedSequenceChanges: 0,+ description: "When current SDL matches database metadata format, skip diff even if previous format was different",+ },+ {+ name: "view_format_difference_but_same_definition",+ currentSDLText: `CREATE VIEW "public"."user_view" AS SELECT users.id, users.name FROM public.users;`,+ previousUserSDLText: `CREATE VIEW user_view AS SELECT id, name FROM users;`,+ currentSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Views: []*storepb.ViewMetadata{+ {+ Name: "user_view",+ Definition: "SELECT users.id, users.name FROM public.users;",+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ expectedTableChanges: 0,+ expectedViewChanges: 0, // Should skip because current SDL matches database metadata+ expectedFunctionChanges: 0,+ expectedSequenceChanges: 0,+ description: "When current view SDL matches database metadata format, skip diff even if previous format was different",+ },+ {+ name: "actual_structure_change_should_not_skip",+ currentSDLText: `CREATE TABLE public.users (+ id integer NOT NULL DEFAULT nextval('users_id_seq'::regclass),+ name text NOT NULL,+ email text,+ CONSTRAINT users_pkey PRIMARY KEY (id)+);`,+ previousUserSDLText: `CREATE TABLE public.users (+ id SERIAL PRIMARY KEY,+ name TEXT NOT NULL+);`,+ currentSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Tables: []*storepb.TableMetadata{+ {+ Name: "users",+ Columns: []*storepb.ColumnMetadata{+ {+ Name: "id",+ Type: "integer",+ Nullable: false,+ Default: "nextval('users_id_seq'::regclass)",+ },+ {+ Name: "name",+ Type: "text",+ Nullable: false,+ },+ },+ Indexes: []*storepb.IndexMetadata{+ {+ Name: "users_pkey",+ Unique: true,+ Primary: true,+ KeyLength: []int64{},+ Expressions: []string{"id"},+ },+ },+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ expectedTableChanges: 1, // Should NOT skip because there is an actual structural difference (email column)+ expectedViewChanges: 0,+ expectedFunctionChanges: 0,+ expectedSequenceChanges: 0,+ description: "When there is actual structural difference, do not skip diff even if formats are different",+ },+ {+ name: "pk_uk_column_quotes_format_difference_should_skip",+ currentSDLText: `CREATE TABLE "public"."users" (+ "id" integer DEFAULT nextval('users_id_seq'::regclass) NOT NULL,+ "name" text NOT NULL,+ "email" text,+ CONSTRAINT "users_pkey" PRIMARY KEY (id)+);++CREATE UNIQUE INDEX "users_email_key" ON ONLY "public"."users" (email);`,+ previousUserSDLText: `CREATE TABLE "public"."users" (+ "id" integer DEFAULT nextval('users_id_seq'::regclass) NOT NULL,+ "name" text NOT NULL,+ "email" text,+ CONSTRAINT "users_pkey" PRIMARY KEY ("id")+);++CREATE UNIQUE INDEX "users_email_key" ON ONLY "public"."users" ("email");`,+ currentSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Tables: []*storepb.TableMetadata{+ {+ Name: "users",+ Columns: []*storepb.ColumnMetadata{+ {+ Name: "id",+ Type: "integer",+ Nullable: false,+ Default: "nextval('users_id_seq'::regclass)",+ },+ {+ Name: "name",+ Type: "text",+ Nullable: false,+ },+ {+ Name: "email",+ Type: "text",+ Nullable: true,+ },+ },+ Indexes: []*storepb.IndexMetadata{+ {+ Name: "users_pkey",+ Unique: true,+ Primary: true,+ KeyLength: []int64{},+ Expressions: []string{"id"},+ },+ {+ Name: "users_email_key",+ Unique: true,+ Primary: false,+ KeyLength: []int64{},+ Expressions: []string{"email"},+ },+ },+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ expectedTableChanges: 0, // Should skip because only PK/UK column quotes differ, same structure+ expectedViewChanges: 0,+ expectedFunctionChanges: 0,+ expectedSequenceChanges: 0,+ description: "When current SDL matches database metadata except for PK/UK column quotes (old vs new format), should skip diff",+ },+ {+ name: "pk_column_quotes_only_format_difference_should_skip",+ currentSDLText: `CREATE TABLE "public"."orders" (+ "order_id" bigint DEFAULT nextval('orders_order_id_seq'::regclass) NOT NULL,+ "customer_name" text NOT NULL,+ CONSTRAINT "orders_pkey" PRIMARY KEY (order_id)+);`,+ previousUserSDLText: `CREATE TABLE "public"."orders" (+ "order_id" bigint DEFAULT nextval('orders_order_id_seq'::regclass) NOT NULL,+ "customer_name" text NOT NULL,+ CONSTRAINT "orders_pkey" PRIMARY KEY ("order_id")+);`,+ currentSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Tables: []*storepb.TableMetadata{+ {+ Name: "orders",+ Columns: []*storepb.ColumnMetadata{+ {+ Name: "order_id",+ Type: "bigint",+ Nullable: false,+ Default: "nextval('orders_order_id_seq'::regclass)",+ },+ {+ Name: "customer_name",+ Type: "text",+ Nullable: false,+ },+ },+ Indexes: []*storepb.IndexMetadata{+ {+ Name: "orders_pkey",+ Unique: true,+ Primary: true,+ KeyLength: []int64{},+ Expressions: []string{"order_id"},+ },+ },+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ expectedTableChanges: 0, // Should skip because only PK column quotes differ (new format vs old format)+ expectedViewChanges: 0,+ expectedFunctionChanges: 0,+ expectedSequenceChanges: 0,+ description: "When current SDL matches database metadata except for PK column quotes only (new vs old Bytebase format), should skip diff",+ },+ {+ name: "fine_grained_column_level_usability_handling",+ currentSDLText: `CREATE TABLE "public"."products" (+ "id" integer DEFAULT nextval('products_id_seq'::regclass) NOT NULL,+ "name" text NOT NULL,+ "price" numeric(10,2)+);`,+ previousUserSDLText: `CREATE TABLE products (+ id SERIAL PRIMARY KEY,+ name TEXT NOT NULL,+ price DECIMAL(10,2)+);`,+ currentSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Tables: []*storepb.TableMetadata{+ {+ Name: "products",+ Columns: []*storepb.ColumnMetadata{+ {+ Name: "id",+ Type: "integer",+ Nullable: false,+ Default: "nextval('products_id_seq'::regclass)",+ },+ {+ Name: "name",+ Type: "text",+ Nullable: false,+ },+ {+ Name: "price",+ Type: "numeric(10,2)",+ Nullable: true,+ },+ },+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ expectedTableChanges: 0, // Should skip column-level changes because current columns match database metadata+ expectedViewChanges: 0,+ expectedFunctionChanges: 0,+ expectedSequenceChanges: 0,+ description: "When individual columns match database metadata format, skip column-level diffs even if table format differs",+ },+ {+ name: "fine_grained_constraint_level_usability_handling",+ currentSDLText: `CREATE TABLE "public"."orders" (+ "order_id" bigint DEFAULT nextval('orders_order_id_seq'::regclass) NOT NULL,+ "customer_name" text NOT NULL,+ CONSTRAINT "orders_pkey" PRIMARY KEY (order_id),+ CONSTRAINT "orders_customer_name_check" CHECK ((length(customer_name) > 0))+);`,+ previousUserSDLText: `CREATE TABLE orders (+ order_id BIGSERIAL PRIMARY KEY,+ customer_name TEXT NOT NULL CHECK (length(customer_name) > 0)+);`,+ currentSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Tables: []*storepb.TableMetadata{+ {+ Name: "orders",+ Columns: []*storepb.ColumnMetadata{+ {+ Name: "order_id",+ Type: "bigint",+ Nullable: false,+ Default: "nextval('orders_order_id_seq'::regclass)",+ },+ {+ Name: "customer_name",+ Type: "text",+ Nullable: false,+ },+ },+ Indexes: []*storepb.IndexMetadata{+ {+ Name: "orders_pkey",+ Unique: true,+ Primary: true,+ KeyLength: []int64{},+ Expressions: []string{"order_id"},+ },+ },+ CheckConstraints: []*storepb.CheckConstraintMetadata{+ {+ Name: "orders_customer_name_check",+ Expression: "(length(customer_name) > 0)",+ },+ },+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ expectedTableChanges: 0, // Should skip constraint-level changes because current constraints match database metadata+ expectedViewChanges: 0,+ expectedFunctionChanges: 0,+ expectedSequenceChanges: 0,+ description: "When individual constraints match database metadata format, skip constraint-level diffs even if table format differs",+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ t.Logf("Test description: %s", tt.description)++ // Call GetSDLDiff+ diff, err := GetSDLDiff(tt.currentSDLText, tt.previousUserSDLText, tt.currentSchema, nil)+ require.NoError(t, err)+ require.NotNil(t, diff)++ // Check the number of changes+ require.Equal(t, tt.expectedTableChanges, len(diff.TableChanges),+ "Expected %d table changes but got %d", tt.expectedTableChanges, len(diff.TableChanges))+ require.Equal(t, tt.expectedViewChanges, len(diff.ViewChanges),+ "Expected %d view changes but got %d", tt.expectedViewChanges, len(diff.ViewChanges))+ require.Equal(t, tt.expectedFunctionChanges, len(diff.FunctionChanges),+ "Expected %d function changes but got %d", tt.expectedFunctionChanges, len(diff.FunctionChanges))+ require.Equal(t, tt.expectedSequenceChanges, len(diff.SequenceChanges),+ "Expected %d sequence changes but got %d", tt.expectedSequenceChanges, len(diff.SequenceChanges))++ // Log the detected changes for debugging+ if len(diff.TableChanges) > 0 {+ t.Log("Detected table changes:")+ for i, change := range diff.TableChanges {+ t.Logf(" %d. Table: %s.%s, Action: %v", i+1, change.SchemaName, change.TableName, change.Action)+ }+ }+ })+ }+}++// TestShouldSkipChunkDiffForUsability tests the core usability logic+func TestShouldSkipChunkDiffForUsability(t *testing.T) {+ // Create a test schema+ testSchema := model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Tables: []*storepb.TableMetadata{+ {+ Name: "users",+ Columns: []*storepb.ColumnMetadata{+ {+ Name: "id",+ Type: "integer",+ Nullable: false,+ Default: "nextval('users_id_seq'::regclass)",+ },+ {+ Name: "name",+ Type: "text",+ Nullable: false,+ },+ },+ Indexes: []*storepb.IndexMetadata{+ {+ Name: "users_pkey",+ Unique: true,+ Primary: true,+ KeyLength: []int64{},+ Expressions: []string{"id"},+ },+ },+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ )++ tests := []struct {+ name string+ chunkText string+ chunkID string+ currentSchema *model.DatabaseSchema+ expectedSkip bool+ description string+ }{+ {+ name: "matching_table_should_skip",+ chunkText: `CREATE TABLE "public"."users" (+ "id" integer DEFAULT nextval('users_id_seq'::regclass) NOT NULL,+ "name" text NOT NULL,+ CONSTRAINT "users_pkey" PRIMARY KEY (id)+)`,+ chunkID: "public.users",+ currentSchema: testSchema,+ expectedSkip: true,+ description: "Chunk text matches database metadata SDL, should skip",+ },+ {+ name: "different_table_should_not_skip",+ chunkText: `CREATE TABLE public.users (+ id SERIAL PRIMARY KEY,+ name TEXT NOT NULL,+ email TEXT+);`,+ chunkID: "public.users",+ currentSchema: testSchema,+ expectedSkip: false,+ description: "Chunk text differs from database metadata SDL, should not skip",+ },+ {+ name: "nil_schema_should_not_skip",+ chunkText: "CREATE TABLE users (id SERIAL PRIMARY KEY);",+ chunkID: "public.users",+ currentSchema: nil,+ expectedSkip: false,+ description: "When no current schema provided, should not skip",+ },+ {+ name: "unknown_chunk_should_not_skip",+ chunkText: "CREATE TABLE nonexistent (id SERIAL PRIMARY KEY);",+ chunkID: "public.nonexistent",+ currentSchema: testSchema,+ expectedSkip: false,+ description: "When chunk doesn't exist in database metadata, should not skip",+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ t.Logf("Test description: %s", tt.description)++ // Build current database SDL chunks+ currentDBSDLChunks, err := buildCurrentDatabaseSDLChunks(tt.currentSchema)+ require.NoError(t, err)++ result := currentDBSDLChunks.shouldSkipChunkDiffForUsability(tt.chunkText, tt.chunkID)+ require.Equal(t, tt.expectedSkip, result,+ "Expected skip=%v but got skip=%v", tt.expectedSkip, result)+ })+ }+}++// TestCurrentDatabaseSDLChunksPerformance validates that the current database SDL chunks provide performance benefits+func TestCurrentDatabaseSDLChunksPerformance(t *testing.T) {+ // Create a simple test schema+ testSchema := model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Tables: []*storepb.TableMetadata{+ {+ Name: "users",+ Columns: []*storepb.ColumnMetadata{+ {+ Name: "id",+ Type: "integer",+ Nullable: false,+ Default: "nextval('users_id_seq'::regclass)",+ },+ {+ Name: "name",+ Type: "text",+ Nullable: false,+ },+ },+ Indexes: []*storepb.IndexMetadata{+ {+ Name: "users_pkey",+ Unique: true,+ Primary: true,+ KeyLength: []int64{},+ Expressions: []string{"id"},+ },+ },+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ )++ // Build current database SDL chunks once+ currentDBSDLChunks, err := buildCurrentDatabaseSDLChunks(testSchema)+ require.NoError(t, err)+ require.NotNil(t, currentDBSDLChunks)++ // Verify that SDL chunks contain expected entries+ require.Greater(t, len(currentDBSDLChunks.chunks), 0, "SDL chunks should contain chunks")+ t.Logf("Current database SDL chunks contains %d chunks", len(currentDBSDLChunks.chunks))++ // Test that cache lookup is fast (should be O(1))+ testChunkText := `CREATE TABLE "public"."users" (+ "id" integer DEFAULT nextval('users_id_seq'::regclass) NOT NULL,+ "name" text NOT NULL,+ CONSTRAINT "users_pkey" PRIMARY KEY (id)+)`++ // Multiple lookups should be fast+ found := false+ for i := 0; i < 100; i++ {+ result := currentDBSDLChunks.shouldSkipChunkDiffForUsability(testChunkText, "public.users")+ if result {+ found = true+ }+ }++ // At least verify the SDL chunks work+ require.True(t, found, "Current database SDL chunks should have found a match at least once")+ t.Log("Performance test completed successfully - current database SDL chunks provide O(1) lookups")+}++func TestApplyMinimalChangesToChunks_MultipleTableCorruption(t *testing.T) {+ testCases := []struct {+ name string+ description string+ currentSDL string+ previousSDL string+ }{+ {+ name: "simple_constraint_deletion_test",+ description: "Test constraint deletion logic with a simple table",+ currentSDL: `CREATE TABLE "test"."simple_table" (+ "id" integer NOT NULL,+ "name" text NOT NULL+);`,+ previousSDL: `CREATE TABLE "test"."simple_table" (+ "id" integer NOT NULL,+ "name" text NOT NULL,+ CONSTRAINT "simple_table_pkey" PRIMARY KEY (id)+);`,+ },+ {+ name: "simple_column_deletion_test",+ description: "Test column deletion logic when deleting the last column",+ currentSDL: `CREATE TABLE "test"."simple_table" (+ "id" integer NOT NULL+);`,+ previousSDL: `CREATE TABLE "test"."simple_table" (+ "id" integer NOT NULL,+ "name" text NOT NULL+);`,+ },+ }++ for _, tc := range testCases {+ t.Run(tc.name, func(t *testing.T) {+ t.Logf("Test description: %s", tc.description)++ // Parse current and previous SDL+ currentChunks, err := ChunkSDLText(tc.currentSDL)+ require.NoError(t, err)++ previousChunks, err := ChunkSDLText(tc.previousSDL)+ require.NoError(t, err)++ // Log original chunks+ t.Log("=== Current SDL Chunks ===")+ for id, chunk := range currentChunks.Tables {+ t.Logf("Table %s: %s", id, chunk.GetText())+ }+ t.Log("=== Previous SDL Chunks ===")+ for id, chunk := range previousChunks.Tables {+ t.Logf("Table %s: %s", id, chunk.GetText())+ }++ // Create mock schemas for applyMinimalChangesToChunks based on test case+ var currentSchema, previousSchema *model.DatabaseSchema++ switch tc.name {+ case "simple_constraint_deletion_test":+ currentSchema = createMockDatabaseSchema() // No constraints+ previousSchema = createMockDatabaseSchemaWithoutTestColumn() // Has primary key constraint+ case "simple_column_deletion_test":+ currentSchema = createMockDatabaseSchemaForColumnDeletion() // Only has id column+ previousSchema = createMockDatabaseSchema() // Has id and name columns+ default:+ currentSchema = createMockDatabaseSchema()+ previousSchema = createMockDatabaseSchemaWithoutTestColumn()+ }++ // Apply minimal changes - this should test both the chunk mapping logic and rewriter fix+ err = applyMinimalChangesToChunks(previousChunks, currentSchema, previousSchema)+ require.NoError(t, err)++ // Log chunks after minimal changes+ t.Log("=== Previous SDL Chunks After Minimal Changes ===")+ for id, chunk := range previousChunks.Tables {+ t.Logf("Table %s: %s", id, chunk.GetText())+ }++ // Verify that each chunk identifier matches its content+ for identifier, chunk := range previousChunks.Tables {+ chunkText := chunk.GetText()+ t.Logf("Checking identifier '%s'", identifier)++ // The chunk text should contain the correct table name based on the identifier+ expectedTableName := identifier+ if strings.Contains(expectedTableName, ".") {+ parts := strings.Split(expectedTableName, ".")+ expectedTableName = parts[len(parts)-1] // Get table name part+ }++ // Check that the chunk text contains the expected table name+ assert.Contains(t, chunkText, expectedTableName,+ "Chunk with identifier '%s' should contain table name '%s' but got: %s",+ identifier, expectedTableName, chunkText)++ // More specific check: chunk should contain CREATE TABLE with the correct qualified name+ // PostgreSQL can format identifiers in different ways: "schema.table", schema.table, "schema"."table"+ parts := strings.Split(identifier, ".")+ if len(parts) == 2 {+ schema, table := parts[0], parts[1]++ // Check various valid formats+ formats := []string{+ fmt.Sprintf(`CREATE TABLE "%s"."%s"`, schema, table), // "schema"."table"+ fmt.Sprintf(`CREATE TABLE "%s"`, identifier), // "schema.table"+ fmt.Sprintf(`CREATE TABLE %s`, identifier), // schema.table+ fmt.Sprintf(`CREATE TABLE %s.%s`, schema, table), // schema.table+ }++ hasValidFormat := false+ for _, format := range formats {+ if strings.Contains(chunkText, format) {+ hasValidFormat = true+ break+ }+ }++ assert.True(t, hasValidFormat,+ "Chunk with identifier '%s' should contain CREATE TABLE in a valid format but got: %s",+ identifier, chunkText)+ }+ }+ })+ }+}++// TestProcessCheckConstraintChanges tests check constraint modify/create/drop logic+func TestProcessCheckConstraintChanges(t *testing.T) {+ tests := []struct {+ name string+ currentSDL string+ previousSDL string+ expectedDrops int+ expectedCreates int+ expectedModifies int // modify = drop + create+ description string+ }{+ {+ name: "create_new_check_constraint",+ currentSDL: `CREATE TABLE "test"."users" (+ "id" integer NOT NULL,+ "age" integer,+ CONSTRAINT "users_age_check" CHECK (age > 0)+);`,+ previousSDL: `CREATE TABLE "test"."users" (+ "id" integer NOT NULL,+ "age" integer+);`,+ expectedDrops: 0,+ expectedCreates: 1,+ expectedModifies: 0,+ description: "Adding a new check constraint should create one CREATE operation",+ },+ {+ name: "drop_check_constraint",+ currentSDL: `CREATE TABLE "test"."users" (+ "id" integer NOT NULL,+ "age" integer+);`,+ previousSDL: `CREATE TABLE "test"."users" (+ "id" integer NOT NULL,+ "age" integer,+ CONSTRAINT "users_age_check" CHECK (age > 0)+);`,+ expectedDrops: 1,+ expectedCreates: 0,+ expectedModifies: 0,+ description: "Removing a check constraint should create one DROP operation",+ },+ {+ name: "modify_check_constraint",+ currentSDL: `CREATE TABLE "test"."users" (+ "id" integer NOT NULL,+ "age" integer,+ CONSTRAINT "users_age_check" CHECK (age >= 18)+);`,+ previousSDL: `CREATE TABLE "test"."users" (+ "id" integer NOT NULL,+ "age" integer,+ CONSTRAINT "users_age_check" CHECK (age > 0)+);`,+ expectedDrops: 1,+ expectedCreates: 1,+ expectedModifies: 1,+ description: "Modifying a check constraint should create one DROP and one CREATE operation",+ },+ {+ name: "multiple_constraint_operations",+ currentSDL: `CREATE TABLE "test"."users" (+ "id" integer NOT NULL,+ "age" integer,+ "name" text,+ CONSTRAINT "users_age_check" CHECK (age >= 21),+ CONSTRAINT "users_name_check" CHECK (length(name) > 0)+);`,+ previousSDL: `CREATE TABLE "test"."users" (+ "id" integer NOT NULL,+ "age" integer,+ "email" text,+ CONSTRAINT "users_age_check" CHECK (age > 0),+ CONSTRAINT "users_email_check" CHECK (email LIKE '%@%')+);`,+ expectedDrops: 2, // drop old age_check + drop email_check+ expectedCreates: 2, // create new age_check + create name_check+ expectedModifies: 1, // modify age_check+ description: "Multiple operations: modify age_check, drop email_check, create name_check",+ },+ {+ name: "no_changes",+ currentSDL: `CREATE TABLE "test"."users" (+ "id" integer NOT NULL,+ "age" integer,+ CONSTRAINT "users_age_check" CHECK (age > 0)+);`,+ previousSDL: `CREATE TABLE "test"."users" (+ "id" integer NOT NULL,+ "age" integer,+ CONSTRAINT "users_age_check" CHECK (age > 0)+);`,+ expectedDrops: 0,+ expectedCreates: 0,+ expectedModifies: 0,+ description: "No changes should result in no operations",+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ t.Logf("Test description: %s", tt.description)++ // Parse current and previous SDL+ currentChunks, err := ChunkSDLText(tt.currentSDL)+ require.NoError(t, err)+ previousChunks, err := ChunkSDLText(tt.previousSDL)+ require.NoError(t, err)++ // Get table chunks+ require.Len(t, currentChunks.Tables, 1, "Should have exactly one current table")+ require.Len(t, previousChunks.Tables, 1, "Should have exactly one previous table")++ var currentChunk, previousChunk *schema.SDLChunk+ for _, chunk := range currentChunks.Tables {+ currentChunk = chunk+ break+ }+ for _, chunk := range previousChunks.Tables {+ previousChunk = chunk+ break+ }++ // Extract AST nodes+ currentAST, ok := currentChunk.ASTNode.(*parser.CreatestmtContext)+ require.True(t, ok, "Current chunk should be a CREATE TABLE statement")+ previousAST, ok := previousChunk.ASTNode.(*parser.CreatestmtContext)+ require.True(t, ok, "Previous chunk should be a CREATE TABLE statement")++ // Process check constraint changes (no usability chunks needed for this test)+ checkDiffs := processCheckConstraintChanges(previousAST, currentAST, nil, "test.users")++ // Count operations+ actualDrops := 0+ actualCreates := 0+ for _, diff := range checkDiffs {+ switch diff.Action {+ case schema.MetadataDiffActionDrop:+ actualDrops+++ case schema.MetadataDiffActionCreate:+ actualCreates+++ default:+ // Unknown action - should not happen in normal cases+ }+ }++ // Verify results+ assert.Equal(t, tt.expectedDrops, actualDrops, "Number of DROP operations should match")+ assert.Equal(t, tt.expectedCreates, actualCreates, "Number of CREATE operations should match")++ // Log operations for debugging+ t.Logf("Generated %d DROP and %d CREATE operations:", actualDrops, actualCreates)+ for i, diff := range checkDiffs {+ t.Logf(" %d. Action: %s", i+1, diff.Action)+ }+ })+ }+}++// TestProcessForeignKeyChanges tests foreign key constraint modify/create/drop logic+func TestProcessForeignKeyChanges(t *testing.T) {+ tests := []struct {+ name string+ currentSDL string+ previousSDL string+ expectedDrops int+ expectedCreates int+ description string+ }{+ {+ name: "create_new_foreign_key",+ currentSDL: `CREATE TABLE "test"."orders" (+ "id" integer NOT NULL,+ "user_id" integer,+ CONSTRAINT "orders_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id)+);`,+ previousSDL: `CREATE TABLE "test"."orders" (+ "id" integer NOT NULL,+ "user_id" integer+);`,+ expectedDrops: 0,+ expectedCreates: 1,+ description: "Adding a new foreign key should create one CREATE operation",+ },+ {+ name: "drop_foreign_key",+ currentSDL: `CREATE TABLE "test"."orders" (+ "id" integer NOT NULL,+ "user_id" integer+);`,+ previousSDL: `CREATE TABLE "test"."orders" (+ "id" integer NOT NULL,+ "user_id" integer,+ CONSTRAINT "orders_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id)+);`,+ expectedDrops: 1,+ expectedCreates: 0,+ description: "Removing a foreign key should create one DROP operation",+ },+ {+ name: "modify_foreign_key",+ currentSDL: `CREATE TABLE "test"."orders" (+ "id" integer NOT NULL,+ "user_id" integer,+ CONSTRAINT "orders_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE+);`,+ previousSDL: `CREATE TABLE "test"."orders" (+ "id" integer NOT NULL,+ "user_id" integer,+ CONSTRAINT "orders_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id)+);`,+ expectedDrops: 1,+ expectedCreates: 1,+ description: "Modifying a foreign key should create one DROP and one CREATE operation",+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ t.Logf("Test description: %s", tt.description)++ // Parse current and previous SDL+ currentChunks, err := ChunkSDLText(tt.currentSDL)+ require.NoError(t, err)+ previousChunks, err := ChunkSDLText(tt.previousSDL)+ require.NoError(t, err)++ // Get table chunks+ require.Len(t, currentChunks.Tables, 1, "Should have exactly one current table")+ require.Len(t, previousChunks.Tables, 1, "Should have exactly one previous table")++ var currentChunk, previousChunk *schema.SDLChunk+ for _, chunk := range currentChunks.Tables {+ currentChunk = chunk+ break+ }+ for _, chunk := range previousChunks.Tables {+ previousChunk = chunk+ break+ }++ // Extract AST nodes+ currentAST, ok := currentChunk.ASTNode.(*parser.CreatestmtContext)+ require.True(t, ok, "Current chunk should be a CREATE TABLE statement")+ previousAST, ok := previousChunk.ASTNode.(*parser.CreatestmtContext)+ require.True(t, ok, "Previous chunk should be a CREATE TABLE statement")++ // Process foreign key changes+ fkDiffs := processForeignKeyChanges(previousAST, currentAST, nil, "test.orders")++ // Count operations+ actualDrops := 0+ actualCreates := 0+ for _, diff := range fkDiffs {+ switch diff.Action {+ case schema.MetadataDiffActionDrop:+ actualDrops+++ case schema.MetadataDiffActionCreate:+ actualCreates+++ default:+ // Unknown action - should not happen in normal cases+ }+ }++ // Verify results+ assert.Equal(t, tt.expectedDrops, actualDrops, "Number of DROP operations should match")+ assert.Equal(t, tt.expectedCreates, actualCreates, "Number of CREATE operations should match")++ // Log operations for debugging+ t.Logf("Generated %d DROP and %d CREATE operations:", actualDrops, actualCreates)+ for i, diff := range fkDiffs {+ t.Logf(" %d. Action: %s", i+1, diff.Action)+ }+ })+ }+}++// TestProcessUniqueConstraintChanges tests unique constraint modify/create/drop logic+func TestProcessUniqueConstraintChanges(t *testing.T) {+ tests := []struct {+ name string+ currentSDL string+ previousSDL string+ expectedDrops int+ expectedCreates int+ description string+ }{+ {+ name: "create_new_unique_constraint",+ currentSDL: `CREATE TABLE "test"."users" (+ "id" integer NOT NULL,+ "email" text,+ CONSTRAINT "users_email_key" UNIQUE (email)+);`,+ previousSDL: `CREATE TABLE "test"."users" (+ "id" integer NOT NULL,+ "email" text+);`,+ expectedDrops: 0,+ expectedCreates: 1,+ description: "Adding a new unique constraint should create one CREATE operation",+ },+ {+ name: "drop_unique_constraint",+ currentSDL: `CREATE TABLE "test"."users" (+ "id" integer NOT NULL,+ "email" text+);`,+ previousSDL: `CREATE TABLE "test"."users" (+ "id" integer NOT NULL,+ "email" text,+ CONSTRAINT "users_email_key" UNIQUE (email)+);`,+ expectedDrops: 1,+ expectedCreates: 0,+ description: "Removing a unique constraint should create one DROP operation",+ },+ {+ name: "modify_unique_constraint",+ currentSDL: `CREATE TABLE "test"."users" (+ "id" integer NOT NULL,+ "email" text,+ "username" text,+ CONSTRAINT "users_email_username_key" UNIQUE (email, username)+);`,+ previousSDL: `CREATE TABLE "test"."users" (+ "id" integer NOT NULL,+ "email" text,+ "username" text,+ CONSTRAINT "users_email_username_key" UNIQUE (email)+);`,+ expectedDrops: 1,+ expectedCreates: 1,+ description: "Modifying a unique constraint should create one DROP and one CREATE operation",+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ t.Logf("Test description: %s", tt.description)++ // Parse current and previous SDL+ currentChunks, err := ChunkSDLText(tt.currentSDL)+ require.NoError(t, err)+ previousChunks, err := ChunkSDLText(tt.previousSDL)+ require.NoError(t, err)++ // Get table chunks+ require.Len(t, currentChunks.Tables, 1, "Should have exactly one current table")+ require.Len(t, previousChunks.Tables, 1, "Should have exactly one previous table")++ var currentChunk, previousChunk *schema.SDLChunk+ for _, chunk := range currentChunks.Tables {+ currentChunk = chunk+ break+ }+ for _, chunk := range previousChunks.Tables {+ previousChunk = chunk+ break+ }++ // Extract AST nodes+ currentAST, ok := currentChunk.ASTNode.(*parser.CreatestmtContext)+ require.True(t, ok, "Current chunk should be a CREATE TABLE statement")+ previousAST, ok := previousChunk.ASTNode.(*parser.CreatestmtContext)+ require.True(t, ok, "Previous chunk should be a CREATE TABLE statement")++ // Process unique constraint changes+ ukDiffs := processUniqueConstraintChanges(previousAST, currentAST, nil, "test.users")++ // Count operations+ actualDrops := 0+ actualCreates := 0+ for _, diff := range ukDiffs {+ switch diff.Action {+ case schema.MetadataDiffActionDrop:+ actualDrops+++ case schema.MetadataDiffActionCreate:+ actualCreates+++ default:+ // Unknown action - should not happen in normal cases+ }+ }++ // Verify results+ assert.Equal(t, tt.expectedDrops, actualDrops, "Number of DROP operations should match")+ assert.Equal(t, tt.expectedCreates, actualCreates, "Number of CREATE operations should match")++ // Log operations for debugging+ t.Logf("Generated %d DROP and %d CREATE operations:", actualDrops, actualCreates)+ for i, diff := range ukDiffs {+ t.Logf(" %d. Action: %s", i+1, diff.Action)+ }+ })+ }+}++func createMockDatabaseSchema() *model.DatabaseSchema {+ metadata := &storepb.DatabaseSchemaMetadata{+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "test",+ Tables: []*storepb.TableMetadata{+ {+ Name: "simple_table",+ Columns: []*storepb.ColumnMetadata{+ {Name: "id", Type: "integer", Nullable: false},+ {Name: "name", Type: "text", Nullable: false},+ },+ // No constraints in current schema+ },+ },+ },+ },+ }++ return model.NewDatabaseSchema(metadata, nil, nil, storepb.Engine_POSTGRES, false)+}++func createMockDatabaseSchemaWithoutTestColumn() *model.DatabaseSchema {+ metadata := &storepb.DatabaseSchemaMetadata{+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "test",+ Tables: []*storepb.TableMetadata{+ {+ Name: "simple_table",+ Columns: []*storepb.ColumnMetadata{+ {Name: "id", Type: "integer", Nullable: false},+ {Name: "name", Type: "text", Nullable: false},+ },+ Indexes: []*storepb.IndexMetadata{+ {+ Name: "simple_table_pkey",+ Primary: true,+ Unique: true,+ IsConstraint: true,+ Expressions: []string{"id"},+ },+ },+ },+ },+ },+ },+ }++ return model.NewDatabaseSchema(metadata, nil, nil, storepb.Engine_POSTGRES, false)+}++func createMockDatabaseSchemaForColumnDeletion() *model.DatabaseSchema {+ // Create schema for column deletion test - only has id column (name column deleted)+ metadata := &storepb.DatabaseSchemaMetadata{+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Tables: []*storepb.TableMetadata{+ {+ Name: "employees",+ Columns: []*storepb.ColumnMetadata{+ {+ Name: "id",+ Type: "integer",+ Nullable: false,+ },+ // Only id column - this represents the state after name column deletion+ },+ },+ },+ },+ },+ }++ return model.NewDatabaseSchema(metadata, nil, nil, storepb.Engine_POSTGRES, false)+}