Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve randomize testdb sequences #11433

Merged
merged 1 commit into from
Nov 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading