infra
infra copied to clipboard
Isolate all references to gorm in the data package
In preparation for removing gorm and replacing it with database/sql (or possibly sqlx/sqlc) we need to make sure that the data package is the only thing that is aware of gorm.
Once that is done, it should be much easier to change the library we use for interacting with the database.
Tasks:
- [x] audit
database/sql, kyleconroy/sqlc, and jmoiron/sqlx, specially how they deal with transactions to make sure that they all fit well with the new proposed interface for the data package (described in proposal 18) - [x] create a
RequestContextto expose a transaction (proposal 19), and store it ingin.Contexthttps://github.com/infrahq/infra/pull/2702 - [ ] convert a small number of
dataquery functions to the new pattern, and call them from the handlers using the transaction fromRequestContext#2657 - [ ] if everything looks good, repeat for all the other
dataquery functions
Follow ups
- [x] ~decide on
database/sql, kyleconroy/sqlc, or jmoiron/sqlx~ stdlib should be sufficient - [ ] convert one of the more involved queries to use the new library
- [x] find a replacement for gormigrate - forked and inlined https://github.com/infrahq/infra/pull/2722
Notes from task 1
I looked at these:
-
database/sql#Tx - queries are performed on a
Txtype, that usesExecfor writes, andQueryfor selects -
jmoiron/sqlx#Tx - embeds
sql.Txso the transaction has roughly the same shape -
kyleconroy/sqlc - does not itself implement a
Tx, but does generate transaction interfaces that are satisfied by the stdlibTx -
lib/pq - is a driver for the stdlib
database/sql, so does not have its own concept of a transaction -
jackc/pgx#Tx - has two modes, one is a driver for
database/sql(which would use the stdlibTx), the other is its own library that makes use of postgres binary protocol. Its own mode has aTxthat is very similar to the stdlibTx.
So it seems that whatever we choose to replace gorm should fit well into the interface described in https://github.com/infrahq/internal/issues/18