Skip to content

Commit 0080321

Browse files
murfffikenshaw
authored andcommitted
Fix #321: postgres and pgx support schema name in table destination for /copy
1 parent 813645c commit 0080321

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

drivers/drivers_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,15 @@ func TestCopy(t *testing.T) {
422422
src: "select * from staff",
423423
dest: "staff_copy",
424424
},
425+
{
426+
dbName: "pgsql",
427+
setupQueries: []setupQuery{
428+
{query: "DROP TABLE staff_copy"},
429+
{query: "CREATE TABLE staff_copy AS SELECT * FROM staff WHERE 0=1", check: true},
430+
},
431+
src: "select * from staff",
432+
dest: "public.staff_copy",
433+
},
425434
{
426435
dbName: "pgx",
427436
setupQueries: []setupQuery{
@@ -431,6 +440,15 @@ func TestCopy(t *testing.T) {
431440
src: "select * from staff",
432441
dest: "staff_copy",
433442
},
443+
{
444+
dbName: "pgx",
445+
setupQueries: []setupQuery{
446+
{query: "DROP TABLE staff_copy"},
447+
{query: "CREATE TABLE staff_copy AS SELECT * FROM staff WHERE 0=1", check: true},
448+
},
449+
src: "select * from staff",
450+
dest: "public.staff_copy",
451+
},
434452
{
435453
dbName: "mysql",
436454
setupQueries: []setupQuery{

drivers/pgx/pgx.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func init() {
122122
var n int64
123123
err = conn.Raw(func(driverConn interface{}) error {
124124
conn := driverConn.(*stdlib.Conn).Conn()
125-
n, err = conn.CopyFrom(ctx, pgx.Identifier{table}, columns, crows)
125+
n, err = conn.CopyFrom(ctx, pgx.Identifier(strings.SplitN(table, ".", 2)), columns, crows)
126126
return err
127127
})
128128
return n, err
@@ -141,7 +141,11 @@ func (r *copyRows) Next() bool {
141141

142142
func (r *copyRows) Values() ([]interface{}, error) {
143143
err := r.rows.Scan(r.values...)
144-
return r.values, err
144+
actuals := make([]interface{}, len(r.values))
145+
for i, v := range r.values {
146+
actuals[i] = *(v.(*interface{}))
147+
}
148+
return actuals, err
145149
}
146150

147151
func (r *copyRows) Err() error {

drivers/postgres/postgres.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,11 @@ func init() {
137137
if err != nil {
138138
return 0, fmt.Errorf("failed to fetch target table columns: %w", err)
139139
}
140-
query = pq.CopyIn(table, columns...)
140+
if schemaSep := strings.Index(table, "."); schemaSep >= 0 {
141+
query = pq.CopyInSchema(table[:schemaSep], table[schemaSep+1:], columns...)
142+
} else {
143+
query = pq.CopyIn(table, columns...)
144+
}
141145
}
142146
tx, err := db.BeginTx(ctx, nil)
143147
if err != nil {

0 commit comments

Comments
 (0)