diff --git a/main.go b/main.go index 9ebffe6..f1781ef 100644 --- a/main.go +++ b/main.go @@ -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.") @@ -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 != "" { diff --git a/pgdump/column.go b/pgdump/column.go index afb2842..29a96e6 100644 --- a/pgdump/column.go +++ b/pgdump/column.go @@ -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 diff --git a/pgdump/pgdump.go b/pgdump/pgdump.go index 9ba1dcf..5a90abb 100644 --- a/pgdump/pgdump.go +++ b/pgdump/pgdump.go @@ -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 diff --git a/pgdump/statement.go b/pgdump/statement.go index cfb0dae..7a13d68 100644 --- a/pgdump/statement.go +++ b/pgdump/statement.go @@ -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 {