stephenafamo / bob

SQL query builder and ORM/Factory generator for Go with support for PostgreSQL, MySQL and SQLite
https://bob.stephenafamo.com
MIT License
701 stars 37 forks source link

broken netip.Addr scan support v0.25.1-0.20240516073824-99941dbd5078 #211

Closed gstarikov closed 1 month ago

gstarikov commented 1 month ago

Hi. trying to migrate from v0.22.1 to v0.25.1-0.20240516073824-99941dbd5078 and:

how to reproduce:

  1. take postgres 16.2
  2. create table create table n (addr inet );
  3. and just go run github.com/stephenafamo/bob/gen/bobgen-psql@v0.25.1-0.20240516073824-99941dbd5078 and there will be error - unsupported Scan
    
    Scan error on column index 0, name "addr": unsupported Scan, storing driver.Value type string into type *netip.Addr

rows, err := models.N.InsertMany(ctx, t.DB, n...)

stephenafamo commented 1 month ago

I see where this bug comes from. I somehow didn't take into account that netip.Addr does not implement the Scan interface.

I'll have to create some wrapper type for it 🤦🏾

stephenafamo commented 1 month ago

Should be fixed in v0.26.0. Let me know if it persists

gstarikov commented 1 month ago

nope. it is still there.

unsupported Scan, storing driver.Value type string into type *netip.Addr
// Network is an object representing the database table.
type Network struct {
    ID                    int64                `db:"id,pk,generated" `
    NetworkAddr           null.Val[netip.Addr] `db:"network_addr,generated" `
}
...
models.Networks.InsertMany(ctx, t.DB, networks...)

and here is created target to *null.Val[netip.Addr] https://github.com/stephenafamo/scan/blob/main/row.go#L68

stephenafamo commented 1 month ago

You have to regenerate the models. It should now use a wrapper type which can be scanned into

gstarikov commented 1 month ago

oh, it's my fault. I forgot to update the generator to 0.26. but with the new one (0.26) in the same place there is another problem.

Scan error on column index 5, name "network_addr": ParseAddr("10.11.0.0/24"): unexpected character (at "/24")

the postgres inet type can also handle subnets https://www.postgresql.org/docs/current/datatype-net-types.html#DATATYPE-INET

The inet type holds an IPv4 or IPv6 host address, and optionally its subnet, all in one field. The input format for this type is address/y where address is an IPv4 or IPv6 address and y is the number of bits in the netmask. If the /y portion is omitted, the netmask is taken to be 32 for IPv4 or 128 for IPv6, so the value represents just a single host. On display, the /y portion is suppressed if the netmask specifies a single host.

so its better to use netip.Prefix instead of netip.Addr

stephenafamo commented 1 month ago

so its better to use netip.Prefix instead of netip.Addr

Great suggestion 👍🏾

stephenafamo commented 1 month ago

This should now be fixed by #217.

Try it out and let me know.

gstarikov commented 1 month ago

Thanks a lot. everything is working. please tell me when to expect the tagged version?

stephenafamo commented 1 month ago

Tagged v0.26.1 now.