diff --git a/internal/diff/table.go b/internal/diff/table.go index 8983a6b7..927465ec 100644 --- a/internal/diff/table.go +++ b/internal/diff/table.go @@ -766,6 +766,23 @@ func (td *tableDiff) generateAlterTableStatements(targetSchema string, collector collector.collect(context, stmt+";") } + // Add comments for new columns + for _, column := range td.AddedColumns { + if column.Comment != "" { + tableName := getTableNameWithSchema(td.Table.Schema, td.Table.Name, targetSchema) + sql := fmt.Sprintf("COMMENT ON COLUMN %s.%s IS %s;", tableName, ir.QuoteIdentifier(column.Name), quoteString(column.Comment)) + + context := &diffContext{ + Type: DiffTypeTableColumnComment, + Operation: DiffOperationCreate, + Path: fmt.Sprintf("%s.%s.%s", td.Table.Schema, td.Table.Name, column.Name), + Source: column, + CanRunInTransaction: true, + } + collector.collect(context, sql) + } + } + // Modify existing columns - already sorted by the Diff operation for _, ColumnDiff := range td.ModifiedColumns { // Generate column modification statements and collect as a single step diff --git a/testdata/diff/comment/mixed_comments/diff.sql b/testdata/diff/comment/mixed_comments/diff.sql index 0307ba26..9fdf7197 100644 --- a/testdata/diff/comment/mixed_comments/diff.sql +++ b/testdata/diff/comment/mixed_comments/diff.sql @@ -12,6 +12,10 @@ COMMENT ON COLUMN categories.created_at IS 'Category creation timestamp'; COMMENT ON INDEX idx_categories_parent IS 'Index for hierarchical category queries'; +ALTER TABLE posts ADD COLUMN views integer DEFAULT 0; + +COMMENT ON COLUMN posts.views IS 'Number of post views'; + COMMENT ON TABLE posts IS 'Blog posts and articles'; COMMENT ON COLUMN posts.id IS 'Unique post identifier'; diff --git a/testdata/diff/comment/mixed_comments/new.sql b/testdata/diff/comment/mixed_comments/new.sql index aeca554f..19f115e9 100644 --- a/testdata/diff/comment/mixed_comments/new.sql +++ b/testdata/diff/comment/mixed_comments/new.sql @@ -3,7 +3,8 @@ CREATE TABLE posts ( title VARCHAR(200) NOT NULL, content TEXT, author_id INTEGER NOT NULL, - published_at TIMESTAMP + published_at TIMESTAMP, + views INTEGER DEFAULT 0 ); COMMENT ON TABLE posts IS 'Blog posts and articles'; COMMENT ON COLUMN posts.id IS 'Unique post identifier'; @@ -11,6 +12,7 @@ COMMENT ON COLUMN posts.title IS 'Post title, max 200 characters'; COMMENT ON COLUMN posts.content IS 'Post body in markdown format'; COMMENT ON COLUMN posts.author_id IS 'Foreign key to users table'; COMMENT ON COLUMN posts.published_at IS 'Publication timestamp, NULL for drafts'; +COMMENT ON COLUMN posts.views IS 'Number of post views'; CREATE INDEX idx_posts_author ON posts (author_id); COMMENT ON INDEX idx_posts_author IS 'Index for finding posts by author'; diff --git a/testdata/diff/comment/mixed_comments/plan.json b/testdata/diff/comment/mixed_comments/plan.json index 1a431aaf..91735907 100644 --- a/testdata/diff/comment/mixed_comments/plan.json +++ b/testdata/diff/comment/mixed_comments/plan.json @@ -50,6 +50,18 @@ "operation": "alter", "path": "public.categories.idx_categories_parent" }, + { + "sql": "ALTER TABLE posts ADD COLUMN views integer DEFAULT 0;", + "type": "table.column", + "operation": "create", + "path": "public.posts.views" + }, + { + "sql": "COMMENT ON COLUMN posts.views IS 'Number of post views';", + "type": "table.column.comment", + "operation": "create", + "path": "public.posts.views" + }, { "sql": "COMMENT ON TABLE posts IS 'Blog posts and articles';", "type": "table.comment", diff --git a/testdata/diff/comment/mixed_comments/plan.sql b/testdata/diff/comment/mixed_comments/plan.sql index 0307ba26..9fdf7197 100644 --- a/testdata/diff/comment/mixed_comments/plan.sql +++ b/testdata/diff/comment/mixed_comments/plan.sql @@ -12,6 +12,10 @@ COMMENT ON COLUMN categories.created_at IS 'Category creation timestamp'; COMMENT ON INDEX idx_categories_parent IS 'Index for hierarchical category queries'; +ALTER TABLE posts ADD COLUMN views integer DEFAULT 0; + +COMMENT ON COLUMN posts.views IS 'Number of post views'; + COMMENT ON TABLE posts IS 'Blog posts and articles'; COMMENT ON COLUMN posts.id IS 'Unique post identifier'; diff --git a/testdata/diff/comment/mixed_comments/plan.txt b/testdata/diff/comment/mixed_comments/plan.txt index ae5960b9..6ac14ba1 100644 --- a/testdata/diff/comment/mixed_comments/plan.txt +++ b/testdata/diff/comment/mixed_comments/plan.txt @@ -13,11 +13,13 @@ Tables: ~ categories (comment) ~ idx_categories_parent (index.comment) ~ posts + + views (column) ~ author_id (column.comment) ~ content (column.comment) ~ id (column.comment) ~ published_at (column.comment) ~ title (column.comment) + + views (column.comment) ~ posts (comment) ~ idx_posts_author (index.comment) ~ idx_posts_published (index.comment) @@ -39,6 +41,10 @@ COMMENT ON COLUMN categories.created_at IS 'Category creation timestamp'; COMMENT ON INDEX idx_categories_parent IS 'Index for hierarchical category queries'; +ALTER TABLE posts ADD COLUMN views integer DEFAULT 0; + +COMMENT ON COLUMN posts.views IS 'Number of post views'; + COMMENT ON TABLE posts IS 'Blog posts and articles'; COMMENT ON COLUMN posts.id IS 'Unique post identifier';