Skip to content

Commit 65171ff

Browse files
authored
fix: quote table name in postgresql_publication (#402)
1 parent 926a6e8 commit 65171ff

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

postgresql/helpers.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,3 +606,16 @@ func findStringSubmatchMap(expression string, text string) map[string]string {
606606
func defaultDiffSuppressFunc(k, old, new string, d *schema.ResourceData) bool {
607607
return old == new
608608
}
609+
610+
// quoteTable can quote a table name with or without a schema prefix
611+
// Example:
612+
//
613+
// my_table -> "my_table"
614+
// public.my_table -> "public"."my_table"
615+
func quoteTableName(tableName string) string {
616+
parts := strings.Split(tableName, ".")
617+
for i := range parts {
618+
parts[i] = pq.QuoteIdentifier(parts[i])
619+
}
620+
return strings.Join(parts, ".")
621+
}

postgresql/helpers_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,31 @@ func TestFindStringSubmatchMap(t *testing.T) {
1717
},
1818
)
1919
}
20+
21+
func TestQuoteTableName(t *testing.T) {
22+
tests := []struct {
23+
name string
24+
input string
25+
expected string
26+
}{
27+
{
28+
name: "simple table name",
29+
input: "users",
30+
expected: `"users"`,
31+
},
32+
{
33+
name: "table name with schema",
34+
input: "test.users",
35+
expected: `"test"."users"`,
36+
},
37+
}
38+
39+
for _, tt := range tests {
40+
t.Run(tt.name, func(t *testing.T) {
41+
actual := quoteTableName(tt.input)
42+
if actual != tt.expected {
43+
t.Errorf("quoteTableName() = %v, want %v", actual, tt.expected)
44+
}
45+
})
46+
}
47+
}

postgresql/resource_postgresql_publication.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,12 @@ func setPubTables(txn *sql.Tx, d *schema.ResourceData) error {
183183
added := arrayDifference(newList, oldList)
184184

185185
for _, p := range added {
186-
query := fmt.Sprintf("ALTER PUBLICATION %s ADD TABLE %s", pubName, p.(string))
186+
query := fmt.Sprintf("ALTER PUBLICATION %s ADD TABLE %s", pubName, quoteTableName(p.(string)))
187187
queries = append(queries, query)
188188
}
189189

190190
for _, p := range dropped {
191-
query := fmt.Sprintf("ALTER PUBLICATION %s DROP TABLE %s", pubName, p.(string))
191+
query := fmt.Sprintf("ALTER PUBLICATION %s DROP TABLE %s", pubName, quoteTableName(p.(string)))
192192
queries = append(queries, query)
193193
}
194194

@@ -461,7 +461,7 @@ func getTablesForPublication(d *schema.ResourceData) (string, error) {
461461
return tablesString, fmt.Errorf("'%s' is duplicated for attribute `%s`", elem.(string), pubTablesAttr)
462462
}
463463
for _, t := range tables {
464-
tlist = append(tlist, t.(string))
464+
tlist = append(tlist, quoteTableName(t.(string)))
465465
}
466466
tablesString = fmt.Sprintf("FOR TABLE %s", strings.Join(tlist, ", "))
467467
}

0 commit comments

Comments
 (0)