mirror of
https://github.com/alexgo-io/gaze-indexer.git
synced 2026-01-12 08:34:28 +08:00
* feat: recover nodesale module. * fix: refactored. * fix: fix table type. * fix: add entity * fix: bug UTC time. * ci: try to tidy before testing * ci: touch result file * ci: use echo to create new file * fix: try to skip test in ci * fix: remove os.Exit * fix: handle error * feat: add todo note * fix: Cannot run nodesale test because qtx is not initiated. * fix: 50% chance public key compare incorrectly. * fix: more consistent SQL * fix: sanity refactor. * fix: remove unused code. * fix: move last_block_default to config file. * fix: minor mistakes. * fix: * fix: refactor * fix: refactor * fix: delegate tx hash not record into db. * refactor: prepare for moving integration tests. * refactor: convert to unit tests. * fix: change to using input values since output values deducted fee. * feat: add extra unit test. * fix: wrong timestamp format. * fix: handle block timeout = 0 --------- Co-authored-by: Gaze <gazenw@users.noreply.github.com>
63 lines
1.4 KiB
Go
63 lines
1.4 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/cockroachdb/errors"
|
|
"github.com/gaze-network/indexer-network/modules/nodesale/datagateway"
|
|
"github.com/gaze-network/indexer-network/pkg/logger"
|
|
"github.com/jackc/pgx/v5"
|
|
)
|
|
|
|
var ErrTxAlreadyExists = errors.New("Transaction already exists. Call Commit() or Rollback() first.")
|
|
|
|
func (r *Repository) begin(ctx context.Context) (*Repository, error) {
|
|
if r.tx != nil {
|
|
return nil, errors.WithStack(ErrTxAlreadyExists)
|
|
}
|
|
tx, err := r.db.Begin(ctx)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "failed to begin transaction")
|
|
}
|
|
return &Repository{
|
|
db: r.db,
|
|
queries: r.queries.WithTx(tx),
|
|
tx: tx,
|
|
}, nil
|
|
}
|
|
|
|
func (r *Repository) BeginNodeSaleTx(ctx context.Context) (datagateway.NodeSaleDataGatewayWithTx, error) {
|
|
repo, err := r.begin(ctx)
|
|
if err != nil {
|
|
return nil, errors.WithStack(err)
|
|
}
|
|
return repo, nil
|
|
}
|
|
|
|
func (r *Repository) Commit(ctx context.Context) error {
|
|
if r.tx == nil {
|
|
return nil
|
|
}
|
|
err := r.tx.Commit(ctx)
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to commit transaction")
|
|
}
|
|
r.tx = nil
|
|
return nil
|
|
}
|
|
|
|
func (r *Repository) Rollback(ctx context.Context) error {
|
|
if r.tx == nil {
|
|
return nil
|
|
}
|
|
err := r.tx.Rollback(ctx)
|
|
if err != nil && !errors.Is(err, pgx.ErrTxClosed) {
|
|
return errors.Wrap(err, "failed to rollback transaction")
|
|
}
|
|
if err == nil {
|
|
logger.DebugContext(ctx, "rolled back transaction")
|
|
}
|
|
r.tx = nil
|
|
return nil
|
|
}
|