infra icon indicating copy to clipboard operation
infra copied to clipboard

Isolate all references to gorm in the data package

Open pdevine opened this issue 3 years ago • 1 comments

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 RequestContext to expose a transaction (proposal 19), and store it in gin.Context https://github.com/infrahq/infra/pull/2702
  • [ ] convert a small number of data query functions to the new pattern, and call them from the handlers using the transaction from RequestContext #2657
  • [ ] if everything looks good, repeat for all the other data query 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

pdevine avatar Jun 27 '22 16:06 pdevine

Notes from task 1

I looked at these:

  • database/sql#Tx - queries are performed on a Tx type, that uses Exec for writes, and Query for selects
  • jmoiron/sqlx#Tx - embeds sql.Tx so 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 stdlib Tx
  • 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 stdlib Tx), the other is its own library that makes use of postgres binary protocol. Its own mode has a Tx that is very similar to the stdlib Tx.

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

dnephin avatar Jul 18 '22 22:07 dnephin