Skip to content

Commit cc76ca3

Browse files
authored
fix(internal): apply QuotedIdentifier in more cases (#274)
Add test cases to test for these, fixes #270
1 parent 1b8344b commit cc76ca3

File tree

14 files changed

+95
-11
lines changed

14 files changed

+95
-11
lines changed

internal/diff/column.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func (cd *ColumnDiff) generateColumnSQL(tableSchema, tableName string, targetSch
2727
// If type is changing with USING clause and there's an existing default, drop the default first
2828
if needsUsing && hasOldDefault {
2929
sql := fmt.Sprintf("ALTER TABLE %s ALTER COLUMN %s DROP DEFAULT;",
30-
qualifiedTableName, cd.New.Name)
30+
qualifiedTableName, ir.QuoteIdentifier(cd.New.Name))
3131
statements = append(statements, sql)
3232
}
3333

@@ -38,11 +38,11 @@ func (cd *ColumnDiff) generateColumnSQL(tableSchema, tableName string, targetSch
3838
// because PostgreSQL cannot implicitly cast these types
3939
if needsUsing {
4040
sql := fmt.Sprintf("ALTER TABLE %s ALTER COLUMN %s TYPE %s USING %s::%s;",
41-
qualifiedTableName, cd.New.Name, newType, cd.New.Name, newType)
41+
qualifiedTableName, ir.QuoteIdentifier(cd.New.Name), newType, ir.QuoteIdentifier(cd.New.Name), newType)
4242
statements = append(statements, sql)
4343
} else {
4444
sql := fmt.Sprintf("ALTER TABLE %s ALTER COLUMN %s TYPE %s;",
45-
qualifiedTableName, cd.New.Name, newType)
45+
qualifiedTableName, ir.QuoteIdentifier(cd.New.Name), newType)
4646
statements = append(statements, sql)
4747
}
4848
}
@@ -52,12 +52,12 @@ func (cd *ColumnDiff) generateColumnSQL(tableSchema, tableName string, targetSch
5252
if cd.New.IsNullable {
5353
// DROP NOT NULL
5454
sql := fmt.Sprintf("ALTER TABLE %s ALTER COLUMN %s DROP NOT NULL;",
55-
qualifiedTableName, cd.New.Name)
55+
qualifiedTableName, ir.QuoteIdentifier(cd.New.Name))
5656
statements = append(statements, sql)
5757
} else {
5858
// ADD NOT NULL - generate canonical SQL only
5959
sql := fmt.Sprintf("ALTER TABLE %s ALTER COLUMN %s SET NOT NULL;",
60-
qualifiedTableName, cd.New.Name)
60+
qualifiedTableName, ir.QuoteIdentifier(cd.New.Name))
6161
statements = append(statements, sql)
6262
}
6363
}
@@ -69,7 +69,7 @@ func (cd *ColumnDiff) generateColumnSQL(tableSchema, tableName string, targetSch
6969
// Default was dropped above; add new default if specified
7070
if newDefault != nil && *newDefault != "" {
7171
sql := fmt.Sprintf("ALTER TABLE %s ALTER COLUMN %s SET DEFAULT %s;",
72-
qualifiedTableName, cd.New.Name, *newDefault)
72+
qualifiedTableName, ir.QuoteIdentifier(cd.New.Name), *newDefault)
7373
statements = append(statements, sql)
7474
}
7575
} else {
@@ -80,10 +80,10 @@ func (cd *ColumnDiff) generateColumnSQL(tableSchema, tableName string, targetSch
8080
var sql string
8181
if newDefault == nil {
8282
sql = fmt.Sprintf("ALTER TABLE %s ALTER COLUMN %s DROP DEFAULT;",
83-
qualifiedTableName, cd.New.Name)
83+
qualifiedTableName, ir.QuoteIdentifier(cd.New.Name))
8484
} else {
8585
sql = fmt.Sprintf("ALTER TABLE %s ALTER COLUMN %s SET DEFAULT %s;",
86-
qualifiedTableName, cd.New.Name, *newDefault)
86+
qualifiedTableName, ir.QuoteIdentifier(cd.New.Name), *newDefault)
8787
}
8888

8989
statements = append(statements, sql)

internal/diff/table.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ func generateCreateTablesSQL(
437437
for _, column := range table.Columns {
438438
if column.Comment != "" {
439439
tableName := qualifyEntityName(table.Schema, table.Name, targetSchema)
440-
sql := fmt.Sprintf("COMMENT ON COLUMN %s.%s IS %s;", tableName, column.Name, quoteString(column.Comment))
440+
sql := fmt.Sprintf("COMMENT ON COLUMN %s.%s IS %s;", tableName, ir.QuoteIdentifier(column.Name), quoteString(column.Comment))
441441

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

11711171
context := &diffContext{
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
COMMENT ON COLUMN ex."ID" IS 'Primary identifier';
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE public.ex (
2+
"ID" integer NOT NULL
3+
);
4+
5+
COMMENT ON COLUMN ex."ID" IS 'Primary identifier';
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CREATE TABLE public.ex (
2+
"ID" integer NOT NULL
3+
);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"version": "1.0.0",
3+
"pgschema_version": "1.6.1",
4+
"created_at": "1970-01-01T00:00:00Z",
5+
"source_fingerprint": {
6+
"hash": "9d443bc536153eed8fce077bfacc3d7f42b1a94f02d33868bb78be3b9de05088"
7+
},
8+
"groups": [
9+
{
10+
"steps": [
11+
{
12+
"sql": "COMMENT ON COLUMN ex.\"ID\" IS 'Primary identifier';",
13+
"type": "table.column.comment",
14+
"operation": "alter",
15+
"path": "public.ex.ID"
16+
}
17+
]
18+
}
19+
]
20+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
COMMENT ON COLUMN ex."ID" IS 'Primary identifier';
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Plan: 1 to modify.
2+
3+
Summary by type:
4+
tables: 1 to modify
5+
6+
Tables:
7+
~ ex
8+
~ ID (column.comment)
9+
10+
DDL to be executed:
11+
--------------------------------------------------
12+
13+
COMMENT ON COLUMN ex."ID" IS 'Primary identifier';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE ex ALTER COLUMN "ID" TYPE bigint;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CREATE TABLE public.ex (
2+
"ID" bigint NOT NULL
3+
);

0 commit comments

Comments
 (0)