Skip to content

Commit

Permalink
Improve randomize testdb sequences (#11433)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilija42 committed Nov 30, 2023
1 parent 5b54684 commit 74e4c68
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions core/cmd/shell_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
gethCommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/fatih/color"
"github.com/lib/pq"

"github.com/kylelemons/godebug/diff"
"github.com/pkg/errors"
Expand Down Expand Up @@ -834,15 +835,18 @@ func (m *failedToRandomizeTestDBSequencesError) Error() string {
// randomizeTestDBSequences randomizes sequenced table columns sequence
// This is necessary as to avoid false positives in some test cases.
func randomizeTestDBSequences(db *sqlx.DB) error {
seqRows, err := db.Query(`SELECT sequence_schema, sequence_name FROM information_schema.sequences WHERE sequence_schema = $1`, "public")
// not ideal to hard code this, but also not safe to do it programmatically :(
schemas := pq.Array([]string{"public", "evm"})
seqRows, err := db.Query(`SELECT sequence_schema, sequence_name, minimum_value FROM information_schema.sequences WHERE sequence_schema IN ($1)`, schemas)
if err != nil {
return fmt.Errorf("%s: error fetching sequences: %s", failedToRandomizeTestDBSequencesError{}, err)
}

defer seqRows.Close()
for seqRows.Next() {
var sequenceSchema, sequenceName string
if err = seqRows.Scan(&sequenceSchema, &sequenceName); err != nil {
var minimumSequenceValue int64
if err = seqRows.Scan(&sequenceSchema, &sequenceName, &minimumSequenceValue); err != nil {
return fmt.Errorf("%s: failed scanning sequence rows: %s", failedToRandomizeTestDBSequencesError{}, err)
}

Expand All @@ -855,6 +859,7 @@ func randomizeTestDBSequences(db *sqlx.DB) error {
if err != nil {
return fmt.Errorf("%s: failed to generate random number", failedToRandomizeTestDBSequencesError{})
}
randNum.Add(randNum, big.NewInt(minimumSequenceValue))

if _, err = db.Exec(fmt.Sprintf("ALTER SEQUENCE %s.%s RESTART WITH %d", sequenceSchema, sequenceName, randNum)); err != nil {
return fmt.Errorf("%s: failed to alter and restart %s sequence: %w", failedToRandomizeTestDBSequencesError{}, sequenceName, err)
Expand Down

0 comments on commit 74e4c68

Please sign in to comment.