Skip to content

Commit

Permalink
BCFG-2637 Change evmChainID not null migration to use env var to inje…
Browse files Browse the repository at this point in the history
…ct goose (#10711)

* Change evmChainID not null migration to use env var to inject goose

* Update changelog

* Add sonar qube no scan to goose migration script

* Fix BEGIN-NOSCAN sonarqube comment

* Fix sonarqube no scan comment

* Move 0195 migration env var to env package

* Exclude 0195 migration script from sq scan

* Exclude 0195 migration script from sq scan

* Fix exclude 0195 migration script from sq scan

* Update sonar qube tests artifacts output path
  • Loading branch information
ilija42 authored Sep 21, 2023
1 parent 175d70c commit 26652d7
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 55 deletions.
41 changes: 1 addition & 40 deletions core/cmd/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ type ChainlinkAppFactory struct{}
func (n ChainlinkAppFactory) NewApplication(ctx context.Context, cfg chainlink.GeneralConfig, appLggr logger.Logger, db *sqlx.DB) (app chainlink.Application, err error) {
initGlobals(cfg.Prometheus())

// TODO TO BE REMOVED IN v2.7.0
err = evmChainIDMigration(cfg, db.DB, appLggr)
err = migrate.SetMigrationENVVars(cfg)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -301,44 +300,6 @@ func takeBackupIfVersionUpgrade(dbUrl url.URL, rootDir string, cfg periodicbacku
return err
}

// evmChainIDMigration TODO TO BE REMOVED IN v2.7.0. This is a helper function for evmChainID 0195 migration in v2.6.0 only, so that we don't have to inject evmChainID into goose.
func evmChainIDMigration(generalConfig chainlink.GeneralConfig, db *sql.DB, lggr logger.Logger) error {
migrationVer, err := migrate.Current(db, lggr)
if err != nil {
return err
}
if migrationVer != 194 {
return nil
}

if generalConfig.EVMEnabled() {
if generalConfig.EVMConfigs() == nil {
return errors.New("evm configs are missing")
}
if generalConfig.EVMConfigs()[0] == nil {
return errors.New("evm config is nil")
}
updateQueries := []string{
`UPDATE direct_request_specs SET evm_chain_id = $1 WHERE evm_chain_id IS NULL;`,
`UPDATE flux_monitor_specs SET evm_chain_id = $1 WHERE evm_chain_id IS NULL;`,
`UPDATE ocr_oracle_specs SET evm_chain_id = $1 WHERE evm_chain_id IS NULL;`,
`UPDATE keeper_specs SET evm_chain_id = $1 WHERE evm_chain_id IS NULL;`,
`UPDATE vrf_specs SET evm_chain_id = $1 WHERE evm_chain_id IS NULL;`,
`UPDATE blockhash_store_specs SET evm_chain_id = $1 WHERE evm_chain_id IS NULL;`,
`UPDATE block_header_feeder_specs SET evm_chain_id = $1 WHERE evm_chain_id IS NULL;`,
}

chainID := generalConfig.EVMConfigs()[0].ChainID.String()
for i := range updateQueries {
_, err := db.Exec(updateQueries[i], chainID)
if err != nil {
return errors.Wrap(err, "failed to set missing evm chain ids")
}
}
}
return nil
}

// Runner implements the Run method.
type Runner interface {
Run(context.Context, chainlink.Application) error
Expand Down
14 changes: 1 addition & 13 deletions core/cmd/shell_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -840,19 +840,7 @@ func (s *Shell) MigrateDatabase(_ *cli.Context) error {
return s.errorOut(errDBURLMissing)
}

// TODO TO BE REMOVED IN v2.7.0
db, err := sql.Open(string(dialects.Postgres), parsed.String())
if err != nil {
return fmt.Errorf("unable to open postgres database for evmChainID helper migration: %+v", err)
}
defer func() {
if cerr := db.Close(); cerr != nil {
err = multierr.Append(err, cerr)
}
}()

// TODO TO BE REMOVED IN v2.7.0
err = evmChainIDMigration(s.Config, db, s.Logger)
err := migrate.SetMigrationENVVars(s.Config)
if err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions core/config/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ var (
PyroscopeAuthToken = Secret("CL_PYROSCOPE_AUTH_TOKEN")
PrometheusAuthToken = Secret("CL_PROMETHEUS_AUTH_TOKEN")
ThresholdKeyShare = Secret("CL_THRESHOLD_KEY_SHARE")
// Migrations env vars
EVMChainIDNotNullMigration0195 = "CL_EVM_CHAINID_NOT_NULL_MIGRATION_0195"
)

type Var string
Expand Down
13 changes: 13 additions & 0 deletions core/store/migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import (
"github.com/smartcontractkit/sqlx"
"gopkg.in/guregu/null.v4"

"github.com/smartcontractkit/chainlink/v2/core/config/env"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/chainlink"
"github.com/smartcontractkit/chainlink/v2/core/services/pg"
"github.com/smartcontractkit/chainlink/v2/core/store/migrate/migrations" // Invoke init() functions within migrations pkg.
)
Expand Down Expand Up @@ -131,3 +133,14 @@ func Status(db *sql.DB, lggr logger.Logger) error {
func Create(db *sql.DB, name, migrationType string) error {
return goose.Create(db, "core/store/migrate/migrations", name, migrationType)
}

// SetMigrationENVVars is used to inject values from config to goose migrations via env.
func SetMigrationENVVars(generalConfig chainlink.GeneralConfig) error {
if generalConfig.EVMEnabled() {
err := os.Setenv(env.EVMChainIDNotNullMigration0195, generalConfig.EVMConfigs()[0].ChainID.String())
if err != nil {
panic(errors.Wrap(err, "failed to set migrations env variables"))
}
}
return nil
}
36 changes: 36 additions & 0 deletions core/store/migrate/migrate_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package migrate_test

import (
"math/big"
"os"
"testing"
"time"

Expand All @@ -11,13 +13,19 @@ import (
"gopkg.in/guregu/null.v4"

"github.com/smartcontractkit/chainlink-relay/pkg/types"

evmcfg "github.com/smartcontractkit/chainlink/v2/core/chains/evm/config/toml"
"github.com/smartcontractkit/chainlink/v2/core/config/env"
"github.com/smartcontractkit/chainlink/v2/core/internal/cltest/heavyweight"
configtest "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest/v2"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/chainlink"
"github.com/smartcontractkit/chainlink/v2/core/services/job"
"github.com/smartcontractkit/chainlink/v2/core/services/pipeline"
"github.com/smartcontractkit/chainlink/v2/core/services/relay"
"github.com/smartcontractkit/chainlink/v2/core/store/migrate"
"github.com/smartcontractkit/chainlink/v2/core/store/models"
"github.com/smartcontractkit/chainlink/v2/core/utils"
)

var migrationDir = "migrations"
Expand Down Expand Up @@ -401,3 +409,31 @@ func TestMigrate(t *testing.T) {
require.NoError(t, err)
require.Equal(t, int64(99), ver)
}

func TestSetMigrationENVVars(t *testing.T) {
t.Run("ValidEVMConfig", func(t *testing.T) {
chainID := utils.NewBig(big.NewInt(1337))
testConfig := configtest.NewGeneralConfig(t, func(c *chainlink.Config, s *chainlink.Secrets) {
evmEnabled := true
c.EVM = evmcfg.EVMConfigs{&evmcfg.EVMConfig{
ChainID: chainID,
Enabled: &evmEnabled,
}}
})

require.NoError(t, migrate.SetMigrationENVVars(testConfig))

actualChainID := os.Getenv(env.EVMChainIDNotNullMigration0195)
require.Equal(t, actualChainID, chainID.String())
})

t.Run("EVMConfigMissing", func(t *testing.T) {
chainID := utils.NewBig(big.NewInt(1337))
testConfig := configtest.NewGeneralConfig(t, func(c *chainlink.Config, s *chainlink.Secrets) { c.EVM = nil })

require.NoError(t, migrate.SetMigrationENVVars(testConfig))

actualChainID := os.Getenv(env.EVMChainIDNotNullMigration0195)
require.Equal(t, actualChainID, chainID.String())
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package migrations
import (
"context"
"database/sql"
"os"

"github.com/pkg/errors"
"github.com/pressly/goose/v3"

"github.com/smartcontractkit/chainlink/v2/core/config/env"
)

func init() {
Expand Down Expand Up @@ -36,6 +39,25 @@ const (

// nolint
func Up195(ctx context.Context, tx *sql.Tx) error {
chainID, set := os.LookupEnv(env.EVMChainIDNotNullMigration0195)
if set {
updateQueries := []string{
`UPDATE direct_request_specs SET evm_chain_id = $1 WHERE evm_chain_id IS NULL;`,
`UPDATE flux_monitor_specs SET evm_chain_id = $1 WHERE evm_chain_id IS NULL;`,
`UPDATE ocr_oracle_specs SET evm_chain_id = $1 WHERE evm_chain_id IS NULL;`,
`UPDATE keeper_specs SET evm_chain_id = $1 WHERE evm_chain_id IS NULL;`,
`UPDATE vrf_specs SET evm_chain_id = $1 WHERE evm_chain_id IS NULL;`,
`UPDATE blockhash_store_specs SET evm_chain_id = $1 WHERE evm_chain_id IS NULL;`,
`UPDATE block_header_feeder_specs SET evm_chain_id = $1 WHERE evm_chain_id IS NULL;`,
}
for i := range updateQueries {
_, err := tx.Exec(updateQueries[i], chainID)
if err != nil {
return errors.Wrap(err, "failed to set missing evm chain ids")
}
}
}

_, err := tx.ExecContext(ctx, addNullConstraintsToSpecs)
return errors.Wrap(err, "failed to add null constraints")
}
Expand Down
2 changes: 1 addition & 1 deletion docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Temporary helper function for proper migration of evmChainID not null in specs, that fills in missing chainIDs. This needs to be removed after one version after everyone migrated properly. For proper migrations all nodes must upgrade sequentially without skipping this version.
- Helper migrations function for injecting env vars into goose migrations. This was done to inject chainID into evm chain id not null in specs migrations.

### Removed

Expand Down
2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ sonar.python.version=3.8
# Full exclusions from the static analysis
sonar.exclusions=**/node_modules/**/*,**/mocks/**/*, **/testdata/**/*, **/contracts/typechain/**/*, **/contracts/artifacts/**/*, **/contracts/cache/**/*, **/contracts/scripts/**/*, **/generated/**/*, **/fixtures/**/*, **/docs/**/*, **/tools/**/*, **/*.pb.go, **/*report.xml, **/*.config.ts, **/*.txt, **/*.abi, **/*.bin, **/*_codecgen.go
# Coverage exclusions
sonar.coverage.exclusions=**/*.test.ts, **/*_test.go, **/contracts/test/**/*, **/contracts/**/tests/**/*, **/core/**/testutils/**/*, **/core/**/mocks/**/*, **/core/**/cltest/**/*, **/integration-tests/**/*, **/generated/**/*, **/core/scripts**/* , **/*.pb.go, ./plugins/**/*, **/main.go
sonar.coverage.exclusions=**/*.test.ts, **/*_test.go, **/contracts/test/**/*, **/contracts/**/tests/**/*, **/core/**/testutils/**/*, **/core/**/mocks/**/*, **/core/**/cltest/**/*, **/integration-tests/**/*, **/generated/**/*, **/core/scripts**/* , **/*.pb.go, ./plugins/**/*, **/main.go, **/0195_add_not_null_to_evm_chain_id_in_job_specs.go
# Duplication exclusions
sonar.cpd.exclusions=**/contracts/**/*.sol, **/config.go, /core/services/ocr2/plugins/ocr2keeper/evm*/*

Expand Down

0 comments on commit 26652d7

Please sign in to comment.