Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrilgdn authored Feb 24, 2024
2 parents 29d97ae + 65171ff commit dcd6988
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
13 changes: 13 additions & 0 deletions postgresql/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,3 +606,16 @@ func findStringSubmatchMap(expression string, text string) map[string]string {
func defaultDiffSuppressFunc(k, old, new string, d *schema.ResourceData) bool {
return old == new
}

// quoteTable can quote a table name with or without a schema prefix
// Example:
//
// my_table -> "my_table"
// public.my_table -> "public"."my_table"
func quoteTableName(tableName string) string {
parts := strings.Split(tableName, ".")
for i := range parts {
parts[i] = pq.QuoteIdentifier(parts[i])
}
return strings.Join(parts, ".")
}
28 changes: 28 additions & 0 deletions postgresql/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,31 @@ func TestFindStringSubmatchMap(t *testing.T) {
},
)
}

func TestQuoteTableName(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "simple table name",
input: "users",
expected: `"users"`,
},
{
name: "table name with schema",
input: "test.users",
expected: `"test"."users"`,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := quoteTableName(tt.input)
if actual != tt.expected {
t.Errorf("quoteTableName() = %v, want %v", actual, tt.expected)
}
})
}
}
6 changes: 3 additions & 3 deletions postgresql/resource_postgresql_publication.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,12 @@ func setPubTables(txn *sql.Tx, d *schema.ResourceData) error {
added := arrayDifference(newList, oldList)

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

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

Expand Down Expand Up @@ -461,7 +461,7 @@ func getTablesForPublication(d *schema.ResourceData) (string, error) {
return tablesString, fmt.Errorf("'%s' is duplicated for attribute `%s`", elem.(string), pubTablesAttr)
}
for _, t := range tables {
tlist = append(tlist, t.(string))
tlist = append(tlist, quoteTableName(t.(string)))
}
tablesString = fmt.Sprintf("FOR TABLE %s", strings.Join(tlist, ", "))
}
Expand Down
1 change: 1 addition & 0 deletions website/docs/index.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ The following arguments are supported:
* `clientcert` - (Optional) - Configure the SSL client certificate.
* `cert` - (Required) - The SSL client certificate file path. The file must contain PEM encoded data.
* `key` - (Required) - The SSL client certificate private key file path. The file must contain PEM encoded data.
* `sslinline` - (Optional) - If set to `true`, arguments accept inline ssl cert and key rather than a filename. Defaults to `false`.
* `sslrootcert` - (Optional) - The SSL server root certificate file path. The file must contain PEM encoded data.
* `connect_timeout` - (Optional) Maximum wait for connection, in seconds. The
default is `180s`. Zero or not specified means wait indefinitely.
Expand Down

0 comments on commit dcd6988

Please sign in to comment.