Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func main() {
sourceTable := flag.String("from", "", "Source table to dump.")
destTable := flag.String("to", "", "Table name to use in INSERT statements. Defaults to the source table.")
insert := flag.String("insert-columns", "", "Comma-separated list of columns to include in INSERT statement. Defaults to all columns.")
skipcolnamesinsert := flag.Bool("skip-colnames-insert", false, "Skip the column names at the INSERT statements and only use VALUES.")
conflict := flag.String("conflict-column", "", "Append an ON CONFLICT clause for this column. All other columns will be included in a DO UPDATE SET list.")
noconflict := flag.Bool("noconflict", false, "Append ON CONFLICT DO NOTHING.")
tx := flag.Bool("tx", false, "Wrap INSERT statements in transaction.")
Expand All @@ -40,6 +41,7 @@ func main() {
opts.Query = strings.TrimSpace(*query)
opts.ConflictColumn = strings.TrimSpace(*conflict)
opts.NoConflict = *noconflict
opts.SkipColnamesInsert = *skipcolnamesinsert
opts.Verbose = *verbose

if *insert != "" {
Expand Down
2 changes: 1 addition & 1 deletion pgdump/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func getColumns(q Querier, table string, opts *Options) ([]column, error) {
LEFT OUTER JOIN information_schema.element_types e
ON (c.table_catalog, c.table_schema, c.table_name, 'TABLE', c.dtd_identifier)
= (e.object_catalog, e.object_schema, e.object_name, e.object_type, e.collection_type_identifier)
WHERE table_name = $1 AND c.is_generated = 'NEVER'
WHERE c.table_schema||'.'||c.table_name = $1 AND c.is_generated = 'NEVER'
`, table)
if err != nil {
return nil, err
Expand Down
3 changes: 3 additions & 0 deletions pgdump/pgdump.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ type Options struct {
// Which columns to include in INSERT statement. Defaults to all columns if empty.
InsertColumns []string

// Skip the column names at the INSERT statements and only use VALUES
SkipColnamesInsert bool

// Append a ON CONFLICT clause for this column. All other columns will end
// up in a DO UPDATE SET list.
ConflictColumn string
Expand Down
26 changes: 16 additions & 10 deletions pgdump/statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,28 @@ func getQueryStatement(table string, cols []column) string {
func getInsertStatement(table string, cols []column, opts *Options) string {
var buf bytes.Buffer

buf.WriteString("INSERT INTO " + table + " (")
count := 0
buf.WriteString("INSERT INTO " + table)

for _, col := range cols {
if col.insert {
if count > 0 {
buf.WriteString(", ")
if !opts.SkipColnamesInsert {
buf.WriteString(" (")

count := 0
for _, col := range cols {
if col.insert {
if count > 0 {
buf.WriteString(", ")
}
buf.WriteString(quoteColumn(col.Name))
count++
}
buf.WriteString(quoteColumn(col.Name))
count++
}

buf.WriteString(")")
}

buf.WriteString(") VALUES (")
count = 0
buf.WriteString(" VALUES (")

count := 0
for _, col := range cols {
if col.insert {
if count > 0 {
Expand Down
Loading