1
0
mirror of https://github.com/rancher/steve.git synced 2025-08-27 10:29:48 +00:00
steve/pkg/sqlcache/db/transaction/transaction.go
Silvio Moioli 772dc7577e
sql: use a closure to wrap transactions (#469)
This introduces the a `WithTransaction` function, which is then used for all transactional work in Steve.

Because `WithTransaction` takes care of all `Begin`s, `Commit`s and `Rollback`s, it eliminates the problem where forgotten open transactions can block all other operations (with long stalling and `SQLITE_BUSY` errors).

This also:

- merges together the disparate `DBClient` interfaces in one only `db.Client` interface with one unexported non-test implementation. I found this much easier to follow
- refactors the transaction package in order to make it as minimal as possible, and as close to the wrapped `sql.Tx` and `sql.Stmt` functions as possible, in order to reduce cognitive load when working with this part of the codebase
- simplifies tests accordingly
- adds a couple of known files to `.gitignore`
    
Credits to @tomleb for suggesting the approach: https://github.com/rancher/lasso/pull/121#pullrequestreview-2515872507
2025-02-05 10:05:52 +01:00

45 lines
1.1 KiB
Go

/*
Package transaction provides mockable interfaces of sql package struct types.
*/
package transaction
import (
"context"
"database/sql"
)
// Client is an interface over a subset of sql.Tx methods
// rationale 1: explicitly forbid direct access to Commit and Rollback functionality
// as that is exclusively dealt with by WithTransaction in ../db
// rationale 2: allow mocking
type Client interface {
Exec(query string, args ...any) (sql.Result, error)
Stmt(stmt *sql.Stmt) Stmt
}
// client is the main implementation of Client, delegates to sql.Tx
// other implementations exist for testing purposes
type client struct {
tx *sql.Tx
}
func NewClient(tx *sql.Tx) Client {
return &client{tx: tx}
}
func (c client) Exec(query string, args ...any) (sql.Result, error) {
return c.tx.Exec(query, args...)
}
func (c client) Stmt(stmt *sql.Stmt) Stmt {
return c.tx.Stmt(stmt)
}
// Stmt is an interface over a subset of sql.Stmt methods
// rationale: allow mocking
type Stmt interface {
Exec(args ...any) (sql.Result, error)
Query(args ...any) (*sql.Rows, error)
QueryContext(ctx context.Context, args ...any) (*sql.Rows, error)
}