Add rollback if GetTicInTx fails and move GetTicInTx to shared/repository.go

This commit is contained in:
yaoandrew 2018-12-20 20:29:48 -08:00
parent c8aa3dc275
commit e2b864baa8
4 changed files with 20 additions and 13 deletions

View File

@ -34,6 +34,10 @@ func (repository DentRepository) Create(headerID int64, models []interface{}) er
tic, getTicErr := shared.GetTicInTx(headerID, tx) tic, getTicErr := shared.GetTicInTx(headerID, tx)
if getTicErr != nil { if getTicErr != nil {
rollbackErr := tx.Rollback()
if rollbackErr != nil {
log.Error("failed to rollback ", rollbackErr)
}
return getTicErr return getTicErr
} }

View File

@ -7,6 +7,7 @@ import (
"fmt" "fmt"
"github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" "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 { func MarkHeaderChecked(headerID int64, db *postgres.DB, checkedHeadersColumn string) error {
@ -100,3 +101,14 @@ func CreateNotCheckedSQL(boolColumns []string) string {
return result.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
}

View File

@ -15,7 +15,6 @@
package shared package shared
import ( import (
"database/sql"
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants" "github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants"
"math/big" "math/big"
) )
@ -79,15 +78,3 @@ func convert(conversion string, value string, precision int) string {
} }
return result.Text('f', precision) 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
}

View File

@ -34,6 +34,10 @@ func (repository TendRepository) Create(headerID int64, models []interface{}) er
tic, getTicErr := shared.GetTicInTx(headerID, tx) tic, getTicErr := shared.GetTicInTx(headerID, tx)
if getTicErr != nil { if getTicErr != nil {
rollbackErr := tx.Rollback()
if rollbackErr != nil {
log.Error("failed to rollback ", rollbackErr)
}
return getTicErr return getTicErr
} }