Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions internal/diff/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions testdata/diff/comment/mixed_comments/diff.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
4 changes: 3 additions & 1 deletion testdata/diff/comment/mixed_comments/new.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ 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';
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';
Expand Down
12 changes: 12 additions & 0 deletions testdata/diff/comment/mixed_comments/plan.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions testdata/diff/comment/mixed_comments/plan.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
6 changes: 6 additions & 0 deletions testdata/diff/comment/mixed_comments/plan.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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';
Expand Down