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
16 changes: 8 additions & 8 deletions internal/diff/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (cd *ColumnDiff) generateColumnSQL(tableSchema, tableName string, targetSch
// If type is changing with USING clause and there's an existing default, drop the default first
if needsUsing && hasOldDefault {
sql := fmt.Sprintf("ALTER TABLE %s ALTER COLUMN %s DROP DEFAULT;",
qualifiedTableName, cd.New.Name)
qualifiedTableName, ir.QuoteIdentifier(cd.New.Name))
statements = append(statements, sql)
}

Expand All @@ -38,11 +38,11 @@ func (cd *ColumnDiff) generateColumnSQL(tableSchema, tableName string, targetSch
// because PostgreSQL cannot implicitly cast these types
if needsUsing {
sql := fmt.Sprintf("ALTER TABLE %s ALTER COLUMN %s TYPE %s USING %s::%s;",
qualifiedTableName, cd.New.Name, newType, cd.New.Name, newType)
qualifiedTableName, ir.QuoteIdentifier(cd.New.Name), newType, ir.QuoteIdentifier(cd.New.Name), newType)
statements = append(statements, sql)
} else {
sql := fmt.Sprintf("ALTER TABLE %s ALTER COLUMN %s TYPE %s;",
qualifiedTableName, cd.New.Name, newType)
qualifiedTableName, ir.QuoteIdentifier(cd.New.Name), newType)
statements = append(statements, sql)
}
}
Expand All @@ -52,12 +52,12 @@ func (cd *ColumnDiff) generateColumnSQL(tableSchema, tableName string, targetSch
if cd.New.IsNullable {
// DROP NOT NULL
sql := fmt.Sprintf("ALTER TABLE %s ALTER COLUMN %s DROP NOT NULL;",
qualifiedTableName, cd.New.Name)
qualifiedTableName, ir.QuoteIdentifier(cd.New.Name))
statements = append(statements, sql)
} else {
// ADD NOT NULL - generate canonical SQL only
sql := fmt.Sprintf("ALTER TABLE %s ALTER COLUMN %s SET NOT NULL;",
qualifiedTableName, cd.New.Name)
qualifiedTableName, ir.QuoteIdentifier(cd.New.Name))
statements = append(statements, sql)
}
}
Expand All @@ -69,7 +69,7 @@ func (cd *ColumnDiff) generateColumnSQL(tableSchema, tableName string, targetSch
// Default was dropped above; add new default if specified
if newDefault != nil && *newDefault != "" {
sql := fmt.Sprintf("ALTER TABLE %s ALTER COLUMN %s SET DEFAULT %s;",
qualifiedTableName, cd.New.Name, *newDefault)
qualifiedTableName, ir.QuoteIdentifier(cd.New.Name), *newDefault)
statements = append(statements, sql)
}
} else {
Expand All @@ -80,10 +80,10 @@ func (cd *ColumnDiff) generateColumnSQL(tableSchema, tableName string, targetSch
var sql string
if newDefault == nil {
sql = fmt.Sprintf("ALTER TABLE %s ALTER COLUMN %s DROP DEFAULT;",
qualifiedTableName, cd.New.Name)
qualifiedTableName, ir.QuoteIdentifier(cd.New.Name))
} else {
sql = fmt.Sprintf("ALTER TABLE %s ALTER COLUMN %s SET DEFAULT %s;",
qualifiedTableName, cd.New.Name, *newDefault)
qualifiedTableName, ir.QuoteIdentifier(cd.New.Name), *newDefault)
}

statements = append(statements, sql)
Expand Down
6 changes: 3 additions & 3 deletions internal/diff/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ func generateCreateTablesSQL(
for _, column := range table.Columns {
if column.Comment != "" {
tableName := qualifyEntityName(table.Schema, table.Name, targetSchema)
sql := fmt.Sprintf("COMMENT ON COLUMN %s.%s IS %s;", tableName, column.Name, quoteString(column.Comment))
sql := fmt.Sprintf("COMMENT ON COLUMN %s.%s IS %s;", tableName, ir.QuoteIdentifier(column.Name), quoteString(column.Comment))

// Create context for this statement
context := &diffContext{
Expand Down Expand Up @@ -1163,9 +1163,9 @@ func (td *tableDiff) generateAlterTableStatements(targetSchema string, collector
tableName := getTableNameWithSchema(td.Table.Schema, td.Table.Name, targetSchema)
var sql string
if colDiff.New.Comment == "" {
sql = fmt.Sprintf("COMMENT ON COLUMN %s.%s IS NULL;", tableName, colDiff.New.Name)
sql = fmt.Sprintf("COMMENT ON COLUMN %s.%s IS NULL;", tableName, ir.QuoteIdentifier(colDiff.New.Name))
} else {
sql = fmt.Sprintf("COMMENT ON COLUMN %s.%s IS %s;", tableName, colDiff.New.Name, quoteString(colDiff.New.Comment))
sql = fmt.Sprintf("COMMENT ON COLUMN %s.%s IS %s;", tableName, ir.QuoteIdentifier(colDiff.New.Name), quoteString(colDiff.New.Comment))
}

context := &diffContext{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
COMMENT ON COLUMN ex."ID" IS 'Primary identifier';
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE public.ex (
"ID" integer NOT NULL
);

COMMENT ON COLUMN ex."ID" IS 'Primary identifier';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CREATE TABLE public.ex (
"ID" integer NOT NULL
);
20 changes: 20 additions & 0 deletions testdata/diff/comment/column_comment_quoted_identifier/plan.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"version": "1.0.0",
"pgschema_version": "1.6.1",
"created_at": "1970-01-01T00:00:00Z",
"source_fingerprint": {
"hash": "9d443bc536153eed8fce077bfacc3d7f42b1a94f02d33868bb78be3b9de05088"
},
"groups": [
{
"steps": [
{
"sql": "COMMENT ON COLUMN ex.\"ID\" IS 'Primary identifier';",
"type": "table.column.comment",
"operation": "alter",
"path": "public.ex.ID"
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
COMMENT ON COLUMN ex."ID" IS 'Primary identifier';
13 changes: 13 additions & 0 deletions testdata/diff/comment/column_comment_quoted_identifier/plan.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Plan: 1 to modify.

Summary by type:
tables: 1 to modify

Tables:
~ ex
~ ID (column.comment)

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

COMMENT ON COLUMN ex."ID" IS 'Primary identifier';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE ex ALTER COLUMN "ID" TYPE bigint;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CREATE TABLE public.ex (
"ID" bigint NOT NULL
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CREATE TABLE public.ex (
"ID" integer NOT NULL
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"version": "1.0.0",
"pgschema_version": "1.6.1",
"created_at": "1970-01-01T00:00:00Z",
"source_fingerprint": {
"hash": "9d443bc536153eed8fce077bfacc3d7f42b1a94f02d33868bb78be3b9de05088"
},
"groups": [
{
"steps": [
{
"sql": "ALTER TABLE ex ALTER COLUMN \"ID\" TYPE bigint;",
"type": "table.column",
"operation": "alter",
"path": "public.ex.ID"
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE ex ALTER COLUMN "ID" TYPE bigint;
13 changes: 13 additions & 0 deletions testdata/diff/create_table/alter_column_quoted_identifier/plan.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Plan: 1 to modify.

Summary by type:
tables: 1 to modify

Tables:
~ ex
~ ID (column)

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

ALTER TABLE ex ALTER COLUMN "ID" TYPE bigint;
Loading