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
11 changes: 11 additions & 0 deletions internal/diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -2179,6 +2179,17 @@ func qualifyEntityNameMode(entitySchema, entityName, targetSchema string, qualif
return fmt.Sprintf("%s.%s", quotedSchema, quotedName)
}

// qualifyColumnCommentTable returns the table name for use in COMMENT ON COLUMN
// statements. Like qualifyEntityNameMode, it omits the schema when the table is
// in the target schema — unless the table name itself matches the target schema,
// which would cause stripSchemaQualifications to strip the table qualifier.
func qualifyColumnCommentTable(tableSchema, tableName, targetSchema string, qualifySchema bool) string {
if !qualifySchema && tableSchema == targetSchema && strings.EqualFold(tableName, targetSchema) {
return fmt.Sprintf("%s.%s", ir.QuoteIdentifier(tableSchema), ir.QuoteIdentifier(tableName))
}
return qualifyEntityNameMode(tableSchema, tableName, targetSchema, qualifySchema)
}

// quoteString properly quotes a string for SQL, handling single quotes
func quoteString(s string) string {
// Escape single quotes by doubling them
Expand Down
6 changes: 3 additions & 3 deletions internal/diff/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ func generateCreateTablesSQL(
// Add column comments
for _, column := range table.Columns {
if column.Comment != "" {
tableName := qualifyEntityNameMode(table.Schema, table.Name, targetSchema, collector.qualifySchema)
tableName := qualifyColumnCommentTable(table.Schema, table.Name, targetSchema, collector.qualifySchema)
sql := fmt.Sprintf("COMMENT ON COLUMN %s.%s IS %s;", tableName, ir.QuoteIdentifier(column.Name), quoteString(column.Comment))

// Create context for this statement
Expand Down Expand Up @@ -1134,7 +1134,7 @@ func (td *tableDiff) generateAlterTableStatements(targetSchema string, collector
// Add comments for new columns
for _, column := range td.AddedColumns {
if column.Comment != "" {
tableName := getTableNameWithSchema(td.Table.Schema, td.Table.Name, targetSchema)
tableName := qualifyColumnCommentTable(td.Table.Schema, td.Table.Name, targetSchema, false)
sql := fmt.Sprintf("COMMENT ON COLUMN %s.%s IS %s;", tableName, ir.QuoteIdentifier(column.Name), quoteString(column.Comment))

context := &diffContext{
Expand Down Expand Up @@ -1603,7 +1603,7 @@ func (td *tableDiff) generateAlterTableStatements(targetSchema string, collector
// Handle column comment changes
for _, colDiff := range td.ModifiedColumns {
if colDiff.Old.Comment != colDiff.New.Comment {
tableName := getTableNameWithSchema(td.Table.Schema, td.Table.Name, targetSchema)
tableName := qualifyColumnCommentTable(td.Table.Schema, td.Table.Name, targetSchema, false)
var sql string
if colDiff.New.Comment == "" {
sql = fmt.Sprintf("COMMENT ON COLUMN %s.%s IS NULL;", tableName, ir.QuoteIdentifier(colDiff.New.Name))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
COMMENT ON COLUMN public.public.id IS 'Primary key';

COMMENT ON COLUMN public.public.name IS 'Display name';
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE public (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL
);

COMMENT ON COLUMN public.public.id IS 'Primary key';
COMMENT ON COLUMN public.public.name IS 'Display name';
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE public (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"version": "1.0.0",
"pgschema_version": "1.11.1",
"created_at": "1970-01-01T00:00:00Z",
"source_fingerprint": {
"hash": "c8aff322ec8a9d064fda21505117c0b296ba9669f7503ad7d8055d7f8f1f5ff7"
},
"groups": [
{
"steps": [
{
"sql": "COMMENT ON COLUMN public.public.id IS 'Primary key';",
"type": "table.column.comment",
"operation": "alter",
"path": "public.public.id"
},
{
"sql": "COMMENT ON COLUMN public.public.name IS 'Display name';",
"type": "table.column.comment",
"operation": "alter",
"path": "public.public.name"
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
COMMENT ON COLUMN public.public.id IS 'Primary key';

COMMENT ON COLUMN public.public.name IS 'Display name';
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Plan: 1 to modify.

Summary by type:
tables: 1 to modify

Tables:
~ public
~ id (column.comment)
~ name (column.comment)

DDL to be executed:
--------------------------------------------------

COMMENT ON COLUMN public.public.id IS 'Primary key';

COMMENT ON COLUMN public.public.name IS 'Display name';