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

BCF-2637 Change evmChainID not null migration to use env var to inject goose #10711

Merged
Merged
Show file tree
Hide file tree
Changes from 6 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
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
12 changes: 12 additions & 0 deletions core/store/migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"gopkg.in/guregu/null.v4"

"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 +132,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(migrations.EVMChainIDNotNullMigrationHelper, 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/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/migrate/migrations"
"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(migrations.EVMChainIDNotNullMigrationHelper)
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(migrations.EVMChainIDNotNullMigrationHelper)
require.Equal(t, actualChainID, chainID.String())
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@ package migrations
import (
"context"
"database/sql"
"os"

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

// No need to scan migration script
// START-NOSCAN
func init() {
goose.AddMigrationContext(Up195, Down195)
}

const (
EVMChainIDNotNullMigrationHelper = "EVM_CHAINID_MIGRATION_HELPER_ENV_VAR"
ilija42 marked this conversation as resolved.
Show resolved Hide resolved

addNullConstraintsToSpecs = `
ALTER TABLE direct_request_specs ALTER COLUMN evm_chain_id SET NOT NULL;
ALTER TABLE flux_monitor_specs ALTER COLUMN evm_chain_id SET NOT NULL;
Expand All @@ -36,6 +41,25 @@ const (

// nolint
func Up195(ctx context.Context, tx *sql.Tx) error {
chainID, set := os.LookupEnv(EVMChainIDNotNullMigrationHelper)
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")
jmank88 marked this conversation as resolved.
Show resolved Hide resolved
}
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
Loading