Skip to content

Commit

Permalink
[BCF-2320] Move DatabaseURL to cfg.Database; Rename (smartcontractkit…
Browse files Browse the repository at this point in the history
…#9400)

Rename DatabaseURL -> URL
  • Loading branch information
cedric-cordenier authored Jun 2, 2023
1 parent 3d3bc94 commit 2421ed2
Show file tree
Hide file tree
Showing 23 changed files with 38 additions and 82 deletions.
14 changes: 0 additions & 14 deletions core/chains/evm/config/mocks/chain_scoped_config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions core/chains/evm/txmgr/broadcaster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func NewTestEthBroadcaster(
nonceAutoSync bool,
) (*txmgr.EvmBroadcaster, error) {
t.Helper()
eventBroadcaster := cltest.NewEventBroadcaster(t, config.DatabaseURL())
eventBroadcaster := cltest.NewEventBroadcaster(t, config.Database().URL())
err := eventBroadcaster.Start(testutils.Context(t.(*testing.T)))
require.NoError(t, err)
t.Cleanup(func() { assert.NoError(t, eventBroadcaster.Close()) })
Expand All @@ -78,7 +78,7 @@ func NewTestEthBroadcaster(

func TestEthBroadcaster_Lifecycle(t *testing.T) {
cfg, db := heavyweight.FullTestDBV2(t, "eth_broadcaster_optimistic_locking", nil)
eventBroadcaster := cltest.NewEventBroadcaster(t, cfg.DatabaseURL())
eventBroadcaster := cltest.NewEventBroadcaster(t, cfg.Database().URL())
err := eventBroadcaster.Start(testutils.Context(t))
require.NoError(t, err)
t.Cleanup(func() { assert.NoError(t, eventBroadcaster.Close()) })
Expand Down Expand Up @@ -1235,7 +1235,7 @@ func TestEthBroadcaster_ProcessUnstartedEthTxs_Errors(t *testing.T) {

// same as the parent test, but callback is set by ctor
t.Run("callback set by ctor", func(t *testing.T) {
eventBroadcaster := pg.NewEventBroadcaster(cfg.DatabaseURL(), 0, 0, logger.TestLogger(t), uuid.New())
eventBroadcaster := pg.NewEventBroadcaster(cfg.Database().URL(), 0, 0, logger.TestLogger(t), uuid.New())
err := eventBroadcaster.Start(testutils.Context(t))
require.NoError(t, err)
t.Cleanup(func() { assert.NoError(t, eventBroadcaster.Close()) })
Expand Down Expand Up @@ -1962,7 +1962,7 @@ func TestEthBroadcaster_EthTxInsertEventCausesTriggerToFire(t *testing.T) {

ethKeyStore := cltest.NewKeyStore(t, db, cfg).Eth()
_, fromAddress := cltest.MustAddRandomKeyToKeystore(t, ethKeyStore)
eventBroadcaster := cltest.NewEventBroadcaster(t, evmcfg.DatabaseURL())
eventBroadcaster := cltest.NewEventBroadcaster(t, evmcfg.Database().URL())
require.NoError(t, eventBroadcaster.Start(testutils.Context(t)))
t.Cleanup(func() { require.NoError(t, eventBroadcaster.Close()) })

Expand Down
6 changes: 3 additions & 3 deletions core/cmd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (n ChainlinkAppFactory) NewApplication(ctx context.Context, cfg chainlink.G
}

dbListener := cfg.Database().Listener()
eventBroadcaster := pg.NewEventBroadcaster(cfg.DatabaseURL(), dbListener.MinReconnectInterval(), dbListener.MaxReconnectDuration(), appLggr, cfg.AppID())
eventBroadcaster := pg.NewEventBroadcaster(cfg.Database().URL(), dbListener.MinReconnectInterval(), dbListener.MaxReconnectDuration(), appLggr, cfg.AppID())
ccOpts := evm.ChainSetOpts{
Config: cfg,
Logger: appLggr,
Expand Down Expand Up @@ -211,7 +211,7 @@ func (n ChainlinkAppFactory) NewApplication(ctx context.Context, cfg chainlink.G
return nil, err
}

restrictedClient := clhttp.NewRestrictedHTTPClient(cfg, appLggr)
restrictedClient := clhttp.NewRestrictedHTTPClient(cfg.Database(), appLggr)
unrestrictedClient := clhttp.NewUnrestrictedHTTPClient()
externalInitiatorManager := webhook.NewExternalInitiatorManager(db, unrestrictedClient, appLggr, cfg)
return chainlink.NewApplication(chainlink.ApplicationOpts{
Expand Down Expand Up @@ -358,7 +358,7 @@ func handleNodeVersioning(db *sqlx.DB, appLggr logger.Logger, rootDir string, cf
// Need to do this BEFORE migration
backupCfg := cfg.Backup()
if backupCfg.Mode() != config.DatabaseBackupModeNone && backupCfg.OnVersionUpgrade() {
if err = takeBackupIfVersionUpgrade(cfg.DatabaseURL(), rootDir, cfg.Backup(), appLggr, appv, dbv); err != nil {
if err = takeBackupIfVersionUpgrade(cfg.URL(), rootDir, cfg.Backup(), appLggr, appv, dbv); err != nil {
if errors.Is(err, sql.ErrNoRows) {
appLggr.Debugf("Failed to find any node version in the DB: %w", err)
} else if strings.Contains(err.Error(), "relation \"node_versions\" does not exist") {
Expand Down
12 changes: 6 additions & 6 deletions core/cmd/local_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ func (cli *Client) validateDB(ctx *clipkg.Context) error {
// in secrets TOML. This is useful to setup the database for testing
func (cli *Client) ResetDatabase(c *clipkg.Context) error {
cfg := cli.Config.Database()
parsed := cfg.DatabaseURL()
parsed := cfg.URL()
if parsed.String() == "" {
return cli.errorOut(errDBURLMissing)
}
Expand Down Expand Up @@ -738,7 +738,7 @@ func (cli *Client) PrepareTestDatabase(c *clipkg.Context) error {
cfg := cli.Config

// Creating pristine DB copy to speed up FullTestDB
dbUrl := cfg.DatabaseURL()
dbUrl := cfg.Database().URL()
db, err := sqlx.Open(string(dialects.Postgres), dbUrl.String())
if err != nil {
return cli.errorOut(err)
Expand Down Expand Up @@ -805,7 +805,7 @@ func (cli *Client) PrepareTestDatabaseUserOnly(c *clipkg.Context) error {
return cli.errorOut(err)
}
cfg := cli.Config
if err := insertFixtures(cfg.DatabaseURL(), "../store/fixtures/users_only_fixtures.sql"); err != nil {
if err := insertFixtures(cfg.Database().URL(), "../store/fixtures/users_only_fixtures.sql"); err != nil {
return cli.errorOut(err)
}
return nil
Expand All @@ -814,7 +814,7 @@ func (cli *Client) PrepareTestDatabaseUserOnly(c *clipkg.Context) error {
// MigrateDatabase migrates the database
func (cli *Client) MigrateDatabase(c *clipkg.Context) error {
cfg := cli.Config.Database()
parsed := cfg.DatabaseURL()
parsed := cfg.URL()
if parsed.String() == "" {
return cli.errorOut(errDBURLMissing)
}
Expand Down Expand Up @@ -905,12 +905,12 @@ type dbConfig interface {
DefaultLockTimeout() time.Duration
MaxOpenConns() int
MaxIdleConns() int
DatabaseURL() url.URL
URL() url.URL
Dialect() dialects.DialectName
}

func newConnection(cfg dbConfig) (*sqlx.DB, error) {
parsed := cfg.DatabaseURL()
parsed := cfg.URL()
if parsed.String() == "" {
return nil, errDBURLMissing
}
Expand Down
5 changes: 2 additions & 3 deletions core/config/database_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
// interface defined below and accessed via cfg.Database().<FieldName>().
type DatabaseV1 interface {
DatabaseDefaultQueryTimeout() time.Duration
DatabaseURL() url.URL
LogSQL() bool
}

Expand Down Expand Up @@ -40,13 +39,13 @@ type Database interface {
Listener() Listener
Lock() Lock
DatabaseDefaultQueryTimeout() time.Duration
DatabaseURL() url.URL
LogSQL() bool

Dialect() dialects.DialectName
DefaultIdleInTxSessionTimeout() time.Duration
DefaultLockTimeout() time.Duration
MigrateDatabase() bool
MaxIdleConns() int
MaxOpenConns() int
LogSQL() bool
URL() url.URL
}
2 changes: 1 addition & 1 deletion core/internal/cltest/cltest.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ func NewApplicationWithConfig(t testing.TB, cfg chainlink.GeneralConfig, flagsAn

var eventBroadcaster pg.EventBroadcaster = pg.NewNullEventBroadcaster()

url := cfg.DatabaseURL()
url := cfg.Database().URL()
db, err := pg.NewConnection(url.String(), cfg.Database().Dialect(), cfg.Database())
require.NoError(t, err)
t.Cleanup(func() { assert.NoError(t, db.Close()) })
Expand Down
2 changes: 1 addition & 1 deletion core/internal/cltest/heavyweight/orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func prepareFullTestDBV2(t *testing.T, name string, empty bool, loadFixtures boo

require.NoError(t, os.MkdirAll(gcfg.RootDir(), 0700))
name = fmt.Sprintf("%s_%x", name, rand.Intn(0xFFF)) // to avoid name collisions
migrationTestDBURL, err := dropAndCreateThrowawayTestDB(gcfg.DatabaseURL(), name, empty)
migrationTestDBURL, err := dropAndCreateThrowawayTestDB(gcfg.Database().URL(), name, empty)
require.NoError(t, err)
db, err := pg.NewConnection(migrationTestDBURL, dialects.Postgres, gcfg.Database())
require.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions core/internal/cltest/simulated_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func NewApplicationWithConfigV2OnSimulatedBlockchain(
require.Zero(t, defID.Cmp(testutils.SimulatedChainID))
chainID := utils.NewBig(testutils.SimulatedChainID)
client := client.NewSimulatedBackendClient(t, backend, testutils.SimulatedChainID)
eventBroadcaster := pg.NewEventBroadcaster(cfg.DatabaseURL(), 0, 0, logger.TestLogger(t), uuid.New())
eventBroadcaster := pg.NewEventBroadcaster(cfg.Database().URL(), 0, 0, logger.TestLogger(t), uuid.New())

flagsAndDeps = append(flagsAndDeps, client, eventBroadcaster, chainID)

Expand All @@ -65,7 +65,7 @@ func NewApplicationWithConfigV2AndKeyOnSimulatedBlockchain(
require.Zero(t, defID.Cmp(testutils.SimulatedChainID))
chainID := utils.NewBig(testutils.SimulatedChainID)
client := client.NewSimulatedBackendClient(t, backend, testutils.SimulatedChainID)
eventBroadcaster := pg.NewEventBroadcaster(cfg.DatabaseURL(), 0, 0, logger.TestLogger(t), uuid.New())
eventBroadcaster := pg.NewEventBroadcaster(cfg.Database().URL(), 0, 0, logger.TestLogger(t), uuid.New())

flagsAndDeps = append(flagsAndDeps, client, eventBroadcaster, chainID)

Expand Down
4 changes: 2 additions & 2 deletions core/services/chainlink/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ func NewApplication(opts ApplicationOpts) (Application, error) {
if backupCfg.Mode() != config.DatabaseBackupModeNone && backupCfg.Frequency() > 0 {
globalLogger.Infow("DatabaseBackup: periodic database backups are enabled", "frequency", backupCfg.Frequency())

databaseBackup, err := periodicbackup.NewDatabaseBackup(cfg.DatabaseURL(), cfg.RootDir(), backupCfg, globalLogger)
databaseBackup, err := periodicbackup.NewDatabaseBackup(cfg.Database().URL(), cfg.RootDir(), backupCfg, globalLogger)
if err != nil {
return nil, errors.Wrap(err, "NewApplication: failed to initialize database backup")
}
Expand Down Expand Up @@ -451,7 +451,7 @@ func NewApplication(opts ApplicationOpts) (Application, error) {
for _, c := range chains.EVM.Chains() {
lbs = append(lbs, c.LogBroadcaster())
}
jobSpawner := job.NewSpawner(jobORM, cfg, delegates, db, globalLogger, lbs)
jobSpawner := job.NewSpawner(jobORM, cfg.Database(), delegates, db, globalLogger, lbs)
srvcs = append(srvcs, jobSpawner, pipelineRunner)

// We start the log poller after the job spawner
Expand Down
2 changes: 1 addition & 1 deletion core/services/chainlink/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1267,7 +1267,7 @@ func TestNewGeneralConfig_SecretsOverrides(t *testing.T) {
assert.NoError(t, err)
c.SetPasswords(ptr(PWD_OVERRIDE), nil)
assert.Equal(t, PWD_OVERRIDE, c.KeystorePassword())
dbURL := c.DatabaseURL()
dbURL := c.Database().URL()
assert.Equal(t, DBURL_OVERRIDE, (&dbURL).String())
}

Expand Down
2 changes: 1 addition & 1 deletion core/services/chainlink/database_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (d *databaseConfig) DatabaseDefaultQueryTimeout() time.Duration {
return d.c.DefaultQueryTimeout.Duration()
}

func (d *databaseConfig) DatabaseURL() url.URL {
func (d *databaseConfig) URL() url.URL {
return *d.s.URL.URL()
}

Expand Down
2 changes: 1 addition & 1 deletion core/services/chainlink/database_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestDatabaseConfig(t *testing.T) {
assert.Equal(t, db.MaxOpenConns(), 13)
assert.Equal(t, db.MigrateDatabase(), true)
assert.Equal(t, db.Dialect(), dialects.Postgres)
url := db.DatabaseURL()
url := db.URL()
assert.NotEqual(t, url.String(), "")

lock := db.Lock()
Expand Down
14 changes: 0 additions & 14 deletions core/services/chainlink/mocks/general_config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion core/services/job/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type ServiceCtx interface {
}

type Config interface {
DatabaseURL() url.URL
URL() url.URL
pg.QConfig
}

Expand Down
10 changes: 5 additions & 5 deletions core/services/job/spawner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TestSpawner_CreateJobDeleteJob(t *testing.T) {
orm := NewTestORM(t, db, cc, pipeline.NewORM(db, lggr, config), bridges.NewORM(db, lggr, config), keyStore, config)
a := utils.NewDependentAwaiter()
a.AddDependents(1)
spawner := job.NewSpawner(orm, config, map[job.Type]job.Delegate{}, db, lggr, []utils.DependentAwaiter{a})
spawner := job.NewSpawner(orm, config.Database(), map[job.Type]job.Delegate{}, db, lggr, []utils.DependentAwaiter{a})
// Starting the spawner should signal to the dependents
result := make(chan bool)
go func() {
Expand Down Expand Up @@ -127,7 +127,7 @@ func TestSpawner_CreateJobDeleteJob(t *testing.T) {
dB := ocr.NewDelegate(nil, orm, nil, nil, nil, monitoringEndpoint, cc, logger.TestLogger(t), config, mailMon)
delegateB := &delegate{jobB.Type, []job.ServiceCtx{serviceB1, serviceB2}, 0, make(chan struct{}), dB}

spawner := job.NewSpawner(orm, config, map[job.Type]job.Delegate{
spawner := job.NewSpawner(orm, config.Database(), map[job.Type]job.Delegate{
jobA.Type: delegateA,
jobB.Type: delegateB,
}, db, lggr, nil)
Expand Down Expand Up @@ -177,7 +177,7 @@ func TestSpawner_CreateJobDeleteJob(t *testing.T) {
mailMon := srvctest.Start(t, utils.NewMailboxMonitor(t.Name()))
d := ocr.NewDelegate(nil, orm, nil, nil, nil, monitoringEndpoint, cc, logger.TestLogger(t), config, mailMon)
delegateA := &delegate{jobA.Type, []job.ServiceCtx{serviceA1, serviceA2}, 0, nil, d}
spawner := job.NewSpawner(orm, config, map[job.Type]job.Delegate{
spawner := job.NewSpawner(orm, config.Database(), map[job.Type]job.Delegate{
jobA.Type: delegateA,
}, db, lggr, nil)

Expand Down Expand Up @@ -211,7 +211,7 @@ func TestSpawner_CreateJobDeleteJob(t *testing.T) {
mailMon := srvctest.Start(t, utils.NewMailboxMonitor(t.Name()))
d := ocr.NewDelegate(nil, orm, nil, nil, nil, monitoringEndpoint, cc, logger.TestLogger(t), config, mailMon)
delegateA := &delegate{jobA.Type, []job.ServiceCtx{serviceA1, serviceA2}, 0, nil, d}
spawner := job.NewSpawner(orm, config, map[job.Type]job.Delegate{
spawner := job.NewSpawner(orm, config.Database(), map[job.Type]job.Delegate{
jobA.Type: delegateA,
}, db, lggr, nil)

Expand Down Expand Up @@ -283,7 +283,7 @@ func TestSpawner_CreateJobDeleteJob(t *testing.T) {
keyStore.OCR2(), keyStore.DKGSign(), keyStore.DKGEncrypt(), ethKeyStore, relayers, mailMon)
delegateOCR2 := &delegate{jobOCR2VRF.Type, []job.ServiceCtx{}, 0, nil, d}

spawner := job.NewSpawner(orm, config, map[job.Type]job.Delegate{
spawner := job.NewSpawner(orm, config.Database(), map[job.Type]job.Delegate{
jobOCR2VRF.Type: delegateOCR2,
}, db, lggr, nil)

Expand Down
2 changes: 1 addition & 1 deletion core/services/pg/event_broadcaster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
func TestEventBroadcaster(t *testing.T) {
config, _ := heavyweight.FullTestDBNoFixturesV2(t, "event_broadcaster", nil)

eventBroadcaster := cltest.NewEventBroadcaster(t, config.DatabaseURL())
eventBroadcaster := cltest.NewEventBroadcaster(t, config.Database().URL())
require.NoError(t, eventBroadcaster.Start(testutils.Context(t)))
t.Cleanup(func() { require.NoError(t, eventBroadcaster.Close()) })

Expand Down
4 changes: 2 additions & 2 deletions core/services/pg/locked_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type LockedDB interface {

type LockedDBConfig interface {
ConnectionConfig
DatabaseURL() url.URL
URL() url.URL
DatabaseDefaultQueryTimeout() time.Duration
Dialect() dialects.DialectName
}
Expand Down Expand Up @@ -140,7 +140,7 @@ func (l lockedDb) DB() *sqlx.DB {
}

func openDB(appID uuid.UUID, cfg LockedDBConfig) (db *sqlx.DB, err error) {
uri := cfg.DatabaseURL()
uri := cfg.URL()
static.SetConsumerName(&uri, "App", &appID)
dialect := cfg.Dialect()
db, err = NewConnection(uri.String(), dialect, cfg)
Expand Down
1 change: 0 additions & 1 deletion core/services/pipeline/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ type (
Config interface {
BridgeResponseURL() *url.URL
BridgeCacheTTL() time.Duration
DatabaseURL() url.URL
DefaultHTTPLimit() int64
DefaultHTTPTimeout() models.Duration
JobPipelineMaxRunDuration() time.Duration
Expand Down
14 changes: 0 additions & 14 deletions core/services/pipeline/mocks/config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion core/services/pipeline/task.http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func TestHTTPTask_OverrideURLSafe(t *testing.T) {
RequestData: ethUSDPairing,
}
// Use real clients here to actually test the local connection blocking
r := clhttp.NewRestrictedHTTPClient(config, logger.TestLogger(t))
r := clhttp.NewRestrictedHTTPClient(config.Database(), logger.TestLogger(t))
u := clhttp.NewUnrestrictedHTTPClient()
task.HelperSetDependencies(config, r, u)

Expand Down
2 changes: 1 addition & 1 deletion core/utils/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

type httpClientConfig interface {
DatabaseURL() url.URL
URL() url.URL // DatabaseURL
}

// NewRestrictedHTTPClient returns a secure HTTP Client (queries to certain
Expand Down
2 changes: 1 addition & 1 deletion core/utils/http/http_allowed_ips.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func isRestrictedIP(ip net.IP, cfg httpClientConfig) (bool, error) {
}

func isBlacklistedIP(ip net.IP, cfg httpClientConfig) (bool, error) {
dbURL := cfg.DatabaseURL()
dbURL := cfg.URL()
if dbURL.String() == "" {
return false, nil
}
Expand Down
Loading

0 comments on commit 2421ed2

Please sign in to comment.