Skip to content

Quote publication names as identifiers in SQL statements #528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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: 5 additions & 6 deletions postgresql/resource_postgresql_publication.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func setPubOwner(txn *sql.Tx, d *schema.ResourceData) error {
n := nraw.(string)
pubName := d.Get(pubNameAttr).(string)

sql := fmt.Sprintf("ALTER PUBLICATION %s OWNER TO %s", pubName, n)
sql := fmt.Sprintf("ALTER PUBLICATION %s OWNER TO %s", pq.QuoteIdentifier(pubName), n)
if _, err := txn.Exec(sql); err != nil {
return fmt.Errorf("Error updating publication owner: %w", err)
}
Expand All @@ -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, quoteTableName(p.(string)))
query := fmt.Sprintf("ALTER PUBLICATION %s ADD TABLE %s", pq.QuoteIdentifier(pubName), quoteTableName(p.(string)))
queries = append(queries, query)
}

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

Expand All @@ -202,13 +202,12 @@ func setPubTables(txn *sql.Tx, d *schema.ResourceData) error {

func setPubParams(txn *sql.Tx, d *schema.ResourceData, pubViaRootEnabled bool) error {
pubName := d.Get(pubNameAttr).(string)
paramAlterTemplate := "ALTER PUBLICATION %s %s"
publicationParametersString, err := getPublicationParameters(d, pubViaRootEnabled)
if err != nil {
return fmt.Errorf("Error getting publication parameters: %w", err)
}
if publicationParametersString != "" {
sql := fmt.Sprintf(paramAlterTemplate, pubName, publicationParametersString)
sql := fmt.Sprintf("ALTER PUBLICATION %s %s", pq.QuoteIdentifier(pubName), publicationParametersString)
if _, err := txn.Exec(sql); err != nil {
return fmt.Errorf("Error updating publication parameters: %w", err)
}
Expand Down Expand Up @@ -240,7 +239,7 @@ func resourcePostgreSQLPublicationCreate(db *DBConnection, d *schema.ResourceDat
}
defer deferredRollback(txn)

sql := fmt.Sprintf("CREATE PUBLICATION %s %s %s", name, tables, publicationParameters)
sql := fmt.Sprintf("CREATE PUBLICATION %s %s %s", pq.QuoteIdentifier(name), tables, publicationParameters)

if _, err := txn.Exec(sql); err != nil {
return fmt.Errorf("Error creating Publication: %w", err)
Expand Down