Skip to content
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

[rds-postgres] - The Execution Fails when Table name is in camel case #147

Merged
merged 10 commits into from
Feb 13, 2023
Next Next commit
fix: Remove all quotes in table name
  • Loading branch information
lucass4 committed Jan 26, 2023
commit 053d2fdc6ac0692082e8cc92abb41fb496e42c1e
13 changes: 9 additions & 4 deletions pkg/dumper/postgres/dumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"database/sql"
"fmt"
"strconv"
"strings"

"github.com/lib/pq"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -111,9 +112,11 @@ func (d *pgDumper) PreDumpTables(tables []string) error {
if err := rows.Scan(&fk.tableName, &fk.constraintName, &fk.constraintDefinition); err != nil {
return fmt.Errorf("failed to load ForeignKeyInfo: %w", err)
}
query := fmt.Sprintf("ALTER TABLE %s DROP CONSTRAINT %s", strconv.Quote(fk.tableName), strconv.Quote(fk.constraintName))

tableName := strings.ReplaceAll(fk.tableName, "\"", "")
query := fmt.Sprintf("ALTER TABLE %s DROP CONSTRAINT %s", strconv.Quote(tableName), strconv.Quote(fk.constraintName))
if _, err := d.conn.Exec(query); err != nil {
return fmt.Errorf("failed to frop contraint %s.%s: %w", fk.tableName, fk.constraintName, err)
return fmt.Errorf("failed to drop constraint %s.%s: %w", fk.tableName, fk.constraintName, err)
}
d.foreignKeys = append(d.foreignKeys, fk)
}
Expand All @@ -126,7 +129,8 @@ func (d *pgDumper) PostDumpTables(tables []string) error {
if !d.isRDS {
log.Debug("Reenabling triggers")
for _, tbl := range tables {
query := fmt.Sprintf("ALTER TABLE %s ENABLE TRIGGER ALL", strconv.Quote(tbl))
tableName := strings.ReplaceAll(tbl, "\"", "")
query := fmt.Sprintf("ALTER TABLE %s ENABLE TRIGGER ALL", strconv.Quote(tableName))
if _, err := d.conn.Exec(query); err != nil {
return fmt.Errorf("failed to enable triggers for %s: %w", tbl, err)
}
Expand All @@ -136,7 +140,8 @@ func (d *pgDumper) PostDumpTables(tables []string) error {

log.Debug("Recreating foreign keys")
for _, fk := range d.foreignKeys {
query := fmt.Sprintf("ALTER TABLE %s ADD CONSTRAINT %s %s", strconv.Quote(fk.tableName), strconv.Quote(fk.constraintName), fk.constraintDefinition)
tableName := strings.ReplaceAll(fk.tableName, "\"", "")
query := fmt.Sprintf("ALTER TABLE %s ADD CONSTRAINT %s %s", strconv.Quote(tableName), strconv.Quote(fk.constraintName), fk.constraintDefinition)
if _, err := d.conn.Exec(query); err != nil {
return fmt.Errorf("failed to re-create ForeignKey %s.%s: %w", fk.tableName, fk.constraintName, err)
}
Expand Down