-
Hi All, Is there any way of passing struct as argument for psql function?
And this table, type and PSQL function:
Is there a way to do something like this?
The working SQL query is:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Yes, that should be possible. The custom type needs to be registered with pgx. See https://pkg.go.dev/github.com/jackc/pgx/v5@v5.7.1/pgtype#hdr-New_PostgreSQL_Type_Support and https://github.com/jackc/pgx/blob/master/pgtype/composite_test.go. |
Beta Was this translation helpful? Give feedback.
-
I had an issue where I was running my tests with dockertest. Spinning up postgres image in a docker container and running migrations and tests inside of it. I ended up setting up my pgxpool like this: func NewPgxPool(connString string) (*pgxpool.Pool, error) {
config, err := pgxpool.ParseConfig(connString)
if err != nil {
return nil, fmt.Errorf("unable to parse config: %v", err)
}
config.BeforeAcquire = func(ctx context.Context, conn *pgx.Conn) bool {
dataTypeNames := []string{
"my_type",
}
for _, typeName := range dataTypeNames {
dataType, err := conn.LoadType(ctx, typeName)
if err != nil {
continue
}
conn.TypeMap().RegisterType(dataType)
}
return true
}
conn, err := pgxpool.NewWithConfig(context.Background(), config)
if err != nil {
return nil, err
}
return conn, nil
} If someone is interested, rollback part of my tests would look something like this: for _, scenario := range scenarios {
t.Run(scenario.name, func(t *testing.T) {
tx, err := conn.Begin(ctx)
require.NoError(t, err)
defer tx.Rollback(ctx)
cabinets, err := scenario.setup(tx)
if scenario.expectedErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
assert.Len(t, cabinets, scenario.expectedLen)
})
} |
Beta Was this translation helpful? Give feedback.
Yes, that should be possible. The custom type needs to be registered with pgx.
See https://pkg.go.dev/github.com/jackc/pgx/v5@v5.7.1/pgtype#hdr-New_PostgreSQL_Type_Support and https://github.com/jackc/pgx/blob/master/pgtype/composite_test.go.