Skip to content

Commit

Permalink
fix ci failures to find evm_XX schema
Browse files Browse the repository at this point in the history
  • Loading branch information
krehermann committed Jun 19, 2024
1 parent ae0556b commit 9ee4543
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 26 deletions.
8 changes: 4 additions & 4 deletions core/cmd/shell_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,7 @@ func (s *Shell) RollbackDatabase(c *cli.Context) error {
return fmt.Errorf("failed to convert opts %v to evm db cfg %w", opts, err)
}

err = evmdb.Rollback(ctx, db.DB, version, cfg)
err = evmdb.Rollback(ctx, db, version, cfg)
if err != nil {
return fmt.Errorf("evm for cfg %v rollback failed: %w", cfg, err)
}
Expand Down Expand Up @@ -1071,7 +1071,7 @@ func (s *Shell) VersionDatabase(c *cli.Context) error {
if err != nil {
return fmt.Errorf("failed to convert opts %v to evm db cfg %w", opts, err)
}
version, err := evmdb.Current(ctx, db.DB, cfg)
version, err := evmdb.Current(ctx, db, cfg)
if err != nil {
return fmt.Errorf("evm for cfg %v current failed: %w", cfg, err)
}
Expand Down Expand Up @@ -1100,7 +1100,7 @@ func (s *Shell) StatusDatabase(c *cli.Context) error {
if err != nil {
return fmt.Errorf("failed to convert opts %v to evm db cfg %w", opts, err)
}
err = evmdb.Status(ctx, db.DB, cfg)
err = evmdb.Status(ctx, db, cfg)
if err != nil {
return fmt.Errorf("evm for cfg %v status failed: %w", cfg, err)
}
Expand Down Expand Up @@ -1257,7 +1257,7 @@ func migrateDB(ctx context.Context, config dbConfig, lggr logger.Logger, opts *r
if err != nil {
return fmt.Errorf("failed to convert opts %v to evm db cfg %w", opts, err)
}
err = evmdb.Migrate(ctx, db.DB, cfg)
err = evmdb.Migrate(ctx, db, cfg)
if err != nil {
return fmt.Errorf("evm db migrate failed for cfg %v: %w", cfg, err)
}
Expand Down
12 changes: 7 additions & 5 deletions core/services/relay/evm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"os"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -136,12 +137,13 @@ func NewRelayer(lggr logger.Logger, chain legacyevm.Chain, opts RelayerOpts) (*R
}
lggr = lggr.Named("Relayer")
// run the migrations for the relayer
// TODO: need a dburl in options. this is a hack. migration require a sql.DB because that's what goose uses
dburl := string(env.DatabaseURL)
// TODO: need a dburlVar in options. this is a hack. migration require a sql.DB because that's what goose uses
dburl := os.Getenv(string(env.DatabaseURL))
if dburl == "" {
return nil, errors.New("missing DatabaseURL")
}
db, err := sql.Open(string(dialects.Postgres), dburl)

db, err := sqlx.Open(string(dialects.Postgres), dburl)
if err != nil {
return nil, fmt.Errorf("failed to open db: %w", err)
}
Expand All @@ -154,8 +156,8 @@ func NewRelayer(lggr logger.Logger, chain legacyevm.Chain, opts RelayerOpts) (*R
if err != nil {
return nil, fmt.Errorf("failed to migrate evm relayer for chain %s: %w", chain.ID().String(), err)
}
dbx := sqlx.NewDb(db, string(dialects.Postgres))
ds := sqlutil.WrapDataSource(dbx, lggr)

ds := sqlutil.WrapDataSource(db, lggr)
mercuryORM := mercury.NewORM(opts.DS)
lloORM := llo.NewORM(opts.DS, chain.ID())
cdcFactory := llo.NewChannelDefinitionCacheFactory(lggr, lloORM, chain.LogPoller())
Expand Down
35 changes: 24 additions & 11 deletions core/store/migrate/plugins/relayer/evm/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package evm

import (
"context"
"database/sql"
"fmt"
"os"
"path/filepath"
"sync"

"github.com/jmoiron/sqlx"
"github.com/pressly/goose/v3"
"github.com/pressly/goose/v3/database"
"gopkg.in/guregu/null.v4"
Expand All @@ -17,15 +17,28 @@ import (
var mu sync.Mutex
var providerCache = make(map[string]*goose.Provider)

func newProvider(db *sql.DB, cfg Cfg) (*goose.Provider, error) {
mTable := fmt.Sprintf("goose_migration_evmrelayer_%s_%s", cfg.Schema, cfg.ChainID.String())
type session struct {
User string
DB string
}

func newProvider(db *sqlx.DB, cfg Cfg) (*goose.Provider, error) {
// cache doesn't seem to work, with tests; see errors with missed emv_XX tables
// best guess the the db object is different between calls.
mTable := fmt.Sprintf("goose_migration_evmrelayer_%s_%s", cfg.Schema, cfg.ChainID.String())
var s session
err := db.Get(&s, `SELECT user, current_database() as db;`)
if err != nil {
return nil, fmt.Errorf("failed to get session info: %w", err)
}
id := fmt.Sprintf("%s@%s:%s", s.User, s.DB, mTable)
mu.Lock()
defer mu.Unlock()
if p, ok := providerCache[mTable]; ok {
if p, ok := providerCache[id]; ok {
return p, nil
}

// TODO: could be another layer of sharing to reuse generated migrations for the same cfg across different dbs
// maybe that would be more error prone and not worth it
store, err := database.NewStore(goose.DialectPostgres, mTable)
if err != nil {
return nil, fmt.Errorf("failed to create goose store for table %s: %w", mTable, err)
Expand Down Expand Up @@ -63,14 +76,14 @@ func newProvider(db *sql.DB, cfg Cfg) (*goose.Provider, error) {
goose.ResetGlobalMigrations()
p, err := goose.NewProvider(
"",
db, fsys,
db.DB, fsys,
goose.WithStore(store),
goose.WithDisableGlobalRegistry(true), // until/if we refactor the core migrations to use goose provider
goose.WithGoMigrations(goMigrations...))
if err != nil {
return nil, fmt.Errorf("failed to create goose provider: %w", err)
}
providerCache[mTable] = p
providerCache[id] = p
return p, nil
}

Expand All @@ -89,7 +102,7 @@ func setupPluginMigrations(cfg Cfg) error {
*/
// Migrate migrates a subsystem of the chainlink database.
// It generates migrations based on the template for the subsystem and applies them to the database.
func Migrate(ctx context.Context, db *sql.DB, cfg Cfg) error {
func Migrate(ctx context.Context, db *sqlx.DB, cfg Cfg) error {
p, err := newProvider(db, cfg)
if err != nil {
return fmt.Errorf("failed to create goose provider: %w", err)
Expand Down Expand Up @@ -128,7 +141,7 @@ func Migrate(ctx context.Context, db *sql.DB, cfg Cfg) error {
return nil
}

func Rollback(ctx context.Context, db *sql.DB, version null.Int, cfg Cfg) error {
func Rollback(ctx context.Context, db *sqlx.DB, version null.Int, cfg Cfg) error {
p, err := newProvider(db, cfg)
if err != nil {
return fmt.Errorf("failed to create goose provider: %w", err)
Expand All @@ -142,7 +155,7 @@ func Rollback(ctx context.Context, db *sql.DB, version null.Int, cfg Cfg) error
return err
}

func Current(ctx context.Context, db *sql.DB, cfg Cfg) (int64, error) {
func Current(ctx context.Context, db *sqlx.DB, cfg Cfg) (int64, error) {
p, err := newProvider(db, cfg)
if err != nil {
return -1, fmt.Errorf("failed to create goose provider: %w", err)
Expand All @@ -151,7 +164,7 @@ func Current(ctx context.Context, db *sql.DB, cfg Cfg) (int64, error) {

}

Check failure on line 165 in core/store/migrate/plugins/relayer/evm/migrate.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary trailing newline (whitespace)

func Status(ctx context.Context, db *sql.DB, cfg Cfg) error {
func Status(ctx context.Context, db *sqlx.DB, cfg Cfg) error {
p, err := newProvider(db, cfg)
if err != nil {
return fmt.Errorf("failed to create goose provider: %w", err)
Expand Down
10 changes: 5 additions & 5 deletions core/store/migrate/plugins/relayer/evm/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@ func TestMigrate(t *testing.T) {
}
// the evm migrations only work if the core migrations have been run
// because we are moving existing tables
err := evm.Migrate(ctx, db.DB, cfg)
err := evm.Migrate(ctx, db, cfg)
require.Error(t, err)
err = migrate.Migrate(ctx, db.DB)
require.NoError(t, err)

err = evm.Migrate(ctx, db.DB, cfg)
err = evm.Migrate(ctx, db, cfg)
require.NoError(t, err)

v2, err := evm.Current(ctx, db.DB, cfg)
v2, err := evm.Current(ctx, db, cfg)
require.NoError(t, err)
assert.GreaterOrEqual(t, int64(2), v2)

err = evm.Rollback(ctx, db.DB, null.IntFrom(0), cfg)
err = evm.Rollback(ctx, db, null.IntFrom(0), cfg)
require.NoError(t, err)

v2, err = evm.Current(ctx, db.DB, cfg)
v2, err = evm.Current(ctx, db, cfg)
require.NoError(t, err)
assert.Equal(t, int64(0), v2)
})
Expand Down
2 changes: 1 addition & 1 deletion core/store/migrate/plugins/relayer/evm/testutils/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func NewDB(t testing.TB, cfg evm.Cfg) *sqlx.DB {
_, err := evmHeavyDB.DB.Exec(evmInitialState)
require.NoError(t, err, "failed to exec SQL for the initial state of the EVM database")
// now we can run the migrations
err = evm.Migrate(testutils.Context(t), evmHeavyDB.DB, cfg)
err = evm.Migrate(testutils.Context(t), evmHeavyDB, cfg)
require.NoError(t, err, "failed to migrate EVM database for cfg %v", cfg)
migratedDBs[id] = evmHeavyDB
url := c.Database().URL()
Expand Down

0 comments on commit 9ee4543

Please sign in to comment.