feat: add assessment, privacy and support flows
This commit is contained in:
1 parent
ee3738ee73
commit
e4f24fa202
339 files changed
+56454
-1126
No files matched your search
@@ -37,6 +37,7 @@ func NewTransactor(pool *pgxpool.Pool) *Transactor {
|
||||
}
|
||||
|
||||
var _ outbound.Transactor = (*Transactor)(nil)
|
||||
var _ outbound.ReadOnlySnapshotTransactor = (*Transactor)(nil)
|
||||
|
||||
// transactionContextKey marks a context passed to a transaction callback so a
|
||||
// nested WithinTransaction call fails instead of opening a second connection.
|
||||
@@ -81,6 +82,42 @@ func (transactor *Transactor) WithinTransaction(
|
||||
return nil
|
||||
}
|
||||
|
||||
// WithinReadOnlySnapshot runs a callback against one repeatable-read snapshot.
|
||||
// PostgreSQL rejects writes made through the supplied UnitOfWork.
|
||||
func (transactor *Transactor) WithinReadOnlySnapshot(
|
||||
ctx context.Context,
|
||||
fn func(ctx context.Context, unit outbound.UnitOfWork) error,
|
||||
) error {
|
||||
if ctx.Value(transactionContextKey{}) != nil {
|
||||
return fmt.Errorf("Transactor.WithinReadOnlySnapshot - begin transaction: %w", outbound.ErrNestedTransaction)
|
||||
}
|
||||
options := pgx.TxOptions{IsoLevel: pgx.RepeatableRead, AccessMode: pgx.ReadOnly}
|
||||
tx, err := transactor.pool.BeginTx(ctx, options)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Transactor.WithinReadOnlySnapshot - begin transaction: %w", sanitizeConnectError(err))
|
||||
}
|
||||
finished := false
|
||||
defer func() {
|
||||
if !finished {
|
||||
_ = rollback(ctx, tx)
|
||||
}
|
||||
}()
|
||||
|
||||
fnErr := fn(context.WithValue(ctx, transactionContextKey{}, true), &UnitOfWork{tx: tx, queries: sqlc.New(tx)})
|
||||
finished = true
|
||||
if fnErr != nil {
|
||||
fnErr = markTransactionConflict(fnErr)
|
||||
if rollbackErr := rollback(ctx, tx); rollbackErr != nil {
|
||||
return errors.Join(fnErr, fmt.Errorf("Transactor.WithinReadOnlySnapshot - roll back transaction: %w", rollbackErr))
|
||||
}
|
||||
return fnErr
|
||||
}
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return fmt.Errorf("Transactor.WithinReadOnlySnapshot - commit transaction: %w", markTransactionConflict(err))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func rollback(ctx context.Context, tx pgx.Tx) error {
|
||||
rollbackContext, cancel := context.WithTimeout(context.WithoutCancel(ctx), rollbackTimeout)
|
||||
defer cancel()
|
||||
|
||||
Reference in new issue
Block a user