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: handle drift for comment and sequence owner (#17809)
rebelice5a371b178197
diff --git a/backend/plugin/schema/pg/get_sdl_diff.go b/backend/plugin/schema/pg/get_sdl_diff.goindex 3ce4aff317..bf61396789 100644--- a/backend/plugin/schema/pg/get_sdl_diff.go+++ b/backend/plugin/schema/pg/get_sdl_diff.go@@ -2137,6 +2137,18 @@ func applyMinimalChangesToChunks(previousChunks *schema.SDLChunks, currentSchemareturn errors.Wrap(err, "failed to apply sequence changes")}+ // Process view changes: apply minimal changes to view chunks+ err = applyViewChangesToChunks(previousChunks, currentSchema, previousSchema)+ if err != nil {+ return errors.Wrap(err, "failed to apply view changes")+ }++ // Process column comment changes: sync column comments based on metadata+ err = applyColumnCommentChanges(previousChunks, currentSchema, previousSchema)+ if err != nil {+ return errors.Wrap(err, "failed to apply column comment changes")+ }+return nil}@@ -2217,6 +2229,14 @@ func applyTableChangesToChunk(chunk *schema.SDLChunk, currentTable, previousTabl// Update the chunk with the new AST nodechunk.ASTNode = newCreateTableNode+ // Synchronize COMMENT ON TABLE statements only if comment has changed+ if currentTable.Comment != previousTable.Comment {+ schemaName, tableName := parseIdentifier(chunk.Identifier)+ if err := syncObjectCommentStatements(chunk, currentTable.Comment, "TABLE", schemaName, tableName); err != nil {+ return errors.Wrapf(err, "failed to sync COMMENT statements for table %s", chunk.Identifier)+ }+ }+return nil}@@ -3452,13 +3472,24 @@ func applyStandaloneIndexChangesToChunks(previousChunks *schema.SDLChunks, curre}}- // Process index modifications: update existing chunks+ // Process index modifications: update existing chunks or create if chunk doesn't existfor indexKey, currentIndex := range currentIndexes {if previousIndex, exists := previousIndexes[indexKey]; exists {- // Index exists in both - check if it needs modification- err := updateIndexChunkIfNeeded(previousChunks, currentIndex, previousIndex, indexKey)- if err != nil {- return errors.Wrapf(err, "failed to update index chunk for %s", indexKey)+ // Index exists in both metadata+ // Check if chunk exists in SDL - if not, create it; otherwise update it+ if _, chunkExists := previousChunks.Indexes[indexKey]; chunkExists {+ // Chunk exists - update if needed+ err := updateIndexChunkIfNeeded(previousChunks, currentIndex, previousIndex, indexKey)+ if err != nil {+ return errors.Wrapf(err, "failed to update index chunk for %s", indexKey)+ }+ } else {+ // Chunk doesn't exist in SDL but index exists in database+ // Create chunk to add it to SDL+ err := createIndexChunk(previousChunks, currentIndex, indexKey)+ if err != nil {+ return errors.Wrapf(err, "failed to create index chunk for %s", indexKey)+ }}}}@@ -3530,32 +3561,63 @@ func updateIndexChunkIfNeeded(chunks *schema.SDLChunks, currentIndex, previousInreturn nil}- // Check if the index definition has changed- if !indexDefinitionsEqual(currentIndex.IndexMetadata, previousIndex.IndexMetadata) {- // Index has changed - regenerate the chunk- err := createIndexChunk(chunks, currentIndex, indexKey)+ // Get the existing chunk+ chunk, exists := chunks.Indexes[indexKey]+ if !exists {+ return errors.Errorf("index chunk not found for key %s", indexKey)+ }++ // Check if the CREATE INDEX definition has changed (excluding comment)+ definitionChanged := !indexDefinitionsEqualExcludingComment(currentIndex.IndexMetadata, previousIndex.IndexMetadata)++ if definitionChanged {+ // Index definition has changed - regenerate the CREATE INDEX chunk+ indexSDL := generateCreateIndexSDL(currentIndex)+ if indexSDL == "" {+ return errors.New("failed to generate SDL for index")+ }++ // Parse the SDL to get AST node+ parseResult, err := pgparser.ParsePostgreSQL(indexSDL)if err != nil {- return errors.Wrapf(err, "failed to recreate index chunk for %s", indexKey)+ return errors.Wrapf(err, "failed to parse generated index SDL: %s", indexSDL)}- }- return nil-}+ // Extract the CREATE INDEX AST node+ var indexASTNode *parser.IndexstmtContext+ antlr.ParseTreeWalkerDefault.Walk(&indexExtractor{+ result: &indexASTNode,+ }, parseResult.Tree)-// deleteIndexChunk removes an index chunk from the chunks-func deleteIndexChunk(chunks *schema.SDLChunks, indexKey string) {- if chunks != nil && chunks.Indexes != nil {- delete(chunks.Indexes, indexKey)+ if indexASTNode == nil {+ return errors.New("failed to extract CREATE INDEX AST node")+ }++ // Update the CREATE INDEX AST node+ chunk.ASTNode = indexASTNode}++ // Synchronize COMMENT ON INDEX statements only if comment has changed+ if currentIndex.Comment != previousIndex.Comment {+ schemaName := currentIndex.SchemaName+ if schemaName == "" {+ schemaName = "public"+ }+ if err := syncObjectCommentStatements(chunk, currentIndex.Comment, "INDEX", schemaName, currentIndex.Name); err != nil {+ return errors.Wrapf(err, "failed to sync COMMENT statements for index %s", indexKey)+ }+ }++ return nil}-// indexDefinitionsEqual compares two index definitions to see if they are equivalent-func indexDefinitionsEqual(index1, index2 *storepb.IndexMetadata) bool {+// indexDefinitionsEqualExcludingComment compares two index definitions excluding comments+func indexDefinitionsEqualExcludingComment(index1, index2 *storepb.IndexMetadata) bool {if index1 == nil || index2 == nil {return false}- // Compare basic properties+ // Compare basic properties (excluding comment)if index1.Name != index2.Name ||index1.Unique != index2.Unique ||index1.Type != index2.Type ||@@ -3578,9 +3640,27 @@ func indexDefinitionsEqual(index1, index2 *storepb.IndexMetadata) bool {}}- // Note: PostgreSQL IndexMetadata doesn't have a Where field in the current schema- // Index WHERE clauses are handled differently in the system- // For now, we'll consider indexes equal if all other fields match+ return true+}++// deleteIndexChunk removes an index chunk from the chunks+func deleteIndexChunk(chunks *schema.SDLChunks, indexKey string) {+ if chunks != nil && chunks.Indexes != nil {+ delete(chunks.Indexes, indexKey)+ }+}++// indexDefinitionsEqual compares two index definitions to see if they are equivalent+func indexDefinitionsEqual(index1, index2 *storepb.IndexMetadata) bool {+ // First check everything except comment+ if !indexDefinitionsEqualExcludingComment(index1, index2) {+ return false+ }++ // Then check comment+ if index1.Comment != index2.Comment {+ return false+ }return true}@@ -3662,13 +3742,24 @@ func applyFunctionChangesToChunks(previousChunks *schema.SDLChunks, currentSchem}}- // Process function modifications: update existing chunks+ // Process function modifications: update existing chunks or create if chunk doesn't existfor functionKey, currentFunction := range currentFunctions {if previousFunction, exists := previousFunctions[functionKey]; exists {- // Function exists in both - check if it needs modification- err := updateFunctionChunkIfNeeded(previousChunks, currentFunction, previousFunction, functionKey)- if err != nil {- return errors.Wrapf(err, "failed to update function chunk for %s", functionKey)+ // Function exists in both metadata+ // Check if chunk exists in SDL - if not, create it; otherwise update it+ if _, chunkExists := previousChunks.Functions[functionKey]; chunkExists {+ // Chunk exists - update if needed+ err := updateFunctionChunkIfNeeded(previousChunks, currentFunction, previousFunction, functionKey)+ if err != nil {+ return errors.Wrapf(err, "failed to update function chunk for %s", functionKey)+ }+ } else {+ // Chunk doesn't exist in SDL but function exists in database+ // Create chunk to add it to SDL+ err := createFunctionChunk(previousChunks, currentFunction, functionKey)+ if err != nil {+ return errors.Wrapf(err, "failed to create function chunk for %s", functionKey)+ }}}}@@ -3805,51 +3896,45 @@ func updateFunctionChunkIfNeeded(chunks *schema.SDLChunks, currentFunction, prevreturn nil}- // Check if function definitions are different- if !functionDefinitionsEqual(currentFunction, previousFunction) {- // Function was modified - update the chunk- err := updateFunctionChunk(chunks, currentFunction, functionKey)- if err != nil {- return errors.Wrapf(err, "failed to update function chunk for %s", functionKey)- }+ // Get the existing chunk+ chunk, exists := chunks.Functions[functionKey]+ if !exists {+ return errors.Errorf("function chunk not found for key %s", functionKey)}- return nil-}+ // Check if the CREATE FUNCTION definition has changed (excluding comment)+ definitionChanged := !functionDefinitionsEqualExcludingComment(currentFunction, previousFunction)-// updateFunctionChunk updates an existing function chunk with new definition-func updateFunctionChunk(chunks *schema.SDLChunks, function *storepb.FunctionMetadata, functionKey string) error {- if function == nil || chunks == nil {- return nil- }+ if definitionChanged {+ // Function definition has changed - regenerate the CREATE FUNCTION chunk+ sdl := generateCreateFunctionSDL(currentFunction)+ if sdl == "" {+ return errors.Errorf("failed to generate SDL for function %s", functionKey)+ }- // Generate new function SDL- sdl := generateCreateFunctionSDL(function)- if sdl == "" {- return errors.Errorf("failed to generate SDL for function %s", functionKey)- }+ // Parse the SDL to get the AST node+ astNode, err := extractFunctionASTFromSDL(sdl)+ if err != nil {+ return errors.Wrapf(err, "failed to extract AST from generated function SDL for %s", functionKey)+ }- // Parse the SDL to get the AST node- astNode, err := extractFunctionASTFromSDL(sdl)- if err != nil {- return errors.Wrapf(err, "failed to extract AST from generated function SDL for %s", functionKey)+ // Update the CREATE FUNCTION AST node+ chunk.ASTNode = astNode}- // Update the existing chunk- if chunk := chunks.Functions[functionKey]; chunk != nil {- chunk.ASTNode = astNode- } else {- // Create new chunk if it doesn't exist- chunk := &schema.SDLChunk{- Identifier: functionKey,- ASTNode: astNode,+ // Synchronize COMMENT ON FUNCTION statements only if comment has changed+ if currentFunction.Comment != previousFunction.Comment {+ schemaName, functionName := parseIdentifier(functionKey)+ if err := syncObjectCommentStatements(chunk, currentFunction.Comment, "FUNCTION", schemaName, functionName); err != nil {+ return errors.Wrapf(err, "failed to sync COMMENT statements for function %s", functionKey)}- chunks.Functions[functionKey] = chunk}return nil}+// updateFunctionChunk updates an existing function chunk with new definition+// This function synchronizes the CREATE FUNCTION and COMMENT ON FUNCTION statements// deleteFunctionChunk removes a function chunk from the chunksfunc deleteFunctionChunk(chunks *schema.SDLChunks, functionKey string) {if chunks != nil && chunks.Functions != nil {@@ -3895,6 +3980,21 @@ func extractFunctionTextFromAST(functionAST *parser.CreatefunctionstmtContext) s// functionDefinitionsEqual compares two function definitions to see if they are equivalentfunc functionDefinitionsEqual(function1, function2 *storepb.FunctionMetadata) bool {+ // First check everything except comment+ if !functionDefinitionsEqualExcludingComment(function1, function2) {+ return false+ }++ // Then check comment+ if function1.Comment != function2.Comment {+ return false+ }++ return true+}++// functionDefinitionsEqualExcludingComment compares two function definitions excluding comments+func functionDefinitionsEqualExcludingComment(function1, function2 *storepb.FunctionMetadata) bool {if function1 == nil && function2 == nil {return true}@@ -3993,6 +4093,10 @@ func applySequenceChangesToChunks(previousChunks *schema.SDLChunks, currentSchem// Process sequence additions: create new sequence chunksfor sequenceKey, currentSequence := range currentSequences {if _, exists := previousSequences[sequenceKey]; !exists {+ // Skip sequences owned by tables (e.g., SERIAL columns) - they should be managed by their owner tables+ if currentSequence.OwnerTable != "" {+ continue+ }// New sequence - create a chunk for iterr := createSequenceChunk(previousChunks, currentSequence, sequenceKey)if err != nil {@@ -4001,13 +4105,29 @@ func applySequenceChangesToChunks(previousChunks *schema.SDLChunks, currentSchem}}- // Process sequence modifications: update existing chunks+ // Process sequence modifications: update existing chunks or create if chunk doesn't existfor sequenceKey, currentSequence := range currentSequences {if previousSequence, exists := previousSequences[sequenceKey]; exists {- // Sequence exists in both - check if it needs modification- err := updateSequenceChunkIfNeeded(previousChunks, currentSequence, previousSequence, sequenceKey)- if err != nil {- return errors.Wrapf(err, "failed to update sequence chunk for %s", sequenceKey)+ // Sequence exists in both metadata+ // Check if chunk exists in SDL - if not, create it; otherwise update it+ if _, chunkExists := previousChunks.Sequences[sequenceKey]; chunkExists {+ // Chunk exists - update if needed+ err := updateSequenceChunkIfNeeded(previousChunks, currentSequence, previousSequence, sequenceKey)+ if err != nil {+ return errors.Wrapf(err, "failed to update sequence chunk for %s", sequenceKey)+ }+ } else {+ // Chunk doesn't exist in SDL but sequence exists in database+ // Skip sequences owned by tables (e.g., SERIAL columns) - they should be managed by their owner tables+ // We only skip if the chunk doesn't exist in SDL, meaning it's not explicitly defined by the user+ if currentSequence.OwnerTable != "" {+ continue+ }+ // Create chunk to add it to SDL+ err := createSequenceChunk(previousChunks, currentSequence, sequenceKey)+ if err != nil {+ return errors.Wrapf(err, "failed to create sequence chunk for %s", sequenceKey)+ }}}}@@ -4080,12 +4200,48 @@ func updateSequenceChunkIfNeeded(chunks *schema.SDLChunks, currentSequence, prevreturn nil}- // Check if sequence definitions are different- if !sequenceDefinitionsEqual(currentSequence, previousSequence) {- // Sequence was modified - update the chunk- err := updateSequenceChunk(chunks, currentSequence, sequenceKey)+ // Get the existing chunk+ chunk, exists := chunks.Sequences[sequenceKey]+ if !exists {+ return errors.Errorf("sequence chunk not found for key %s", sequenceKey)+ }++ // Check if the CREATE SEQUENCE definition has changed (excluding comment and owner)+ definitionChanged := !sequenceDefinitionsEqualExcludingCommentAndOwner(currentSequence, previousSequence)++ if definitionChanged {+ // Sequence definition has changed - regenerate the CREATE SEQUENCE chunk+ schemaName := extractSchemaFromSequenceKey(sequenceKey)+ sdl := generateCreateSequenceSDL(schemaName, currentSequence)+ if sdl == "" {+ return errors.Errorf("failed to generate SDL for sequence %s", sequenceKey)+ }++ // Parse the SDL to get the AST node for CREATE SEQUENCE+ astNode, err := extractSequenceASTFromSDL(sdl)if err != nil {- return errors.Wrapf(err, "failed to update sequence chunk for %s", sequenceKey)+ return errors.Wrapf(err, "failed to extract AST from generated sequence SDL for %s", sequenceKey)+ }++ // Update the CREATE SEQUENCE AST node+ chunk.ASTNode = astNode+ }++ schemaName := extractSchemaFromSequenceKey(sequenceKey)++ // Synchronize ALTER SEQUENCE OWNED BY statements only if owner has changed+ ownerChanged := currentSequence.OwnerTable != previousSequence.OwnerTable ||+ currentSequence.OwnerColumn != previousSequence.OwnerColumn+ if ownerChanged {+ if err := syncAlterSequenceStatements(chunk, currentSequence, schemaName); err != nil {+ return errors.Wrapf(err, "failed to sync ALTER statements for sequence %s", sequenceKey)+ }+ }++ // Synchronize COMMENT ON SEQUENCE statements only if comment has changed+ if currentSequence.Comment != previousSequence.Comment {+ if err := syncCommentStatements(chunk, currentSequence, schemaName); err != nil {+ return errors.Wrapf(err, "failed to sync COMMENT statements for sequence %s", sequenceKey)}}@@ -4093,39 +4249,165 @@ func updateSequenceChunkIfNeeded(chunks *schema.SDLChunks, currentSequence, prev}// updateSequenceChunk updates an existing sequence chunk with new definition-func updateSequenceChunk(chunks *schema.SDLChunks, sequence *storepb.SequenceMetadata, sequenceKey string) error {- if sequence == nil || chunks == nil {+// This function synchronizes the CREATE SEQUENCE, ALTER SEQUENCE, and COMMENT ON SEQUENCE statements+// based on the current sequence metadata from the database+// syncAlterSequenceStatements synchronizes ALTER SEQUENCE OWNED BY statements in the chunk+// based on the current OwnerTable and OwnerColumn from sequence metadata+func syncAlterSequenceStatements(chunk *schema.SDLChunk, sequence *storepb.SequenceMetadata, schemaName string) error {+ if chunk == nil || sequence == nil {return nil}- // Generate new sequence SDL- schemaName := extractSchemaFromSequenceKey(sequenceKey)- sdl := generateCreateSequenceSDL(schemaName, sequence)- if sdl == "" {- return errors.Errorf("failed to generate SDL for sequence %s", sequenceKey)+ // Check if sequence has an owner+ hasOwner := sequence.OwnerTable != "" && sequence.OwnerColumn != ""++ if hasOwner {+ // Generate ALTER SEQUENCE OWNED BY statement+ alterSDL := fmt.Sprintf("ALTER SEQUENCE \"%s\".\"%s\" OWNED BY \"%s\".\"%s\";",+ schemaName, sequence.Name, sequence.OwnerTable, sequence.OwnerColumn)++ // Parse to get AST node+ alterNode, err := extractAlterSequenceASTFromSDL(alterSDL)+ if err != nil {+ return errors.Wrapf(err, "failed to parse ALTER SEQUENCE statement")+ }++ // Replace all existing ALTER statements with the new one+ // (There should only be one ALTER SEQUENCE OWNED BY statement per sequence)+ chunk.AlterStatements = []antlr.ParserRuleContext{alterNode}+ } else {+ // No owner - remove all ALTER SEQUENCE statements+ chunk.AlterStatements = nil}- // Parse the SDL to get the AST node- astNode, err := extractSequenceASTFromSDL(sdl)- if err != nil {- return errors.Wrapf(err, "failed to extract AST from generated sequence SDL for %s", sequenceKey)+ return nil+}++// syncCommentStatements synchronizes COMMENT ON SEQUENCE statements in the chunk+// based on the current Comment field from sequence metadata+func syncCommentStatements(chunk *schema.SDLChunk, sequence *storepb.SequenceMetadata, schemaName string) error {+ if chunk == nil || sequence == nil {+ return nil}- // Update the existing chunk- if chunk := chunks.Sequences[sequenceKey]; chunk != nil {- chunk.ASTNode = astNode- } else {- // Create new chunk if it doesn't exist- chunk := &schema.SDLChunk{- Identifier: sequenceKey,- ASTNode: astNode,+ // Use the generic comment sync function+ return syncObjectCommentStatements(chunk, sequence.Comment, "SEQUENCE", schemaName, sequence.Name)+}++// syncObjectCommentStatements is a generic function to synchronize COMMENT statements for any object type+// objectType should be "SEQUENCE", "TABLE", "VIEW", "FUNCTION", "INDEX", etc.+func syncObjectCommentStatements(chunk *schema.SDLChunk, comment, objectType, schemaName, objectName string) error {+ if chunk == nil {+ return nil+ }++ // Check if object has a comment+ hasComment := comment != ""++ if hasComment {+ // Generate COMMENT ON <objectType> statement+ // Escape single quotes in comment text+ escapedComment := strings.ReplaceAll(comment, "'", "''")+ commentSDL := fmt.Sprintf("COMMENT ON %s \"%s\".\"%s\" IS '%s';",+ objectType, schemaName, objectName, escapedComment)++ // Parse to get AST node+ commentNode, err := extractCommentASTFromSDL(commentSDL)+ if err != nil {+ return errors.Wrapf(err, "failed to parse COMMENT ON %s statement", objectType)}- chunks.Sequences[sequenceKey] = chunk++ // Replace all existing COMMENT statements with the new one+ // (There should only be one COMMENT ON statement per object)+ chunk.CommentStatements = []antlr.ParserRuleContext{commentNode}+ } else {+ // No comment - remove all COMMENT statements+ chunk.CommentStatements = nil}return nil}+// extractAlterSequenceASTFromSDL parses an ALTER SEQUENCE SDL and extracts the AST node+func extractAlterSequenceASTFromSDL(sdl string) (antlr.ParserRuleContext, error) {+ if sdl == "" {+ return nil, errors.New("empty ALTER SEQUENCE SDL provided")+ }++ parseResult, err := pgparser.ParsePostgreSQL(sdl)+ if err != nil {+ return nil, errors.Wrap(err, "failed to parse ALTER SEQUENCE SDL")+ }++ tree := parseResult.Tree+ if tree == nil {+ return nil, errors.New("parse result tree is nil")+ }++ // Extract the ALTER SEQUENCE node+ var result *parser.AlterseqstmtContext+ extractor := &alterSequenceExtractor{result: &result}+ antlr.NewParseTreeWalker().Walk(extractor, tree)++ if result == nil {+ return nil, errors.New("failed to extract ALTER SEQUENCE AST node from SDL")+ }++ return result, nil+}++// extractCommentASTFromSDL parses a COMMENT ON SDL and extracts the AST node+func extractCommentASTFromSDL(sdl string) (antlr.ParserRuleContext, error) {+ if sdl == "" {+ return nil, errors.New("empty COMMENT SDL provided")+ }++ parseResult, err := pgparser.ParsePostgreSQL(sdl)+ if err != nil {+ return nil, errors.Wrap(err, "failed to parse COMMENT SDL")+ }++ tree := parseResult.Tree+ if tree == nil {+ return nil, errors.New("parse result tree is nil")+ }++ // Extract the COMMENT node+ var result *parser.CommentstmtContext+ extractor := &commentExtractor{result: &result}+ antlr.NewParseTreeWalker().Walk(extractor, tree)++ if result == nil {+ return nil, errors.New("failed to extract COMMENT AST node from SDL")+ }++ return result, nil+}++// alterSequenceExtractor extracts ALTER SEQUENCE statement from AST+type alterSequenceExtractor struct {+ *parser.BasePostgreSQLParserListener+ result **parser.AlterseqstmtContext+}++func (e *alterSequenceExtractor) EnterAlterseqstmt(ctx *parser.AlterseqstmtContext) {+ if *e.result == nil {+ *e.result = ctx+ }+}++// commentExtractor extracts COMMENT statement from AST+type commentExtractor struct {+ *parser.BasePostgreSQLParserListener+ result **parser.CommentstmtContext+}++func (e *commentExtractor) EnterCommentstmt(ctx *parser.CommentstmtContext) {+ if *e.result == nil {+ *e.result = ctx+ }+}+// deleteSequenceChunk removes a sequence chunk from the chunksfunc deleteSequenceChunk(chunks *schema.SDLChunks, sequenceKey string) {if chunks != nil && chunks.Sequences != nil {@@ -4154,6 +4436,27 @@ func generateCreateSequenceSDL(schemaName string, sequence *storepb.SequenceMeta// sequenceDefinitionsEqual compares two sequence definitions to see if they are equivalentfunc sequenceDefinitionsEqual(sequence1, sequence2 *storepb.SequenceMetadata) bool {+ // First check everything except comment and owner+ if !sequenceDefinitionsEqualExcludingCommentAndOwner(sequence1, sequence2) {+ return false+ }++ // Then check owner+ if sequence1.OwnerTable != sequence2.OwnerTable ||+ sequence1.OwnerColumn != sequence2.OwnerColumn {+ return false+ }++ // Finally check comment+ if sequence1.Comment != sequence2.Comment {+ return false+ }++ return true+}++// sequenceDefinitionsEqualExcludingCommentAndOwner compares two sequence definitions excluding comments and owner+func sequenceDefinitionsEqualExcludingCommentAndOwner(sequence1, sequence2 *storepb.SequenceMetadata) bool {if sequence1 == nil && sequence2 == nil {return true}@@ -4166,16 +4469,14 @@ func sequenceDefinitionsEqual(sequence1, sequence2 *storepb.SequenceMetadata) boreturn false}- // Compare sequence parameters+ // Compare sequence parameters (excluding OwnerTable, OwnerColumn, and Comment)if sequence1.DataType != sequence2.DataType ||sequence1.Start != sequence2.Start ||sequence1.Increment != sequence2.Increment ||sequence1.MaxValue != sequence2.MaxValue ||sequence1.MinValue != sequence2.MinValue ||sequence1.CacheSize != sequence2.CacheSize ||- sequence1.Cycle != sequence2.Cycle ||- sequence1.OwnerTable != sequence2.OwnerTable ||- sequence1.OwnerColumn != sequence2.OwnerColumn {+ sequence1.Cycle != sequence2.Cycle {return false}@@ -4498,3 +4799,379 @@ func getFirstCommentNode(nodes []antlr.ParserRuleContext) antlr.ParserRuleContex}return nodes[0]}++// applyViewChangesToChunks applies minimal changes to CREATE VIEW chunks+// This handles creation, modification, and deletion of view statements+func applyViewChangesToChunks(previousChunks *schema.SDLChunks, currentSchema, previousSchema *model.DatabaseSchema) error {+ if currentSchema == nil || previousSchema == nil || previousChunks == nil {+ return nil+ }++ // Get view differences by comparing schema metadata+ currentMetadata := currentSchema.GetMetadata()+ previousMetadata := previousSchema.GetMetadata()+ if currentMetadata == nil || previousMetadata == nil {+ return nil+ }++ // Build view maps for current and previous schemas+ currentViews := make(map[string]*storepb.ViewMetadata)+ previousViews := make(map[string]*storepb.ViewMetadata)++ // Collect all views from current schema+ for _, schema := range currentMetadata.Schemas {+ for _, view := range schema.Views {+ viewKey := formatViewKey(schema.Name, view.Name)+ currentViews[viewKey] = view+ }+ }++ // Collect all views from previous schema+ for _, schema := range previousMetadata.Schemas {+ for _, view := range schema.Views {+ viewKey := formatViewKey(schema.Name, view.Name)+ previousViews[viewKey] = view+ }+ }++ // Process view additions: create new view chunks+ for viewKey, currentView := range currentViews {+ if _, exists := previousViews[viewKey]; !exists {+ // New view - create a chunk for it+ err := createViewChunk(previousChunks, currentView, viewKey)+ if err != nil {+ return errors.Wrapf(err, "failed to create view chunk for %s", viewKey)+ }+ }+ }++ // Process view modifications: update existing chunks or create if chunk doesn't exist+ for viewKey, currentView := range currentViews {+ if previousView, exists := previousViews[viewKey]; exists {+ // View exists in both metadata+ // Check if chunk exists in SDL - if not, create it; otherwise update it+ if _, chunkExists := previousChunks.Views[viewKey]; chunkExists {+ // Chunk exists - update if needed+ err := updateViewChunkIfNeeded(previousChunks, currentView, previousView, viewKey)+ if err != nil {+ return errors.Wrapf(err, "failed to update view chunk for %s", viewKey)+ }+ } else {+ // Chunk doesn't exist in SDL but view exists in database+ // Create chunk to add it to SDL+ err := createViewChunk(previousChunks, currentView, viewKey)+ if err != nil {+ return errors.Wrapf(err, "failed to create view chunk for %s", viewKey)+ }+ }+ }+ }++ // Process view deletions: remove dropped view chunks+ for viewKey := range previousViews {+ if _, exists := currentViews[viewKey]; !exists {+ // View was dropped - remove it from chunks+ deleteViewChunk(previousChunks, viewKey)+ }+ }++ return nil+}++// formatViewKey creates a consistent key for view identification+func formatViewKey(schemaName, viewName string) string {+ if schemaName == "" {+ schemaName = "public"+ }+ return schemaName + "." + viewName+}++// createViewChunk creates a new CREATE VIEW chunk and adds it to the chunks+func createViewChunk(chunks *schema.SDLChunks, view *storepb.ViewMetadata, viewKey string) error {+ if view == nil || chunks == nil {+ return nil+ }++ // Generate SDL text for the view+ schemaName, _ := parseIdentifier(viewKey)+ viewSDL := generateCreateViewSDL(schemaName, view)+ if viewSDL == "" {+ return errors.New("failed to generate SDL for view")+ }++ // Parse the SDL to get AST node+ parseResult, err := pgparser.ParsePostgreSQL(viewSDL)+ if err != nil {+ return errors.Wrapf(err, "failed to parse generated view SDL: %s", viewSDL)+ }++ // Extract the CREATE VIEW AST node+ var viewASTNode *parser.ViewstmtContext+ antlr.ParseTreeWalkerDefault.Walk(&viewExtractor{+ result: &viewASTNode,+ }, parseResult.Tree)++ if viewASTNode == nil {+ return errors.New("failed to extract CREATE VIEW AST node")+ }++ // Create and add the chunk+ chunk := &schema.SDLChunk{+ Identifier: viewKey,+ ASTNode: viewASTNode,+ }++ if chunks.Views == nil {+ chunks.Views = make(map[string]*schema.SDLChunk)+ }+ chunks.Views[viewKey] = chunk++ return nil+}++// updateViewChunkIfNeeded updates an existing view chunk if the view definition has changed+func updateViewChunkIfNeeded(chunks *schema.SDLChunks, currentView, previousView *storepb.ViewMetadata, viewKey string) error {+ if currentView == nil || previousView == nil || chunks == nil {+ return nil+ }++ // Get the existing chunk+ chunk, exists := chunks.Views[viewKey]+ if !exists {+ return errors.Errorf("view chunk not found for key %s", viewKey)+ }++ // Check if the CREATE VIEW definition has changed (excluding comment)+ definitionChanged := !viewDefinitionsEqualExcludingComment(currentView, previousView)++ if definitionChanged {+ // View definition has changed - regenerate the CREATE VIEW chunk+ schemaName, _ := parseIdentifier(viewKey)+ viewSDL := generateCreateViewSDL(schemaName, currentView)+ if viewSDL == "" {+ return errors.New("failed to generate SDL for view")+ }++ // Parse the SDL to get AST node+ parseResult, err := pgparser.ParsePostgreSQL(viewSDL)+ if err != nil {+ return errors.Wrapf(err, "failed to parse generated view SDL: %s", viewSDL)+ }++ // Extract the CREATE VIEW AST node+ var viewASTNode *parser.ViewstmtContext+ antlr.ParseTreeWalkerDefault.Walk(&viewExtractor{+ result: &viewASTNode,+ }, parseResult.Tree)++ if viewASTNode == nil {+ return errors.New("failed to extract CREATE VIEW AST node")+ }++ // Update the CREATE VIEW AST node+ chunk.ASTNode = viewASTNode+ }++ // Synchronize COMMENT ON VIEW statements only if comment has changed+ if currentView.Comment != previousView.Comment {+ schemaName, _ := parseIdentifier(viewKey)+ if err := syncObjectCommentStatements(chunk, currentView.Comment, "VIEW", schemaName, currentView.Name); err != nil {+ return errors.Wrapf(err, "failed to sync COMMENT statements for view %s", viewKey)+ }+ }++ return nil+}++// deleteViewChunk removes a view chunk from the chunks+func deleteViewChunk(chunks *schema.SDLChunks, viewKey string) {+ if chunks != nil && chunks.Views != nil {+ delete(chunks.Views, viewKey)+ }+}++// viewDefinitionsEqualExcludingComment compares two view definitions excluding comments+func viewDefinitionsEqualExcludingComment(view1, view2 *storepb.ViewMetadata) bool {+ if view1 == nil || view2 == nil {+ return false+ }++ // Compare name and definition (excluding comment)+ if view1.Name != view2.Name ||+ view1.Definition != view2.Definition {+ return false+ }++ return true+}++// generateCreateViewSDL generates SDL text for a CREATE VIEW statement+func generateCreateViewSDL(schemaName string, view *storepb.ViewMetadata) string {+ if view == nil {+ return ""+ }++ var buf strings.Builder+ if err := writeViewSDL(&buf, schemaName, view); err != nil {+ return ""+ }++ return buf.String()+}++// viewExtractor is a walker to extract CREATE VIEW AST nodes+type viewExtractor struct {+ parser.BasePostgreSQLParserListener+ result **parser.ViewstmtContext+}++func (e *viewExtractor) EnterViewstmt(ctx *parser.ViewstmtContext) {+ if e.result != nil && *e.result == nil {+ *e.result = ctx+ }+}++// applyColumnCommentChanges applies minimal changes to column comments based on schema metadata+// This function only updates COMMENT ON COLUMN statements without modifying CREATE TABLE statements+func applyColumnCommentChanges(previousChunks *schema.SDLChunks, currentSchema, previousSchema *model.DatabaseSchema) error {+ if currentSchema == nil || previousSchema == nil || previousChunks == nil {+ return nil+ }++ // Get table metadata from schemas+ currentMetadata := currentSchema.GetMetadata()+ previousMetadata := previousSchema.GetMetadata()+ if currentMetadata == nil || previousMetadata == nil {+ return nil+ }++ // Build table maps for current and previous schemas+ currentTables := make(map[string]*storepb.TableMetadata)+ previousTables := make(map[string]*storepb.TableMetadata)++ // Collect all tables from current schema+ for _, schema := range currentMetadata.Schemas {+ schemaName := schema.Name+ if schemaName == "" {+ schemaName = "public"+ }+ for _, table := range schema.Tables {+ tableKey := schemaName + "." + table.Name+ currentTables[tableKey] = table+ }+ }++ // Collect all tables from previous schema+ for _, schema := range previousMetadata.Schemas {+ schemaName := schema.Name+ if schemaName == "" {+ schemaName = "public"+ }+ for _, table := range schema.Tables {+ tableKey := schemaName + "." + table.Name+ previousTables[tableKey] = table+ }+ }++ // Initialize ColumnComments map if needed+ if previousChunks.ColumnComments == nil {+ previousChunks.ColumnComments = make(map[string]map[string]antlr.ParserRuleContext)+ }++ // Process tables that exist in both schemas+ for tableKey, currentTable := range currentTables {+ previousTable, exists := previousTables[tableKey]+ if !exists {+ // Table is new, skip (will be handled by table creation)+ continue+ }++ // Build column maps+ currentColumns := make(map[string]*storepb.ColumnMetadata)+ previousColumns := make(map[string]*storepb.ColumnMetadata)++ for _, col := range currentTable.Columns {+ currentColumns[col.Name] = col+ }+ for _, col := range previousTable.Columns {+ previousColumns[col.Name] = col+ }++ // Process columns that exist in both versions+ for columnName, currentColumn := range currentColumns {+ previousColumn, colExists := previousColumns[columnName]+ if !colExists {+ // Column is new, skip (will be handled by column addition)+ continue+ }++ // Check if comment has changed+ if currentColumn.Comment != previousColumn.Comment {+ err := syncColumnComment(previousChunks, tableKey, columnName, currentColumn.Comment)+ if err != nil {+ return errors.Wrapf(err, "failed to sync comment for column %s.%s", tableKey, columnName)+ }+ }+ }++ // Process columns that were dropped - remove their comments+ for columnName := range previousColumns {+ if _, exists := currentColumns[columnName]; !exists {+ // Column was dropped - remove its comment+ if previousChunks.ColumnComments[tableKey] != nil {+ delete(previousChunks.ColumnComments[tableKey], columnName)+ if len(previousChunks.ColumnComments[tableKey]) == 0 {+ delete(previousChunks.ColumnComments, tableKey)+ }+ }+ }+ }+ }++ return nil+}++// syncColumnComment synchronizes a single column comment in the chunks+func syncColumnComment(chunks *schema.SDLChunks, tableKey, columnName, comment string) error {+ if chunks == nil {+ return nil+ }++ // Parse table key to get schema and table names+ schemaName, tableName := parseIdentifier(tableKey)++ // If comment is empty, remove the comment statement+ if comment == "" {+ if chunks.ColumnComments[tableKey] != nil {+ delete(chunks.ColumnComments[tableKey], columnName)+ if len(chunks.ColumnComments[tableKey]) == 0 {+ delete(chunks.ColumnComments, tableKey)+ }+ }+ return nil+ }++ // Generate COMMENT ON COLUMN statement+ escapedComment := strings.ReplaceAll(comment, "'", "''")+ commentSDL := fmt.Sprintf("COMMENT ON COLUMN \"%s\".\"%s\".\"%s\" IS '%s';",+ schemaName, tableName, columnName, escapedComment)++ // Parse the SDL to get AST node+ commentNode, err := extractCommentASTFromSDL(commentSDL)+ if err != nil {+ return errors.Wrapf(err, "failed to parse column comment SDL: %s", commentSDL)+ }++ // Initialize maps if needed+ if chunks.ColumnComments == nil {+ chunks.ColumnComments = make(map[string]map[string]antlr.ParserRuleContext)+ }+ if chunks.ColumnComments[tableKey] == nil {+ chunks.ColumnComments[tableKey] = make(map[string]antlr.ParserRuleContext)+ }++ // Update the comment node+ chunks.ColumnComments[tableKey][columnName] = commentNode++ return nil+}diff --git a/backend/plugin/schema/pg/get_sdl_diff_test.go b/backend/plugin/schema/pg/get_sdl_diff_test.goindex bc9203a6dc..b7820d1578 100644--- a/backend/plugin/schema/pg/get_sdl_diff_test.go+++ b/backend/plugin/schema/pg/get_sdl_diff_test.go@@ -2444,3 +2444,2473 @@ func TestConvertDatabaseSchemaToSDL_SerialAndIdentitySequences(t *testing.T) {})}}++func TestApplySequenceChangesToChunks_PreservesAlterAndComment(t *testing.T) {+ tests := []struct {+ name string+ previousUserSDLText string+ currentSchema *model.DatabaseSchema+ previousSchema *model.DatabaseSchema+ shouldPreserveAlter bool+ shouldPreserveComment bool+ expectedAlterCount int+ expectedCommentCount int+ expectedSequenceProperties map[string]string // Properties that should change+ }{+ {+ name: "preserve_alter_sequence_on_metadata_change",+ previousUserSDLText: `CREATE SEQUENCE public.my_seq+ START WITH 1+ INCREMENT BY 1+ NO MINVALUE+ NO MAXVALUE+ CACHE 1;++ALTER SEQUENCE public.my_seq OWNED BY public.users.id;++COMMENT ON SEQUENCE public.my_seq IS 'User ID sequence';`,+ currentSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Sequences: []*storepb.SequenceMetadata{+ {+ Name: "my_seq",+ DataType: "bigint",+ Start: "1",+ Increment: "2", // Changed from 1 to 2+ MinValue: "1",+ MaxValue: "9223372036854775807",+ Cycle: false,+ CacheSize: "1",+ OwnerTable: "users",+ OwnerColumn: "id",+ Comment: "User ID sequence",+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ previousSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Sequences: []*storepb.SequenceMetadata{+ {+ Name: "my_seq",+ DataType: "bigint",+ Start: "1",+ Increment: "1",+ MinValue: "1",+ MaxValue: "9223372036854775807",+ Cycle: false,+ CacheSize: "1",+ OwnerTable: "users",+ OwnerColumn: "id",+ Comment: "User ID sequence",+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ shouldPreserveAlter: true,+ shouldPreserveComment: true,+ expectedAlterCount: 1,+ expectedCommentCount: 1,+ expectedSequenceProperties: map[string]string{+ "INCREMENT": "2",+ },+ },+ {+ name: "preserve_comment_only",+ previousUserSDLText: `CREATE SEQUENCE public.counter_seq+ START WITH 100+ INCREMENT BY 1+ NO MINVALUE+ NO MAXVALUE+ CACHE 1;++COMMENT ON SEQUENCE public.counter_seq IS 'Global counter';`,+ currentSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Sequences: []*storepb.SequenceMetadata{+ {+ Name: "counter_seq",+ DataType: "bigint",+ Start: "100",+ Increment: "5", // Changed+ MinValue: "1",+ MaxValue: "9223372036854775807",+ Cycle: false,+ CacheSize: "1",+ Comment: "Global counter",+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ previousSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Sequences: []*storepb.SequenceMetadata{+ {+ Name: "counter_seq",+ DataType: "bigint",+ Start: "100",+ Increment: "1",+ MinValue: "1",+ MaxValue: "9223372036854775807",+ Cycle: false,+ CacheSize: "1",+ Comment: "Global counter",+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ shouldPreserveAlter: false,+ shouldPreserveComment: true,+ expectedAlterCount: 0,+ expectedCommentCount: 1,+ expectedSequenceProperties: map[string]string{+ "INCREMENT": "5",+ },+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ // Parse previous SDL+ previousChunks, err := ChunkSDLText(tt.previousUserSDLText)+ require.NoError(t, err)+ require.NotNil(t, previousChunks)++ // Log original chunks+ t.Log("=== Previous SDL Chunks (Before Drift Handling) ===")+ for identifier, chunk := range previousChunks.Sequences {+ t.Logf("Sequence %s:", identifier)+ t.Logf(" Text: %s", chunk.GetText())+ t.Logf(" AlterStatements count: %d", len(chunk.AlterStatements))+ t.Logf(" CommentStatements count: %d", len(chunk.CommentStatements))+ }++ // Apply drift handling+ err = applySequenceChangesToChunks(previousChunks, tt.currentSchema, tt.previousSchema)+ require.NoError(t, err)++ // Log chunks after drift handling+ t.Log("=== Previous SDL Chunks (After Drift Handling) ===")+ for identifier, chunk := range previousChunks.Sequences {+ t.Logf("Sequence %s:", identifier)+ t.Logf(" Text: %s", chunk.GetText())+ t.Logf(" AlterStatements count: %d", len(chunk.AlterStatements))+ t.Logf(" CommentStatements count: %d", len(chunk.CommentStatements))+ }++ // Verify sequence exists+ sequenceChunk := previousChunks.Sequences["public.my_seq"]+ if tt.name == "preserve_comment_only" {+ sequenceChunk = previousChunks.Sequences["public.counter_seq"]+ }+ require.NotNil(t, sequenceChunk, "Sequence chunk should exist after drift handling")++ // Get full text+ fullText := sequenceChunk.GetText()+ t.Logf("Full sequence text:\n%s", fullText)++ // Verify ALTER statements are preserved+ if tt.shouldPreserveAlter {+ assert.Equal(t, tt.expectedAlterCount, len(sequenceChunk.AlterStatements),+ "ALTER statements should be preserved during drift handling")+ if tt.expectedAlterCount > 0 {+ assert.Contains(t, fullText, "ALTER SEQUENCE",+ "Full text should contain ALTER SEQUENCE statement")+ assert.Contains(t, fullText, "OWNED BY",+ "Full text should contain OWNED BY clause")+ }+ } else {+ assert.Equal(t, tt.expectedAlterCount, len(sequenceChunk.AlterStatements),+ "Should have expected number of ALTER statements")+ }++ // Verify COMMENT statements are preserved+ if tt.shouldPreserveComment {+ assert.Equal(t, tt.expectedCommentCount, len(sequenceChunk.CommentStatements),+ "COMMENT statements should be preserved during drift handling")+ if tt.expectedCommentCount > 0 {+ assert.Contains(t, fullText, "COMMENT ON SEQUENCE",+ "Full text should contain COMMENT ON SEQUENCE statement")+ }+ } else {+ assert.Equal(t, tt.expectedCommentCount, len(sequenceChunk.CommentStatements),+ "Should have expected number of COMMENT statements")+ }++ // Verify that sequence properties were updated+ createText := extractTextFromNode(sequenceChunk.ASTNode)+ for property, expectedValue := range tt.expectedSequenceProperties {+ assert.Contains(t, createText, expectedValue,+ "CREATE SEQUENCE should reflect the new %s value", property)+ }+ })+ }+}++func TestApplySequenceChangesToChunks_OwnerDrift(t *testing.T) {+ tests := []struct {+ name string+ previousUserSDLText string+ currentSchema *model.DatabaseSchema+ previousSchema *model.DatabaseSchema+ expectedAlterStatements int+ expectedOwnerTable string+ expectedOwnerColumn string+ }{+ {+ name: "sequence_owner_drifted_from_users_to_posts",+ previousUserSDLText: `CREATE SEQUENCE public.my_seq+ START WITH 1+ INCREMENT BY 1+ NO MINVALUE+ NO MAXVALUE+ CACHE 1;++ALTER SEQUENCE public.my_seq OWNED BY public.users.id;`,+ currentSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Sequences: []*storepb.SequenceMetadata{+ {+ Name: "my_seq",+ DataType: "bigint",+ Start: "1",+ Increment: "1",+ MinValue: "1",+ MaxValue: "9223372036854775807",+ Cycle: false,+ CacheSize: "1",+ OwnerTable: "posts", // Changed from "users"+ OwnerColumn: "id",+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ previousSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Sequences: []*storepb.SequenceMetadata{+ {+ Name: "my_seq",+ DataType: "bigint",+ Start: "1",+ Increment: "1",+ MinValue: "1",+ MaxValue: "9223372036854775807",+ Cycle: false,+ CacheSize: "1",+ OwnerTable: "users",+ OwnerColumn: "id",+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ expectedAlterStatements: 1,+ expectedOwnerTable: "posts",+ expectedOwnerColumn: "id",+ },+ {+ name: "sequence_owner_removed",+ previousUserSDLText: `CREATE SEQUENCE public.counter_seq+ START WITH 100+ INCREMENT BY 1+ NO MINVALUE+ NO MAXVALUE+ CACHE 1;++ALTER SEQUENCE public.counter_seq OWNED BY public.users.id;`,+ currentSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Sequences: []*storepb.SequenceMetadata{+ {+ Name: "counter_seq",+ DataType: "bigint",+ Start: "100",+ Increment: "1",+ MinValue: "1",+ MaxValue: "9223372036854775807",+ Cycle: false,+ CacheSize: "1",+ OwnerTable: "", // Owner removed+ OwnerColumn: "",+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ previousSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Sequences: []*storepb.SequenceMetadata{+ {+ Name: "counter_seq",+ DataType: "bigint",+ Start: "100",+ Increment: "1",+ MinValue: "1",+ MaxValue: "9223372036854775807",+ Cycle: false,+ CacheSize: "1",+ OwnerTable: "users",+ OwnerColumn: "id",+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ expectedAlterStatements: 0, // Should be removed+ expectedOwnerTable: "",+ expectedOwnerColumn: "",+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ // Parse previous SDL+ previousChunks, err := ChunkSDLText(tt.previousUserSDLText)+ require.NoError(t, err)+ require.NotNil(t, previousChunks)++ // Log original chunks+ t.Log("=== Previous SDL Chunks (Before Drift Handling) ===")+ for identifier, chunk := range previousChunks.Sequences {+ t.Logf("Sequence %s:", identifier)+ t.Logf(" Text: %s", chunk.GetText())+ t.Logf(" AlterStatements count: %d", len(chunk.AlterStatements))+ }++ // Apply drift handling+ err = applySequenceChangesToChunks(previousChunks, tt.currentSchema, tt.previousSchema)+ require.NoError(t, err)++ // Log chunks after drift handling+ t.Log("=== Previous SDL Chunks (After Drift Handling) ===")+ for identifier, chunk := range previousChunks.Sequences {+ t.Logf("Sequence %s:", identifier)+ t.Logf(" Text: %s", chunk.GetText())+ t.Logf(" AlterStatements count: %d", len(chunk.AlterStatements))+ }++ // Get the sequence chunk+ var sequenceChunk *schema.SDLChunk+ for _, chunk := range previousChunks.Sequences {+ sequenceChunk = chunk+ break+ }+ require.NotNil(t, sequenceChunk, "Sequence chunk should exist after drift handling")++ // Get full text+ fullText := sequenceChunk.GetText()+ t.Logf("Full sequence text:\n%s", fullText)++ // Verify ALTER statements count+ assert.Equal(t, tt.expectedAlterStatements, len(sequenceChunk.AlterStatements),+ "ALTER statements count should match expected")++ // Verify the ALTER statement content matches the expected owner+ if tt.expectedAlterStatements > 0 {+ // Should contain ALTER SEQUENCE with correct owner+ // The owner reference can be in format: "table"."column" or table.column+ assert.Contains(t, fullText, "ALTER SEQUENCE",+ "Full text should contain ALTER SEQUENCE statement")+ assert.Contains(t, fullText, "OWNED BY",+ "ALTER SEQUENCE should contain OWNED BY clause")+ // Check that it references the correct table and column (with or without quotes/schema)+ assert.Contains(t, fullText, tt.expectedOwnerTable,+ "ALTER SEQUENCE should reference the correct owner table: %s", tt.expectedOwnerTable)+ assert.Contains(t, fullText, tt.expectedOwnerColumn,+ "ALTER SEQUENCE should reference the correct owner column: %s", tt.expectedOwnerColumn)+ } else {+ // Should NOT contain ALTER SEQUENCE+ assert.NotContains(t, fullText, "ALTER SEQUENCE",+ "Full text should NOT contain ALTER SEQUENCE when owner is removed")+ }+ })+ }+}++func TestApplySequenceChangesToChunks_CommentDrift(t *testing.T) {+ tests := []struct {+ name string+ previousUserSDLText string+ currentSchema *model.DatabaseSchema+ previousSchema *model.DatabaseSchema+ expectedCommentStatements int+ expectedCommentText string+ }{+ {+ name: "comment_changed_but_sequence_definition_unchanged",+ previousUserSDLText: `CREATE SEQUENCE public.my_seq+ START WITH 1+ INCREMENT BY 1+ NO MINVALUE+ NO MAXVALUE+ CACHE 1;++COMMENT ON SEQUENCE public.my_seq IS 'Old comment';`,+ currentSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Sequences: []*storepb.SequenceMetadata{+ {+ Name: "my_seq",+ DataType: "bigint",+ Start: "1",+ Increment: "1",+ MinValue: "1",+ MaxValue: "9223372036854775807",+ Cycle: false,+ CacheSize: "1",+ Comment: "New comment", // Changed+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ previousSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Sequences: []*storepb.SequenceMetadata{+ {+ Name: "my_seq",+ DataType: "bigint",+ Start: "1",+ Increment: "1",+ MinValue: "1",+ MaxValue: "9223372036854775807",+ Cycle: false,+ CacheSize: "1",+ Comment: "Old comment",+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ expectedCommentStatements: 1,+ expectedCommentText: "New comment",+ },+ {+ name: "comment_added_when_previously_none",+ previousUserSDLText: `CREATE SEQUENCE public.new_seq+ START WITH 1+ INCREMENT BY 1+ NO MINVALUE+ NO MAXVALUE+ CACHE 1;`,+ currentSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Sequences: []*storepb.SequenceMetadata{+ {+ Name: "new_seq",+ DataType: "bigint",+ Start: "1",+ Increment: "1",+ MinValue: "1",+ MaxValue: "9223372036854775807",+ Cycle: false,+ CacheSize: "1",+ Comment: "New comment added", // Added+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ previousSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Sequences: []*storepb.SequenceMetadata{+ {+ Name: "new_seq",+ DataType: "bigint",+ Start: "1",+ Increment: "1",+ MinValue: "1",+ MaxValue: "9223372036854775807",+ Cycle: false,+ CacheSize: "1",+ Comment: "", // No comment before+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ expectedCommentStatements: 1,+ expectedCommentText: "New comment added",+ },+ {+ name: "comment_removed",+ previousUserSDLText: `CREATE SEQUENCE public.counter_seq+ START WITH 100+ INCREMENT BY 1+ NO MINVALUE+ NO MAXVALUE+ CACHE 1;++COMMENT ON SEQUENCE public.counter_seq IS 'Some comment';`,+ currentSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Sequences: []*storepb.SequenceMetadata{+ {+ Name: "counter_seq",+ DataType: "bigint",+ Start: "100",+ Increment: "1",+ MinValue: "1",+ MaxValue: "9223372036854775807",+ Cycle: false,+ CacheSize: "1",+ Comment: "", // Removed+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ previousSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Sequences: []*storepb.SequenceMetadata{+ {+ Name: "counter_seq",+ DataType: "bigint",+ Start: "100",+ Increment: "1",+ MinValue: "1",+ MaxValue: "9223372036854775807",+ Cycle: false,+ CacheSize: "1",+ Comment: "Some comment",+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ expectedCommentStatements: 0,+ expectedCommentText: "",+ },+ {+ name: "comment_unchanged_should_preserve_original_format",+ previousUserSDLText: `CREATE SEQUENCE public.preserve_seq+ START WITH 1+ INCREMENT BY 1+ NO MINVALUE+ NO MAXVALUE+ CACHE 1;++COMMENT ON SEQUENCE public.preserve_seq IS 'Unchanged comment';`,+ currentSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Sequences: []*storepb.SequenceMetadata{+ {+ Name: "preserve_seq",+ DataType: "bigint",+ Start: "1",+ Increment: "1",+ MinValue: "1",+ MaxValue: "9223372036854775807",+ Cycle: false,+ CacheSize: "1",+ Comment: "Unchanged comment", // Same as before+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ previousSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Sequences: []*storepb.SequenceMetadata{+ {+ Name: "preserve_seq",+ DataType: "bigint",+ Start: "1",+ Increment: "1",+ MinValue: "1",+ MaxValue: "9223372036854775807",+ Cycle: false,+ CacheSize: "1",+ Comment: "Unchanged comment", // Same+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ expectedCommentStatements: 1,+ expectedCommentText: "Unchanged comment",+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ // Parse previous SDL+ previousChunks, err := ChunkSDLText(tt.previousUserSDLText)+ require.NoError(t, err)+ require.NotNil(t, previousChunks)++ // Log original chunks+ t.Log("=== Previous SDL Chunks (Before Drift Handling) ===")+ for identifier, chunk := range previousChunks.Sequences {+ t.Logf("Sequence %s:", identifier)+ t.Logf(" Text: %s", chunk.GetText())+ t.Logf(" CommentStatements count: %d", len(chunk.CommentStatements))+ }++ // Apply drift handling+ err = applySequenceChangesToChunks(previousChunks, tt.currentSchema, tt.previousSchema)+ require.NoError(t, err)++ // Log chunks after drift handling+ t.Log("=== Previous SDL Chunks (After Drift Handling) ===")+ for identifier, chunk := range previousChunks.Sequences {+ t.Logf("Sequence %s:", identifier)+ t.Logf(" Text: %s", chunk.GetText())+ t.Logf(" CommentStatements count: %d", len(chunk.CommentStatements))+ }++ // Get the sequence chunk+ var sequenceChunk *schema.SDLChunk+ for _, chunk := range previousChunks.Sequences {+ sequenceChunk = chunk+ break+ }+ require.NotNil(t, sequenceChunk, "Sequence chunk should exist after drift handling")++ // Get full text+ fullText := sequenceChunk.GetText()+ t.Logf("Full sequence text:\n%s", fullText)++ // Verify COMMENT statements count+ assert.Equal(t, tt.expectedCommentStatements, len(sequenceChunk.CommentStatements),+ "COMMENT statements count should match expected")++ // Verify the COMMENT statement content+ if tt.expectedCommentStatements > 0 && tt.expectedCommentText != "" {+ // Use Contains with "COMMENT" and "SEQUENCE" to be flexible with formatting+ assert.Contains(t, fullText, "COMMENT",+ "Full text should contain COMMENT statement")+ assert.Contains(t, fullText, "SEQUENCE",+ "Full text should mention SEQUENCE")+ assert.Contains(t, fullText, tt.expectedCommentText,+ "COMMENT should contain the expected text: %s", tt.expectedCommentText)+ } else if tt.expectedCommentText == "" && tt.expectedCommentStatements == 0 {+ // Either no comment or empty comment+ assert.NotContains(t, fullText, "COMMENT ON SEQUENCE",+ "Full text should NOT contain COMMENT ON SEQUENCE when comment is removed")+ }+ })+ }+}++func TestApplySequenceChangesToChunks_OwnedSequenceNotInSDL(t *testing.T) {+ tests := []struct {+ name string+ previousUserSDLText string+ currentSchema *model.DatabaseSchema+ previousSchema *model.DatabaseSchema+ expectSequenceChunk bool+ sequenceKey string+ }{+ {+ name: "serial_sequence_not_in_sdl_should_not_create_chunk",+ // User SDL only contains CREATE TABLE, not the sequence created by SERIAL+ previousUserSDLText: `CREATE TABLE public.users (+ id SERIAL PRIMARY KEY,+ name TEXT+);`,+ previousSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Tables: []*storepb.TableMetadata{+ {+ Name: "users",+ Columns: []*storepb.ColumnMetadata{+ {Name: "id", Type: "integer", Default: "nextval('users_id_seq'::regclass)"},+ {Name: "name", Type: "text"},+ },+ },+ },+ Sequences: []*storepb.SequenceMetadata{+ {+ Name: "users_id_seq",+ DataType: "bigint",+ Start: "1",+ Increment: "1",+ MinValue: "1",+ MaxValue: "9223372036854775807",+ Cycle: false,+ CacheSize: "1",+ OwnerTable: "users",+ OwnerColumn: "id",+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ 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", Default: "nextval('users_id_seq'::regclass)"},+ {Name: "name", Type: "text"},+ },+ },+ },+ Sequences: []*storepb.SequenceMetadata{+ {+ Name: "users_id_seq",+ DataType: "bigint",+ Start: "1",+ Increment: "2", // Changed increment+ MinValue: "1",+ MaxValue: "9223372036854775807",+ Cycle: false,+ CacheSize: "1",+ OwnerTable: "users",+ OwnerColumn: "id",+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ expectSequenceChunk: false,+ sequenceKey: "public.users_id_seq",+ },+ {+ name: "identity_sequence_not_in_sdl_should_not_create_chunk",+ // User SDL only contains CREATE TABLE with IDENTITY column, not the sequence+ previousUserSDLText: `CREATE TABLE public.products (+ id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,+ name TEXT+);`,+ previousSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Tables: []*storepb.TableMetadata{+ {+ Name: "products",+ Columns: []*storepb.ColumnMetadata{+ {Name: "id", Type: "integer", Default: "nextval('products_id_seq'::regclass)"},+ {Name: "name", Type: "text"},+ },+ },+ },+ Sequences: []*storepb.SequenceMetadata{+ {+ Name: "products_id_seq",+ DataType: "bigint",+ Start: "1",+ Increment: "1",+ MinValue: "1",+ MaxValue: "9223372036854775807",+ Cycle: false,+ CacheSize: "1",+ OwnerTable: "products",+ OwnerColumn: "id",+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ 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", Default: "nextval('products_id_seq'::regclass)"},+ {Name: "name", Type: "text"},+ },+ },+ },+ Sequences: []*storepb.SequenceMetadata{+ {+ Name: "products_id_seq",+ DataType: "bigint",+ Start: "1",+ Increment: "5", // Changed increment+ MinValue: "1",+ MaxValue: "9223372036854775807",+ Cycle: false,+ CacheSize: "1",+ OwnerTable: "products",+ OwnerColumn: "id",+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ expectSequenceChunk: false,+ sequenceKey: "public.products_id_seq",+ },+ {+ name: "owned_sequence_explicitly_in_sdl_should_update_chunk",+ // User explicitly defined the sequence in SDL, so it should be updated+ previousUserSDLText: `CREATE SEQUENCE public.orders_id_seq+ START WITH 1+ INCREMENT BY 1+ NO MINVALUE+ NO MAXVALUE+ CACHE 1;++CREATE TABLE public.orders (+ id INT DEFAULT nextval('orders_id_seq'),+ amount DECIMAL+);++ALTER SEQUENCE public.orders_id_seq OWNED BY public.orders.id;`,+ previousSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Tables: []*storepb.TableMetadata{+ {+ Name: "orders",+ Columns: []*storepb.ColumnMetadata{+ {Name: "id", Type: "integer", Default: "nextval('orders_id_seq'::regclass)"},+ {Name: "amount", Type: "numeric"},+ },+ },+ },+ Sequences: []*storepb.SequenceMetadata{+ {+ Name: "orders_id_seq",+ DataType: "bigint",+ Start: "1",+ Increment: "1",+ MinValue: "1",+ MaxValue: "9223372036854775807",+ Cycle: false,+ CacheSize: "1",+ OwnerTable: "orders",+ OwnerColumn: "id",+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ currentSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Tables: []*storepb.TableMetadata{+ {+ Name: "orders",+ Columns: []*storepb.ColumnMetadata{+ {Name: "id", Type: "integer", Default: "nextval('orders_id_seq'::regclass)"},+ {Name: "amount", Type: "numeric"},+ },+ },+ },+ Sequences: []*storepb.SequenceMetadata{+ {+ Name: "orders_id_seq",+ DataType: "bigint",+ Start: "1",+ Increment: "10", // Changed increment+ MinValue: "1",+ MaxValue: "9223372036854775807",+ Cycle: false,+ CacheSize: "1",+ OwnerTable: "orders",+ OwnerColumn: "id",+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ expectSequenceChunk: true,+ sequenceKey: "public.orders_id_seq",+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ // Parse previous SDL text into chunks+ previousChunks, err := ChunkSDLText(tt.previousUserSDLText)+ require.NoError(t, err)++ // Apply sequence changes to chunks (this is what happens during drift handling)+ err = applySequenceChangesToChunks(previousChunks, tt.currentSchema, tt.previousSchema)+ require.NoError(t, err)++ // Verify whether sequence chunk was created/updated+ _, exists := previousChunks.Sequences[tt.sequenceKey]+ if tt.expectSequenceChunk {+ require.True(t, exists, "Expected sequence chunk to exist for %s", tt.sequenceKey)+ } else {+ require.False(t, exists, "Expected sequence chunk NOT to exist for %s (owned sequences not in SDL should not be created)", tt.sequenceKey)+ }+ })+ }+}++func TestApplyFunctionChangesToChunks_CommentDrift(t *testing.T) {+ tests := []struct {+ name string+ previousUserSDLText string+ currentSchema *model.DatabaseSchema+ previousSchema *model.DatabaseSchema+ expectedCommentStatements int+ expectedCommentText string+ }{+ {+ name: "function_comment_changed_but_definition_unchanged",+ previousUserSDLText: `CREATE FUNCTION public.calculate_total(amount INTEGER) RETURNS INTEGER AS $$+BEGIN+ RETURN amount * 2;+END;+$$ LANGUAGE plpgsql;+COMMENT ON FUNCTION public.calculate_total(INTEGER) IS 'Old comment';`,+ currentSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Functions: []*storepb.FunctionMetadata{+ {+ Name: "calculate_total",+ Definition: "CREATE FUNCTION public.calculate_total(amount INTEGER) RETURNS INTEGER AS $$\nBEGIN\n RETURN amount * 2;\nEND;\n$$ LANGUAGE plpgsql",+ Comment: "New comment", // Changed from "Old comment"+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ previousSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Functions: []*storepb.FunctionMetadata{+ {+ Name: "calculate_total",+ Definition: "CREATE FUNCTION public.calculate_total(amount INTEGER) RETURNS INTEGER AS $$\nBEGIN\n RETURN amount * 2;\nEND;\n$$ LANGUAGE plpgsql",+ Comment: "Old comment",+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ expectedCommentStatements: 1,+ expectedCommentText: "New comment",+ },+ {+ name: "function_comment_added_when_previously_none",+ previousUserSDLText: `CREATE FUNCTION public.add_numbers(a INTEGER, b INTEGER) RETURNS INTEGER AS $$+BEGIN+ RETURN a + b;+END;+$$ LANGUAGE plpgsql;`,+ currentSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Functions: []*storepb.FunctionMetadata{+ {+ Name: "add_numbers",+ Definition: "CREATE FUNCTION public.add_numbers(a INTEGER, b INTEGER) RETURNS INTEGER AS $$\nBEGIN\n RETURN a + b;\nEND;\n$$ LANGUAGE plpgsql",+ Comment: "Adds two numbers", // Added+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ previousSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Functions: []*storepb.FunctionMetadata{+ {+ Name: "add_numbers",+ Definition: "CREATE FUNCTION public.add_numbers(a INTEGER, b INTEGER) RETURNS INTEGER AS $$\nBEGIN\n RETURN a + b;\nEND;\n$$ LANGUAGE plpgsql",+ Comment: "", // No comment before+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ expectedCommentStatements: 1,+ expectedCommentText: "Adds two numbers",+ },+ {+ name: "function_comment_removed",+ previousUserSDLText: `CREATE FUNCTION public.process_data(input_text TEXT) RETURNS TEXT AS $$+BEGIN+ RETURN upper(input_text);+END;+$$ LANGUAGE plpgsql;+COMMENT ON FUNCTION public.process_data(TEXT) IS 'Old comment';`,+ currentSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Functions: []*storepb.FunctionMetadata{+ {+ Name: "process_data",+ Definition: "CREATE FUNCTION public.process_data(input_text TEXT) RETURNS TEXT AS $$\nBEGIN\n RETURN upper(input_text);\nEND;\n$$ LANGUAGE plpgsql",+ Comment: "", // Comment removed+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ previousSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Functions: []*storepb.FunctionMetadata{+ {+ Name: "process_data",+ Definition: "CREATE FUNCTION public.process_data(input_text TEXT) RETURNS TEXT AS $$\nBEGIN\n RETURN upper(input_text);\nEND;\n$$ LANGUAGE plpgsql",+ Comment: "Old comment",+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ expectedCommentStatements: 0,+ expectedCommentText: "",+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ // Parse previous SDL+ previousChunks, err := ChunkSDLText(tt.previousUserSDLText)+ require.NoError(t, err)+ require.NotNil(t, previousChunks)++ // Log original chunks+ t.Log("=== Previous SDL Chunks (Before Drift Handling) ===")+ for identifier, chunk := range previousChunks.Functions {+ t.Logf("Function %s:", identifier)+ t.Logf(" Text: %s", chunk.GetText())+ t.Logf(" CommentStatements count: %d", len(chunk.CommentStatements))+ }++ // Apply drift handling+ err = applyFunctionChangesToChunks(previousChunks, tt.currentSchema, tt.previousSchema)+ require.NoError(t, err)++ // Log chunks after drift handling+ t.Log("=== Previous SDL Chunks (After Drift Handling) ===")+ for identifier, chunk := range previousChunks.Functions {+ t.Logf("Function %s:", identifier)+ t.Logf(" Text: %s", chunk.GetText())+ t.Logf(" CommentStatements count: %d", len(chunk.CommentStatements))+ }++ // Get the function chunk+ var functionChunk *schema.SDLChunk+ for _, chunk := range previousChunks.Functions {+ functionChunk = chunk+ break+ }+ require.NotNil(t, functionChunk, "Function chunk should exist after drift handling")++ // Get full text+ fullText := functionChunk.GetText()+ t.Logf("Full function text:\n%s", fullText)++ // Verify COMMENT statements count+ assert.Equal(t, tt.expectedCommentStatements, len(functionChunk.CommentStatements),+ "COMMENT statements count should match expected")++ // Verify COMMENT content+ if tt.expectedCommentStatements > 0 {+ assert.Contains(t, fullText, "COMMENT ON FUNCTION",+ "Full text should contain COMMENT ON FUNCTION statement")+ assert.Contains(t, fullText, tt.expectedCommentText,+ "COMMENT should contain the expected text: %s", tt.expectedCommentText)+ } else {+ // Should NOT contain COMMENT ON FUNCTION+ assert.NotContains(t, fullText, "COMMENT ON FUNCTION",+ "Full text should NOT contain COMMENT ON FUNCTION when comment is removed")+ }+ })+ }+}++func TestApplyIndexChangesToChunks_CommentDrift(t *testing.T) {+ tests := []struct {+ name string+ previousUserSDLText string+ currentSchema *model.DatabaseSchema+ previousSchema *model.DatabaseSchema+ expectedCommentStatements int+ expectedCommentText string+ }{+ {+ name: "index_comment_changed_but_definition_unchanged",+ previousUserSDLText: `CREATE TABLE public.users (+ id INTEGER PRIMARY KEY,+ email TEXT+);++CREATE INDEX idx_users_email ON public.users (email);++COMMENT ON INDEX public.idx_users_email IS 'Old comment';`,+ 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"},+ {Name: "email", Type: "text"},+ },+ Indexes: []*storepb.IndexMetadata{+ {+ Name: "idx_users_email",+ Expressions: []string{"email"},+ Type: "btree",+ Unique: false,+ Primary: false,+ Comment: "New comment", // Changed from "Old comment"+ },+ },+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ previousSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Tables: []*storepb.TableMetadata{+ {+ Name: "users",+ Columns: []*storepb.ColumnMetadata{+ {Name: "id", Type: "integer"},+ {Name: "email", Type: "text"},+ },+ Indexes: []*storepb.IndexMetadata{+ {+ Name: "idx_users_email",+ Expressions: []string{"email"},+ Type: "btree",+ Unique: false,+ Primary: false,+ Comment: "Old comment",+ },+ },+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ expectedCommentStatements: 1,+ expectedCommentText: "New comment",+ },+ {+ name: "index_comment_added_when_previously_none",+ previousUserSDLText: `CREATE TABLE public.products (+ id INTEGER PRIMARY KEY,+ name TEXT+);++CREATE INDEX idx_products_name ON public.products (name);`,+ 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"},+ {Name: "name", Type: "text"},+ },+ Indexes: []*storepb.IndexMetadata{+ {+ Name: "idx_products_name",+ Expressions: []string{"name"},+ Type: "btree",+ Unique: false,+ Primary: false,+ Comment: "Index for product names", // Added+ },+ },+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ previousSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Tables: []*storepb.TableMetadata{+ {+ Name: "products",+ Columns: []*storepb.ColumnMetadata{+ {Name: "id", Type: "integer"},+ {Name: "name", Type: "text"},+ },+ Indexes: []*storepb.IndexMetadata{+ {+ Name: "idx_products_name",+ Expressions: []string{"name"},+ Type: "btree",+ Unique: false,+ Primary: false,+ Comment: "", // No comment before+ },+ },+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ expectedCommentStatements: 1,+ expectedCommentText: "Index for product names",+ },+ {+ name: "index_comment_removed",+ previousUserSDLText: `CREATE TABLE public.posts (+ id INTEGER PRIMARY KEY,+ title TEXT+);++CREATE UNIQUE INDEX idx_posts_title ON public.posts (title);++COMMENT ON INDEX public.idx_posts_title IS 'Old comment';`,+ currentSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Tables: []*storepb.TableMetadata{+ {+ Name: "posts",+ Columns: []*storepb.ColumnMetadata{+ {Name: "id", Type: "integer"},+ {Name: "title", Type: "text"},+ },+ Indexes: []*storepb.IndexMetadata{+ {+ Name: "idx_posts_title",+ Expressions: []string{"title"},+ Type: "btree",+ Unique: true,+ Primary: false,+ Comment: "", // Comment removed+ },+ },+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ previousSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Tables: []*storepb.TableMetadata{+ {+ Name: "posts",+ Columns: []*storepb.ColumnMetadata{+ {Name: "id", Type: "integer"},+ {Name: "title", Type: "text"},+ },+ Indexes: []*storepb.IndexMetadata{+ {+ Name: "idx_posts_title",+ Expressions: []string{"title"},+ Type: "btree",+ Unique: true,+ Primary: false,+ Comment: "Old comment",+ },+ },+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ expectedCommentStatements: 0,+ expectedCommentText: "",+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ // Parse previous SDL+ previousChunks, err := ChunkSDLText(tt.previousUserSDLText)+ require.NoError(t, err)+ require.NotNil(t, previousChunks)++ // Log original chunks+ t.Log("=== Previous SDL Chunks (Before Drift Handling) ===")+ for identifier, chunk := range previousChunks.Indexes {+ t.Logf("Index %s:", identifier)+ t.Logf(" Text: %s", chunk.GetText())+ t.Logf(" CommentStatements count: %d", len(chunk.CommentStatements))+ }++ // Apply drift handling+ err = applyStandaloneIndexChangesToChunks(previousChunks, tt.currentSchema, tt.previousSchema)+ require.NoError(t, err)++ // Log chunks after drift handling+ t.Log("=== Previous SDL Chunks (After Drift Handling) ===")+ for identifier, chunk := range previousChunks.Indexes {+ t.Logf("Index %s:", identifier)+ t.Logf(" Text: %s", chunk.GetText())+ t.Logf(" CommentStatements count: %d", len(chunk.CommentStatements))+ }++ // Get the index chunk+ var indexChunk *schema.SDLChunk+ for _, chunk := range previousChunks.Indexes {+ indexChunk = chunk+ break+ }+ require.NotNil(t, indexChunk, "Index chunk should exist after drift handling")++ // Get full text+ fullText := indexChunk.GetText()+ t.Logf("Full index text:\n%s", fullText)++ // Verify COMMENT statements count+ assert.Equal(t, tt.expectedCommentStatements, len(indexChunk.CommentStatements),+ "COMMENT statements count should match expected")++ // Verify COMMENT content+ if tt.expectedCommentStatements > 0 {+ assert.Contains(t, fullText, "COMMENT ON INDEX",+ "Full text should contain COMMENT ON INDEX statement")+ assert.Contains(t, fullText, tt.expectedCommentText,+ "COMMENT should contain the expected text: %s", tt.expectedCommentText)+ } else {+ // Should NOT contain COMMENT ON INDEX+ assert.NotContains(t, fullText, "COMMENT ON INDEX",+ "Full text should NOT contain COMMENT ON INDEX when comment is removed")+ }+ })+ }+}++func TestApplyViewChangesToChunks_CommentDrift(t *testing.T) {+ tests := []struct {+ name string+ previousUserSDLText string+ currentSchema *model.DatabaseSchema+ previousSchema *model.DatabaseSchema+ expectedCommentStatements int+ expectedCommentText string+ }{+ {+ name: "view_comment_changed_but_definition_unchanged",+ previousUserSDLText: `CREATE VIEW public.user_emails AS+SELECT id, email FROM users;++COMMENT ON VIEW public.user_emails IS 'Old comment';`,+ currentSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Views: []*storepb.ViewMetadata{+ {+ Name: "user_emails",+ Definition: "SELECT id, email FROM users",+ Comment: "New comment", // Changed from "Old comment"+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ previousSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Views: []*storepb.ViewMetadata{+ {+ Name: "user_emails",+ Definition: "SELECT id, email FROM users",+ Comment: "Old comment",+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ expectedCommentStatements: 1,+ expectedCommentText: "New comment",+ },+ {+ name: "view_comment_added_when_previously_none",+ previousUserSDLText: `CREATE VIEW public.admin_users AS+SELECT * FROM users WHERE role = 'admin';`,+ currentSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Views: []*storepb.ViewMetadata{+ {+ Name: "admin_users",+ Definition: "SELECT * FROM users WHERE role = 'admin'",+ Comment: "View of admin users", // Added+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ previousSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Views: []*storepb.ViewMetadata{+ {+ Name: "admin_users",+ Definition: "SELECT * FROM users WHERE role = 'admin'",+ Comment: "", // No comment before+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ expectedCommentStatements: 1,+ expectedCommentText: "View of admin users",+ },+ {+ name: "view_comment_removed",+ previousUserSDLText: `CREATE VIEW public.active_users AS+SELECT * FROM users WHERE active = true;++COMMENT ON VIEW public.active_users IS 'Old comment';`,+ currentSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Views: []*storepb.ViewMetadata{+ {+ Name: "active_users",+ Definition: "SELECT * FROM users WHERE active = true",+ Comment: "", // Comment removed+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ previousSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Views: []*storepb.ViewMetadata{+ {+ Name: "active_users",+ Definition: "SELECT * FROM users WHERE active = true",+ Comment: "Old comment",+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ expectedCommentStatements: 0,+ expectedCommentText: "",+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ // Parse previous SDL+ previousChunks, err := ChunkSDLText(tt.previousUserSDLText)+ require.NoError(t, err)+ require.NotNil(t, previousChunks)++ // Log original chunks+ t.Log("=== Previous SDL Chunks (Before Drift Handling) ===")+ for identifier, chunk := range previousChunks.Views {+ t.Logf("View %s:", identifier)+ t.Logf(" Text: %s", chunk.GetText())+ t.Logf(" CommentStatements count: %d", len(chunk.CommentStatements))+ }++ // Apply drift handling+ err = applyViewChangesToChunks(previousChunks, tt.currentSchema, tt.previousSchema)+ require.NoError(t, err)++ // Log chunks after drift handling+ t.Log("=== Previous SDL Chunks (After Drift Handling) ===")+ for identifier, chunk := range previousChunks.Views {+ t.Logf("View %s:", identifier)+ t.Logf(" Text: %s", chunk.GetText())+ t.Logf(" CommentStatements count: %d", len(chunk.CommentStatements))+ }++ // Get the view chunk+ var viewChunk *schema.SDLChunk+ for _, chunk := range previousChunks.Views {+ viewChunk = chunk+ break+ }+ require.NotNil(t, viewChunk, "View chunk should exist after drift handling")++ // Get full text+ fullText := viewChunk.GetText()+ t.Logf("Full view text:\n%s", fullText)++ // Verify COMMENT statements count+ assert.Equal(t, tt.expectedCommentStatements, len(viewChunk.CommentStatements),+ "COMMENT statements count should match expected")++ // Verify COMMENT content+ if tt.expectedCommentStatements > 0 {+ assert.Contains(t, fullText, "COMMENT ON VIEW",+ "Full text should contain COMMENT ON VIEW statement")+ assert.Contains(t, fullText, tt.expectedCommentText,+ "COMMENT should contain the expected text: %s", tt.expectedCommentText)+ } else {+ // Should NOT contain COMMENT ON VIEW+ assert.NotContains(t, fullText, "COMMENT ON VIEW",+ "Full text should NOT contain COMMENT ON VIEW when comment is removed")+ }+ })+ }+}++func TestApplyTableChangesToChunk_CommentDrift(t *testing.T) {+ tests := []struct {+ name string+ previousUserSDLText string+ currentSchema *model.DatabaseSchema+ previousSchema *model.DatabaseSchema+ expectedCommentStatements int+ expectedCommentText string+ }{+ {+ name: "table_comment_changed_but_definition_unchanged",+ previousUserSDLText: `CREATE TABLE public.products (+ id INTEGER PRIMARY KEY,+ name TEXT NOT NULL+);++COMMENT ON TABLE public.products IS 'Old comment';`,+ 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"},+ {Name: "name", Type: "text", Nullable: false},+ },+ Comment: "New comment", // Changed from "Old comment"+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ previousSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Tables: []*storepb.TableMetadata{+ {+ Name: "products",+ Columns: []*storepb.ColumnMetadata{+ {Name: "id", Type: "integer"},+ {Name: "name", Type: "text", Nullable: false},+ },+ Comment: "Old comment",+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ expectedCommentStatements: 1,+ expectedCommentText: "New comment",+ },+ {+ name: "table_comment_added_when_previously_none",+ previousUserSDLText: `CREATE TABLE public.customers (+ id INTEGER PRIMARY KEY,+ email TEXT NOT NULL+);`,+ currentSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Tables: []*storepb.TableMetadata{+ {+ Name: "customers",+ Columns: []*storepb.ColumnMetadata{+ {Name: "id", Type: "integer"},+ {Name: "email", Type: "text", Nullable: false},+ },+ Comment: "Customer information table", // Added+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ previousSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Tables: []*storepb.TableMetadata{+ {+ Name: "customers",+ Columns: []*storepb.ColumnMetadata{+ {Name: "id", Type: "integer"},+ {Name: "email", Type: "text", Nullable: false},+ },+ Comment: "", // No comment before+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ expectedCommentStatements: 1,+ expectedCommentText: "Customer information table",+ },+ {+ name: "table_comment_removed",+ previousUserSDLText: `CREATE TABLE public.orders (+ order_id INTEGER PRIMARY KEY,+ total NUMERIC+);++COMMENT ON TABLE public.orders IS 'Old comment';`,+ 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: "integer"},+ {Name: "total", Type: "numeric"},+ },+ Comment: "", // Comment removed+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ previousSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Tables: []*storepb.TableMetadata{+ {+ Name: "orders",+ Columns: []*storepb.ColumnMetadata{+ {Name: "order_id", Type: "integer"},+ {Name: "total", Type: "numeric"},+ },+ Comment: "Old comment",+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ expectedCommentStatements: 0,+ expectedCommentText: "",+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ // Parse previous SDL+ previousChunks, err := ChunkSDLText(tt.previousUserSDLText)+ require.NoError(t, err)+ require.NotNil(t, previousChunks)++ // Log original chunks+ t.Log("=== Previous SDL Chunks (Before Drift Handling) ===")+ for identifier, chunk := range previousChunks.Tables {+ t.Logf("Table %s:", identifier)+ t.Logf(" Text: %s", chunk.GetText())+ t.Logf(" CommentStatements count: %d", len(chunk.CommentStatements))+ }++ // Apply drift handling+ err = applyMinimalChangesToChunks(previousChunks, tt.currentSchema, tt.previousSchema)+ require.NoError(t, err)++ // Log chunks after drift handling+ t.Log("=== Previous SDL Chunks (After Drift Handling) ===")+ for identifier, chunk := range previousChunks.Tables {+ t.Logf("Table %s:", identifier)+ t.Logf(" Text: %s", chunk.GetText())+ t.Logf(" CommentStatements count: %d", len(chunk.CommentStatements))+ }++ // Get the table chunk+ var tableChunk *schema.SDLChunk+ for _, chunk := range previousChunks.Tables {+ tableChunk = chunk+ break+ }+ require.NotNil(t, tableChunk, "Table chunk should exist after drift handling")++ // Get full text+ fullText := tableChunk.GetText()+ t.Logf("Full table text:\n%s", fullText)++ // Verify COMMENT statements count+ assert.Equal(t, tt.expectedCommentStatements, len(tableChunk.CommentStatements),+ "COMMENT statements count should match expected")++ // Verify COMMENT content+ if tt.expectedCommentStatements > 0 {+ assert.Contains(t, fullText, "COMMENT ON TABLE",+ "Full text should contain COMMENT ON TABLE statement")+ assert.Contains(t, fullText, tt.expectedCommentText,+ "COMMENT should contain the expected text: %s", tt.expectedCommentText)+ } else {+ // Should NOT contain COMMENT ON TABLE+ assert.NotContains(t, fullText, "COMMENT ON TABLE",+ "Full text should NOT contain COMMENT ON TABLE when comment is removed")+ }+ })+ }+}++func TestApplyColumnCommentChanges(t *testing.T) {+ tests := []struct {+ name string+ previousUserSDLText string+ currentSchema *model.DatabaseSchema+ previousSchema *model.DatabaseSchema+ expectedColumnComments map[string]map[string]string // tableKey -> columnName -> comment+ shouldContainCommentOn []string // Expected COMMENT ON COLUMN statements+ shouldNotContainCommentOn []string // Should not contain these+ }{+ {+ name: "column_comment_changed_but_table_definition_unchanged",+ previousUserSDLText: `CREATE TABLE public.products (+ id INTEGER PRIMARY KEY,+ name TEXT NOT NULL+);++COMMENT ON COLUMN public.products.name IS 'Old column comment';`,+ 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"},+ {Name: "name", Type: "text", Nullable: false, Comment: "New column comment"},+ },+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ previousSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Tables: []*storepb.TableMetadata{+ {+ Name: "products",+ Columns: []*storepb.ColumnMetadata{+ {Name: "id", Type: "integer"},+ {Name: "name", Type: "text", Nullable: false, Comment: "Old column comment"},+ },+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ expectedColumnComments: map[string]map[string]string{+ "public.products": {+ "name": "New column comment",+ },+ },+ shouldContainCommentOn: []string{+ `COMMENT ON COLUMN "public"."products"."name" IS 'New column comment'`,+ },+ },+ {+ name: "column_comment_added_when_previously_none",+ previousUserSDLText: `CREATE TABLE public.inventory (+ id INTEGER PRIMARY KEY,+ quantity INTEGER NOT NULL+);`,+ currentSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Tables: []*storepb.TableMetadata{+ {+ Name: "inventory",+ Columns: []*storepb.ColumnMetadata{+ {Name: "id", Type: "integer"},+ {Name: "quantity", Type: "integer", Nullable: false, Comment: "Available quantity"}, // Added+ },+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ previousSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Tables: []*storepb.TableMetadata{+ {+ Name: "inventory",+ Columns: []*storepb.ColumnMetadata{+ {Name: "id", Type: "integer"},+ {Name: "quantity", Type: "integer", Nullable: false, Comment: ""}, // No comment before+ },+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ expectedColumnComments: map[string]map[string]string{+ "public.inventory": {+ "quantity": "Available quantity",+ },+ },+ shouldContainCommentOn: []string{+ `COMMENT ON COLUMN "public"."inventory"."quantity" IS 'Available quantity'`,+ },+ },+ {+ name: "column_comment_removed",+ previousUserSDLText: `CREATE TABLE public.orders (+ order_id INTEGER PRIMARY KEY,+ total NUMERIC+);++COMMENT ON COLUMN public.orders.total IS 'Old column comment';`,+ 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: "integer"},+ {Name: "total", Type: "numeric", Comment: ""}, // Comment removed+ },+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ previousSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Tables: []*storepb.TableMetadata{+ {+ Name: "orders",+ Columns: []*storepb.ColumnMetadata{+ {Name: "order_id", Type: "integer"},+ {Name: "total", Type: "numeric", Comment: "Old column comment"},+ },+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ expectedColumnComments: map[string]map[string]string{},+ shouldNotContainCommentOn: []string{+ `COMMENT ON COLUMN "public"."orders"."total"`,+ },+ },+ {+ name: "multiple_column_comments_changed",+ previousUserSDLText: `CREATE TABLE public.users (+ id SERIAL PRIMARY KEY,+ username VARCHAR(50) NOT NULL,+ email VARCHAR(100) NOT NULL,+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP+);++COMMENT ON COLUMN public.users.username IS 'Old username comment';+COMMENT ON COLUMN public.users.email IS 'Old email comment';`,+ 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"},+ {Name: "username", Type: "character varying", Nullable: false, Comment: "New username comment"},+ {Name: "email", Type: "character varying", Nullable: false, Comment: "New email comment"},+ {Name: "created_at", Type: "timestamp without time zone"},+ },+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ previousSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Tables: []*storepb.TableMetadata{+ {+ Name: "users",+ Columns: []*storepb.ColumnMetadata{+ {Name: "id", Type: "integer"},+ {Name: "username", Type: "character varying", Nullable: false, Comment: "Old username comment"},+ {Name: "email", Type: "character varying", Nullable: false, Comment: "Old email comment"},+ {Name: "created_at", Type: "timestamp without time zone"},+ },+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ expectedColumnComments: map[string]map[string]string{+ "public.users": {+ "username": "New username comment",+ "email": "New email comment",+ },+ },+ shouldContainCommentOn: []string{+ `COMMENT ON COLUMN "public"."users"."username" IS 'New username comment'`,+ `COMMENT ON COLUMN "public"."users"."email" IS 'New email comment'`,+ },+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ // Parse previous SDL+ previousChunks, err := ChunkSDLText(tt.previousUserSDLText)+ require.NoError(t, err)+ require.NotNil(t, previousChunks)++ // Log original chunks+ t.Log("=== Previous SDL Chunks (Before Drift Handling) ===")+ for identifier, chunk := range previousChunks.Tables {+ t.Logf("Table %s:", identifier)+ t.Logf(" CREATE TABLE text: %s", chunk.GetText())+ }+ if len(previousChunks.ColumnComments) > 0 {+ t.Logf("ColumnComments count: %d", len(previousChunks.ColumnComments))+ for tableKey, cols := range previousChunks.ColumnComments {+ t.Logf(" Table %s: %d columns with comments", tableKey, len(cols))+ for colName, colComment := range cols {+ t.Logf(" Column %s: %s", colName, colComment.GetText())+ }+ }+ }++ // Apply drift handling+ err = applyMinimalChangesToChunks(previousChunks, tt.currentSchema, tt.previousSchema)+ require.NoError(t, err)++ // Log chunks after drift handling+ t.Log("=== Previous SDL Chunks (After Drift Handling) ===")+ for identifier, chunk := range previousChunks.Tables {+ t.Logf("Table %s:", identifier)+ t.Logf(" CREATE TABLE text: %s", chunk.GetText())+ }+ if len(previousChunks.ColumnComments) > 0 {+ t.Logf("ColumnComments count: %d", len(previousChunks.ColumnComments))+ for tableKey, cols := range previousChunks.ColumnComments {+ t.Logf(" Table %s: %d columns with comments", tableKey, len(cols))+ for colName, colComment := range cols {+ t.Logf(" Column %s: %s", colName, colComment.GetText())+ }+ }+ }++ // Verify column comments+ for tableKey, expectedCols := range tt.expectedColumnComments {+ actualCols, exists := previousChunks.ColumnComments[tableKey]+ assert.True(t, exists, "Table %s should have column comments", tableKey)++ for colName, expectedComment := range expectedCols {+ commentNode, exists := actualCols[colName]+ assert.True(t, exists, "Column %s should have a comment", colName)+ if exists {+ commentText := commentNode.GetText()+ t.Logf("Column %s comment text: %s", colName, commentText)+ assert.Contains(t, commentText, expectedComment,+ "Column comment should contain expected text")+ }+ }++ // Verify expected columns count+ assert.Equal(t, len(expectedCols), len(actualCols),+ "Number of column comments should match")+ }++ // If no column comments expected, verify map is empty or table key doesn't exist+ if len(tt.expectedColumnComments) == 0 {+ for _, shouldNotContain := range tt.shouldNotContainCommentOn {+ found := false+ for _, cols := range previousChunks.ColumnComments {+ for _, commentNode := range cols {+ if strings.Contains(commentNode.GetText(), shouldNotContain) {+ found = true+ break+ }+ }+ if found {+ break+ }+ }+ assert.False(t, found, "Should not contain: %s", shouldNotContain)+ }+ }++ // Verify CREATE TABLE statement is preserved and contains expected COMMENT statements+ for tableKey := range tt.expectedColumnComments {+ tableChunk, exists := previousChunks.Tables[tableKey]+ assert.True(t, exists, "Table chunk should exist")+ if exists {+ tableText := tableChunk.GetText()+ t.Logf("Table text:\n%s", tableText)++ // Verify CREATE TABLE is preserved (should start with CREATE TABLE)+ assert.Contains(t, tableText, "CREATE TABLE",+ "CREATE TABLE statement should be preserved")++ // Verify it contains expected COMMENT statements+ for _, expectedComment := range tt.shouldContainCommentOn {+ // Check in ColumnComments map+ // Note: GetText() removes whitespace, so we normalize both strings for comparison+ normalizedExpected := strings.ReplaceAll(expectedComment, " ", "")+ found := false+ if cols, ok := previousChunks.ColumnComments[tableKey]; ok {+ for _, commentNode := range cols {+ normalizedActual := strings.ReplaceAll(commentNode.GetText(), " ", "")+ if strings.Contains(normalizedActual, normalizedExpected) {+ found = true+ break+ }+ }+ }+ assert.True(t, found, "Should contain COMMENT: %s", expectedComment)+ }+ }+ }+ })+ }+}