From e2b864baa815a7edbb07cc25009d8c8143eea65c Mon Sep 17 00:00:00 2001 From: yaoandrew <1547809+yaoandrew@users.noreply.github.com> Date: Thu, 20 Dec 2018 20:29:48 -0800 Subject: [PATCH] Add rollback if GetTicInTx fails and move GetTicInTx to shared/repository.go --- pkg/transformers/dent/repository.go | 4 ++++ pkg/transformers/shared/repository.go | 12 ++++++++++++ pkg/transformers/shared/utilities.go | 13 ------------- pkg/transformers/tend/repository.go | 4 ++++ 4 files changed, 20 insertions(+), 13 deletions(-) diff --git a/pkg/transformers/dent/repository.go b/pkg/transformers/dent/repository.go index 28c0f5ec..018711da 100644 --- a/pkg/transformers/dent/repository.go +++ b/pkg/transformers/dent/repository.go @@ -34,6 +34,10 @@ func (repository DentRepository) Create(headerID int64, models []interface{}) er tic, getTicErr := shared.GetTicInTx(headerID, tx) if getTicErr != nil { + rollbackErr := tx.Rollback() + if rollbackErr != nil { + log.Error("failed to rollback ", rollbackErr) + } return getTicErr } diff --git a/pkg/transformers/shared/repository.go b/pkg/transformers/shared/repository.go index 65c01c99..9ee55e8f 100644 --- a/pkg/transformers/shared/repository.go +++ b/pkg/transformers/shared/repository.go @@ -7,6 +7,7 @@ import ( "fmt" "github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants" ) func MarkHeaderChecked(headerID int64, db *postgres.DB, checkedHeadersColumn string) error { @@ -100,3 +101,14 @@ func CreateNotCheckedSQL(boolColumns []string) string { return result.String() } + +func GetTicInTx(headerID int64, tx *sql.Tx) (int64, error) { + var blockTimestamp int64 + err := tx.QueryRow(`SELECT block_timestamp FROM public.headers WHERE id = $1;`, headerID).Scan(&blockTimestamp) + if err != nil { + return 0, err + } + + tic := blockTimestamp + constants.TTL + return tic, nil +} diff --git a/pkg/transformers/shared/utilities.go b/pkg/transformers/shared/utilities.go index a97aef7b..073f8783 100644 --- a/pkg/transformers/shared/utilities.go +++ b/pkg/transformers/shared/utilities.go @@ -15,7 +15,6 @@ package shared import ( - "database/sql" "github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants" "math/big" ) @@ -79,15 +78,3 @@ func convert(conversion string, value string, precision int) string { } return result.Text('f', precision) } - -// Grabs the block timestamp for an headerID, and adds the TTL constant -func GetTicInTx(headerID int64, tx *sql.Tx) (int64, error) { - var blockTimestamp int64 - err := tx.QueryRow(`SELECT block_timestamp FROM public.headers WHERE id = $1;`, headerID).Scan(&blockTimestamp) - if err != nil { - return 0, err - } - - tic := blockTimestamp + constants.TTL - return tic, nil -} diff --git a/pkg/transformers/tend/repository.go b/pkg/transformers/tend/repository.go index 9cebdf46..b4ea5475 100644 --- a/pkg/transformers/tend/repository.go +++ b/pkg/transformers/tend/repository.go @@ -34,6 +34,10 @@ func (repository TendRepository) Create(headerID int64, models []interface{}) er tic, getTicErr := shared.GetTicInTx(headerID, tx) if getTicErr != nil { + rollbackErr := tx.Rollback() + if rollbackErr != nil { + log.Error("failed to rollback ", rollbackErr) + } return getTicErr }