-
Notifications
You must be signed in to change notification settings - Fork 962
feat(golang): support custom enum slices for pgx/v5 #2510
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
d7835a7 to
211955c
Compare
|
I forced-pushed a new commit with some fixes
func LoadTypes(ctx context.Context, conn *pgx.Conn) error {
for _, enum := range sqlcgeneratedpackage.EnumNames {
t, err := conn.LoadType(ctx, enum) // register enum
if err != nil {
return err
}
conn.TypeMap().RegisterType(t)
ta, err := conn.LoadType(ctx, "_"+enum) // register []enum
if err != nil {
return err
}
conn.TypeMap().RegisterType(ta)
}
return nil
}
// ...
poolCfg, err := pgxpool.ParseConfig(dsn)
if err != nil {
return nil, fmt.Errorf("could not parse db dsn: %w", err)
}
poolCfg.AfterConnect = LoadTypes |
87df5f0 to
391253a
Compare
|
So test pipeline is failing now due to pgx/v4 missing the pgtype import (I'll see how to cleanly add that one in). I'm mostly interested in the changes for pgx/v5 so I may split this PR into two if the import takes me more time than expected. Also I spotted that the |
b1bc478 to
535fc28
Compare
|
There we go, both issues are now fixed. I also rebased with latest main as the project now lives under the sqlc-dev umbrella (congrats!) and there were some conflicts due to imports being renamed. |
|
Thanks for this. It looks like a longstanding issue. I haven't dug in too deeply yet but at a high level I think we should avoid the pluralize dependency, and scope this down to just add support for But speaking of that, I don't actually see any calls to |
Hi Andrew! So, the It is true that I didn't update any examples or tests to actually prove it's being used. The pluralize dependency is only required for the pgx/v4 code and just to generate proper type names. As for the pgx/v5 specific code (the EnumNames string slice) ideally it would only be called withing examples and or tests as that's only meant to be used in application code as we don't know what sqlc users might want to do in their AfterConnect func. It's not really that useful, it's more like a DX thing as users can already build a list of enum type names by themselves, but it's handier if sqlc builds it for you. I think it may be probably best if I split this PR into two with updated tests and example code showcasing it's usage. |
Yep understood. We just really don't want to add more dependencies unless it's strictly necessary. And in this case it is a nice-to-have rather than need-to-have I think.
Yeah I think splitting into one thing that minimally gets things working for |
535fc28 to
de155ac
Compare
|
@andrewmbenton Done, I just force pushed a new commit with just the pgx/v5 changes. There's a new |
de155ac to
b029dec
Compare
|
Rebased with latest |
b029dec to
8d49158
Compare
|
/cc @andrewmbenton Did you have time to check the pgx/v5 specific changes? Am I missing something? |
|
Gentle ping @andrewmbenton Haven't heard from anybody since Aug 5, are there any blockers for this? |
|
Thanks for the prod, just coming back to this. I think I'm still a little confused about what the best solution is for pgx/v5. If the only thing Is it not the case that sqlc could generate a proper encoding/scanning implementation for each type, such that registration wouldn't be necessary at all and enum slices would just work? I can see that it's not recommended ("While pgtype will often still work with unregistered types it is highly recommended that all types be registered due to an improvement in performance and the elimination of certain edge cases."), but I'm wondering if generating the encoding/scanning is a plausible path forward? If the only proper solution for pgx/v5 is to load and register the enum types before querying, then I think I wouldn't recommend changing |
|
@andrewmbenton think it would be an overreach for sqlc to set up a hook or something to call from pgx to register these types? It's easy enough to write a load/register function, but it could be convenient if sqlc would set these up for you (based on the driver you're using). You'd still have to know to call this when connecting to your DB of course. |
Not necessarily an overreach, just a question of whether the value gained is worth the addition of more go templates. If all we are doing is adding a list of custom type names to a go package so that users can more-easily iterate over the list then it doesn't really seem too valuable. But if we provided the entire |
|
Also see code in #2116 |
Currently queries that use arrays of custom enum types fail (in both pgx/v4 & pgx/v5) as shown in #1256
The error looks like this:
failed to encode args[1]: unable to encode []MyCustomEnumType{\"some_value\"} into text format for unknown type (OID 16877): cannot find encode planThere's a somewhat easy solution to this issue by modifying the generated code for custom enum types to also generate implementations of EncodeText and DecodeText.
Example: #1256 (comment)
I didn't see any pluralize helper func in the codegen/golang folder so I'm using
{{.Name}}sas the type name, better alternatives are welcomed!