Skip to content

Commit

Permalink
Allow support for Table names in camel case (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucass4 authored Feb 13, 2023
1 parent 3d992e3 commit 18f1617
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
6 changes: 6 additions & 0 deletions fixtures/pg_simple.sql
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ CREATE TABLE "order_items" (
FOREIGN KEY (order_id) REFERENCES orders(id)
);

CREATE TABLE "OrderItems" (
id UUID PRIMARY KEY NOT NULL,
order_id UUID NOT NULL,
created_at timestamp,
FOREIGN KEY (order_id) REFERENCES orders(id)
);

CREATE VIEW "users_view" AS SELECT id, username FROM "users" WHERE active=true;

Expand Down
12 changes: 6 additions & 6 deletions pkg/dumper/postgres/dumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package postgres
import (
"database/sql"
"fmt"
"strconv"
"strings"

"github.com/lib/pq"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -85,7 +85,7 @@ func (d *pgDumper) PreDumpTables(tables []string) error {
if !d.isRDS {
log.Debug("Disabling triggers")
for _, tbl := range tables {
query := fmt.Sprintf("ALTER TABLE %s DISABLE TRIGGER ALL", strconv.Quote(tbl))
query := fmt.Sprintf("ALTER TABLE %q DISABLE TRIGGER ALL", strings.Trim(tbl, "\""))
if _, err := d.conn.Exec(query); err != nil {
return fmt.Errorf("failed to disable triggers for %s: %w", tbl, err)
}
Expand All @@ -111,9 +111,9 @@ 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))
query := fmt.Sprintf("ALTER TABLE %q DROP CONSTRAINT %q", strings.Trim(fk.tableName, "\""), strings.Trim(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 +126,7 @@ 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))
query := fmt.Sprintf("ALTER TABLE %q ENABLE TRIGGER ALL", strings.Trim(tbl, "\""))
if _, err := d.conn.Exec(query); err != nil {
return fmt.Errorf("failed to enable triggers for %s: %w", tbl, err)
}
Expand All @@ -136,7 +136,7 @@ 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)
query := fmt.Sprintf("ALTER TABLE %q ADD CONSTRAINT %q %s", strings.Trim(fk.tableName, "\""), strings.Trim(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

0 comments on commit 18f1617

Please sign in to comment.