multi: add a re-usable TxOptions type

Add a re-usable implementation of the sqldb.TxOptions interface and make
use of this in the various spots (invoices, batch and graph/db) where we
had previously defined individual implementations that were all doing
the same thing.
This commit is contained in:
Elle Mouton
2025-05-27 18:53:31 +02:00
parent 8e96bd0308
commit c4e6f23c5b
9 changed files with 79 additions and 134 deletions

View File

@@ -65,9 +65,7 @@ func (s *TimeScheduler[Q]) Execute(ctx context.Context, r *Request[Q]) error {
// By default, we assume that the batch is read-only,
// and we only upgrade it to read-write if a request
// is added that is not read-only.
txOpts: txOpts{
readOnly: true,
},
txOpts: sqldb.ReadTxOpt(),
}
trigger := s.b.trigger
time.AfterFunc(s.duration, func() {
@@ -78,8 +76,8 @@ func (s *TimeScheduler[Q]) Execute(ctx context.Context, r *Request[Q]) error {
// We only upgrade the batch to read-write if the new request is not
// read-only. If it is already read-write, we don't need to do anything.
if s.b.txOpts.readOnly && !r.Opts.ReadOnly {
s.b.txOpts.readOnly = false
if s.b.txOpts.ReadOnly() && !r.Opts.ReadOnly {
s.b.txOpts = sqldb.WriteTxOpt()
}
// If this is a non-lazy request, we'll execute the batch immediately.
@@ -109,7 +107,7 @@ func (s *TimeScheduler[Q]) Execute(ctx context.Context, r *Request[Q]) error {
}
// Otherwise, run the request on its own.
commitErr := s.db.ExecTx(ctx, &txOpts, func(tx Q) error {
commitErr := s.db.ExecTx(ctx, txOpts, func(tx Q) error {
return req.Do(tx)
}, func() {
if req.Reset != nil {