Describe the bug
Hi, I am trying to move data from mongodb to postgres using pgx copy from however I am getting an error and the copy count = 0 ERROR: 'with' expected (SQLSTATE 00000)
To Reproduce
Steps to reproduce the behavior:
If possible, please provide runnable example such as:
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/jackc/pgx/v5"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)
type Category struct {
ID primitive.ObjectID `json:"_id,omitempty" bson:"_id,omitempty"`
Name struct {
En string `json:"en"`
Ar string `json:"ar"`
} `json:"name"`
Company struct {
Name string `json:"name"`
ID string `json:"_id"`
} `json:"company"`
CompanyRef primitive.ObjectID `json:"companyRef"`
Image string `json:"image"`
Description string `json:"description"`
Status string `json:"status"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
func categories(client *mongo.Client, psql *pgx.Conn) {
db := client.Database("test")
collection := db.Collection("categories")
filter := bson.M{}
cursor, err := collection.Find(context.Background(), filter)
if err != nil {
log.Fatal(err)
}
var results []Category
if err = cursor.All(context.TODO(), &results); err != nil {
panic(err)
}
copyCount, err := psql.CopyFrom(
context.Background(),
pgx.Identifier{"categories"},
[]string{"categoryNameEn", "categoryNameAr", "companyNameEn", "companyRef", "description", "createdAt", "categoryRef"},
pgx.CopyFromSlice(len(results), func(i int) ([]any, error) {
return []any{results[i].Name.En, results[i].Name.Ar, results[i].Company.Name, results[i].CompanyRef.String(), results[i].Description, results[i].CreatedAt.Format(time.RFC3339), results[i].ID.Hex()}, nil
}),
)
fmt.Println(err, copyCount)
fmt.Println("Bulk insert completed successfully.")
}
Please run your example with the race detector enabled. For example, go run -race main.go or go test -race.
Expected behavior
Should be able to copy without any error
Actual behavior
Version
Go: $ go version -> go version go1.22.1 linux/amd64
PostgreSQL: $ psql --no-psqlrc --tuples-only -c 'select version()' -> [e.g. PostgreSQL 14.4 on x86_64-apple-darwin21.5.0, compiled by Apple clang version 13.1.6 (clang-1316.0.21.2.5), 64-bit]
Nothing jumps out at me. Try to isolate this example to a reproduction case that doesn't require Mongo. Perhaps manually construct the slice to pass to CopyFrom that demonstrates the problem.
Describe the bug Hi, I am trying to move data from mongodb to postgres using pgx copy from however I am getting an error and the copy count = 0 ERROR: 'with' expected (SQLSTATE 00000)
To Reproduce Steps to reproduce the behavior:
If possible, please provide runnable example such as:
Please run your example with the race detector enabled. For example,
go run -race main.go
orgo test -race
.Expected behavior Should be able to copy without any error
Actual behavior
Version
$ go version
-> go version go1.22.1 linux/amd64$ psql --no-psqlrc --tuples-only -c 'select version()'
-> [e.g. PostgreSQL 14.4 on x86_64-apple-darwin21.5.0, compiled by Apple clang version 13.1.6 (clang-1316.0.21.2.5), 64-bit]$ grep 'github.com/jackc/pgx/v[0-9]' go.mod
-> v5Additional context Add any other context about the problem here.