Skip to content

Commit

Permalink
Replace dialects imports with pgcommon
Browse files Browse the repository at this point in the history
  • Loading branch information
reductionista committed Nov 1, 2024
1 parent c78761e commit 6d01bb7
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 36 deletions.
11 changes: 6 additions & 5 deletions core/cmd/shell_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import (

cutils "github.com/smartcontractkit/chainlink-common/pkg/utils"

pgcommon "github.com/smartcontractkit/chainlink-common/pkg/pg"

"github.com/smartcontractkit/chainlink/v2/core/build"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/assets"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/gas"
Expand All @@ -47,7 +49,6 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/sessions"
"github.com/smartcontractkit/chainlink/v2/core/shutdown"
"github.com/smartcontractkit/chainlink/v2/core/static"
"github.com/smartcontractkit/chainlink/v2/core/store/dialects"
"github.com/smartcontractkit/chainlink/v2/core/store/migrate"
"github.com/smartcontractkit/chainlink/v2/core/utils"
"github.com/smartcontractkit/chainlink/v2/core/web"
Expand Down Expand Up @@ -807,7 +808,7 @@ func (s *Shell) PrepareTestDatabase(c *cli.Context) error {

// Creating pristine DB copy to speed up FullTestDB
dbUrl := cfg.Database().URL()
db, err := sqlx.Open(string(dialects.Postgres), dbUrl.String())
db, err := sqlx.Open(string(pgcommon.Postgres), dbUrl.String())
if err != nil {
return s.errorOut(err)
}
Expand Down Expand Up @@ -1090,7 +1091,7 @@ type dbConfig interface {
MaxOpenConns() int
MaxIdleConns() int
URL() url.URL
Dialect() dialects.DialectName
Dialect() pgcommon.DialectName
}

func newConnection(ctx context.Context, cfg dbConfig) (*sqlx.DB, error) {
Expand All @@ -1106,7 +1107,7 @@ func dropAndCreateDB(parsed url.URL, force bool) (err error) {
// to a different one. template1 should be present on all postgres installations
dbname := parsed.Path[1:]
parsed.Path = "/template1"
db, err := sql.Open(string(dialects.Postgres), parsed.String())
db, err := sql.Open(string(pgcommon.Postgres), parsed.String())
if err != nil {
return fmt.Errorf("unable to open postgres database for creating test db: %+v", err)
}
Expand Down Expand Up @@ -1205,7 +1206,7 @@ func checkSchema(dbURL url.URL, prevSchema string) error {
}

func insertFixtures(dbURL url.URL, pathToFixtures string) (err error) {
db, err := sql.Open(string(dialects.Postgres), dbURL.String())
db, err := sql.Open(string(pgcommon.Postgres), dbURL.String())
if err != nil {
return fmt.Errorf("unable to open postgres database for creating test db: %+v", err)
}
Expand Down
10 changes: 5 additions & 5 deletions core/cmd/shell_local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

commonconfig "github.com/smartcontractkit/chainlink-common/pkg/config"
pgcommon "github.com/smartcontractkit/chainlink-common/pkg/pg"
"github.com/smartcontractkit/chainlink-common/pkg/utils/mailbox"

"github.com/smartcontractkit/chainlink/v2/common/client"
Expand All @@ -29,7 +30,6 @@ import (
chainlinkmocks "github.com/smartcontractkit/chainlink/v2/core/services/chainlink/mocks"
evmrelayer "github.com/smartcontractkit/chainlink/v2/core/services/relay/evm"
"github.com/smartcontractkit/chainlink/v2/core/sessions/localauth"
"github.com/smartcontractkit/chainlink/v2/core/store/dialects"
"github.com/smartcontractkit/chainlink/v2/core/store/models"
"github.com/smartcontractkit/chainlink/v2/core/utils"
"github.com/smartcontractkit/chainlink/v2/core/utils/testutils/heavyweight"
Expand Down Expand Up @@ -283,7 +283,7 @@ func TestShell_RebroadcastTransactions_Txm(t *testing.T) {
// test multiple connections to the database, and changes made within
// the transaction cannot be seen from another connection.
config, sqlxDB := heavyweight.FullTestDBV2(t, func(c *chainlink.Config, s *chainlink.Secrets) {
c.Database.Dialect = dialects.Postgres
c.Database.Dialect = pgcommon.Postgres
// evm config is used in this test. but if set, it must be pass config validation.
// simplest to make it nil
c.EVM = nil
Expand Down Expand Up @@ -363,7 +363,7 @@ func TestShell_RebroadcastTransactions_OutsideRange_Txm(t *testing.T) {
// test multiple connections to the database, and changes made within
// the transaction cannot be seen from another connection.
config, sqlxDB := heavyweight.FullTestDBV2(t, func(c *chainlink.Config, s *chainlink.Secrets) {
c.Database.Dialect = dialects.Postgres
c.Database.Dialect = pgcommon.Postgres
// evm config is used in this test. but if set, it must be pass config validation.
// simplest to make it nil
c.EVM = nil
Expand Down Expand Up @@ -441,7 +441,7 @@ func TestShell_RebroadcastTransactions_AddressCheck(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
config, sqlxDB := heavyweight.FullTestDBV2(t, func(c *chainlink.Config, s *chainlink.Secrets) {
c.Database.Dialect = dialects.Postgres
c.Database.Dialect = pgcommon.Postgres

c.EVM = nil
// seems to be needed for config validate
Expand Down Expand Up @@ -499,7 +499,7 @@ func TestShell_RebroadcastTransactions_AddressCheck(t *testing.T) {
func TestShell_CleanupChainTables(t *testing.T) {
// Just check if it doesn't error, command itself shouldn't be changed unless major schema changes were made.
// It would be really hard to write a test that accounts for schema changes, so this should be enough to alarm us that something broke.
config, _ := heavyweight.FullTestDBV2(t, func(c *chainlink.Config, s *chainlink.Secrets) { c.Database.Dialect = dialects.Postgres })
config, _ := heavyweight.FullTestDBV2(t, func(c *chainlink.Config, s *chainlink.Secrets) { c.Database.Dialect = pgcommon.Postgres })
client := cmd.Shell{
Config: config,
Logger: logger.TestLogger(t),
Expand Down
4 changes: 2 additions & 2 deletions core/config/database_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"net/url"
"time"

"github.com/smartcontractkit/chainlink/v2/core/store/dialects"
pgcommon "github.com/smartcontractkit/chainlink-common/pkg/pg"
)

type Backup interface {
Expand Down Expand Up @@ -35,7 +35,7 @@ type Database interface {
DefaultIdleInTxSessionTimeout() time.Duration
DefaultLockTimeout() time.Duration
DefaultQueryTimeout() time.Duration
Dialect() dialects.DialectName
Dialect() pgcommon.DialectName
LogSQL() bool
MaxIdleConns() int
MaxOpenConns() int
Expand Down
5 changes: 3 additions & 2 deletions core/config/docs/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
"strings"

"github.com/smartcontractkit/chainlink-common/pkg/config"
pgcommon "github.com/smartcontractkit/chainlink-common/pkg/pg"

"github.com/smartcontractkit/chainlink/v2/core/config/toml"
"github.com/smartcontractkit/chainlink/v2/core/services/chainlink/cfgtest"
"github.com/smartcontractkit/chainlink/v2/core/store/dialects"
)

var (
Expand All @@ -22,7 +23,7 @@ func init() {

func CoreDefaults() (c toml.Core) {
c.SetFrom(&defaults)
c.Database.Dialect = dialects.Postgres // not user visible - overridden for tests only
c.Database.Dialect = pgcommon.Postgres // not user visible - overridden for tests only
c.Tracing.Attributes = make(map[string]string)
return
}
4 changes: 2 additions & 2 deletions core/config/toml/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"

"github.com/google/uuid"
pgcommon "github.com/smartcontractkit/chainlink-common/pkg/pg"
"go.uber.org/multierr"
"go.uber.org/zap/zapcore"

Expand All @@ -23,7 +24,6 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/config/parse"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/p2pkey"
"github.com/smartcontractkit/chainlink/v2/core/sessions"
"github.com/smartcontractkit/chainlink/v2/core/store/dialects"
"github.com/smartcontractkit/chainlink/v2/core/store/models"
"github.com/smartcontractkit/chainlink/v2/core/utils"

Expand Down Expand Up @@ -339,7 +339,7 @@ type Database struct {
DefaultIdleInTxSessionTimeout *commonconfig.Duration
DefaultLockTimeout *commonconfig.Duration
DefaultQueryTimeout *commonconfig.Duration
Dialect dialects.DialectName `toml:"-"`
Dialect pgcommon.DialectName `toml:"-"`
LogQueries *bool
MaxIdleConns *int64
MaxOpenConns *int64
Expand Down
4 changes: 2 additions & 2 deletions core/internal/testutils/configtest/general_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
"github.com/stretchr/testify/require"

commonconfig "github.com/smartcontractkit/chainlink-common/pkg/config"

"github.com/smartcontractkit/chainlink/v2/core/chains/evm/client"
evmclient "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client"
evmcfg "github.com/smartcontractkit/chainlink/v2/core/chains/evm/config/toml"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/utils/big"
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils"
"github.com/smartcontractkit/chainlink/v2/core/services/chainlink"
"github.com/smartcontractkit/chainlink/v2/core/store/dialects"
"github.com/smartcontractkit/chainlink/v2/core/store/models"
)

Expand Down Expand Up @@ -48,7 +48,7 @@ func overrides(c *chainlink.Config, s *chainlink.Secrets) {
c.InsecureFastScrypt = ptr(true)
c.ShutdownGracePeriod = commonconfig.MustNewDuration(testutils.DefaultWaitTimeout)

c.Database.Dialect = dialects.TransactionWrappedPostgres
c.Database.Dialect = pgcommon.TransactionWrappedPostgres
c.Database.Lock.Enabled = ptr(false)
c.Database.MaxIdleConns = ptr[int64](20)
c.Database.MaxOpenConns = ptr[int64](20)
Expand Down
5 changes: 3 additions & 2 deletions core/services/chainlink/config_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"net/url"
"time"

pgcommon "github.com/smartcontractkit/chainlink-common/pkg/pg"

"github.com/smartcontractkit/chainlink/v2/core/config"
"github.com/smartcontractkit/chainlink/v2/core/config/toml"
"github.com/smartcontractkit/chainlink/v2/core/store/dialects"
)

type backupConfig struct {
Expand Down Expand Up @@ -109,7 +110,7 @@ func (d *databaseConfig) URL() url.URL {
return *d.s.URL.URL()
}

func (d *databaseConfig) Dialect() dialects.DialectName {
func (d *databaseConfig) Dialect() pgcommon.DialectName {
return d.c.Dialect
}

Expand Down
4 changes: 2 additions & 2 deletions core/services/chainlink/config_database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"testing"
"time"

pgcommon "github.com/smartcontractkit/chainlink-common/pkg/pg"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/smartcontractkit/chainlink/v2/core/config"
"github.com/smartcontractkit/chainlink/v2/core/store/dialects"
)

func TestDatabaseConfig(t *testing.T) {
Expand All @@ -35,7 +35,7 @@ URL = "postgresql://doesnotexist:justtopassvalidationtests@localhost:5432/chainl
assert.Equal(t, db.MaxIdleConns(), 7)
assert.Equal(t, db.MaxOpenConns(), 13)
assert.Equal(t, db.MigrateDatabase(), true)
assert.Equal(t, db.Dialect(), dialects.Postgres)
assert.Equal(t, db.Dialect(), pgcommon.Postgres)
url := db.URL()
assert.NotEqual(t, url.String(), "")

Expand Down
6 changes: 3 additions & 3 deletions core/services/pg/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"go.opentelemetry.io/otel"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"

"github.com/smartcontractkit/chainlink/v2/core/store/dialects"
pgcommon "github.com/smartcontractkit/chainlink-common/pkg/pg"
)

// NOTE: This is the default level in Postgres anyway, we just make it
Expand Down Expand Up @@ -51,7 +51,7 @@ type ConnectionConfig interface {
MaxIdleConns() int
}

func NewConnection(ctx context.Context, uri string, dialect dialects.DialectName, config ConnectionConfig) (*sqlx.DB, error) {
func NewConnection(ctx context.Context, uri string, dialect pgcommon.DialectName, config ConnectionConfig) (*sqlx.DB, error) {
opts := []otelsql.Option{otelsql.WithAttributes(semconv.DBSystemPostgreSQL),
otelsql.WithTracerProvider(otel.GetTracerProvider()),
otelsql.WithSQLCommenter(true),
Expand All @@ -70,7 +70,7 @@ func NewConnection(ctx context.Context, uri string, dialect dialects.DialectName
lockTimeout, idleInTxSessionTimeout, defaultIsolation.String())

var sqldb *sql.DB
if dialect == dialects.TransactionWrappedPostgres {
if dialect == pgcommon.TransactionWrappedPostgres {
// Dbtx uses the uri as a unique identifier for each transaction. Each ORM
// should be encapsulated in it's own transaction, and thus needs its own
// unique id.
Expand Down
3 changes: 1 addition & 2 deletions core/services/pg/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/stretchr/testify/require"

"github.com/smartcontractkit/chainlink/v2/core/internal/testutils"
"github.com/smartcontractkit/chainlink/v2/core/store/dialects"
)

var _ Getter = &mockGetter{}
Expand Down Expand Up @@ -67,7 +66,7 @@ func Test_checkVersion(t *testing.T) {

func Test_disallowReplica(t *testing.T) {
testutils.SkipShortDB(t)
db, err := sqlx.Open(string(dialects.TransactionWrappedPostgres), uuid.New().String())
db, err := sqlx.Open(string(pgcommon.TransactionWrappedPostgres), uuid.New().String())
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, db.Close()) })

Expand Down
5 changes: 3 additions & 2 deletions core/services/pg/locked_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import (

"github.com/google/uuid"
"github.com/pkg/errors"
pgcommon "github.com/smartcontractkit/chainlink-common/pkg/pg"

"github.com/jmoiron/sqlx"

"github.com/smartcontractkit/chainlink-common/pkg/services"

"github.com/smartcontractkit/chainlink/v2/core/config"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/static"
"github.com/smartcontractkit/chainlink/v2/core/store/dialects"
)

// LockedDB bounds DB connection and DB locks.
Expand All @@ -28,7 +29,7 @@ type LockedDBConfig interface {
ConnectionConfig
URL() url.URL
DefaultQueryTimeout() time.Duration
Dialect() dialects.DialectName
Dialect() pgcommon.DialectName
}

type lockedDb struct {
Expand Down
9 changes: 5 additions & 4 deletions core/utils/testutils/heavyweight/orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ import (

"github.com/jmoiron/sqlx"

pgcommon "github.com/smartcontractkit/chainlink-common/pkg/pg"
"github.com/smartcontractkit/chainlink-common/pkg/utils/tests"

"github.com/smartcontractkit/chainlink/v2/core/internal/testutils"
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest"
"github.com/smartcontractkit/chainlink/v2/core/services/chainlink"
"github.com/smartcontractkit/chainlink/v2/core/services/pg"
"github.com/smartcontractkit/chainlink/v2/core/store/dialects"
"github.com/smartcontractkit/chainlink/v2/core/store/models"
"github.com/smartcontractkit/chainlink/v2/internal/testdb"
)
Expand Down Expand Up @@ -56,7 +57,7 @@ func (c Kind) PrepareDB(t testing.TB, overrideFn func(c *chainlink.Config, s *ch
testutils.SkipShort(t, "FullTestDB")

gcfg := configtest.NewGeneralConfigSimulated(t, func(c *chainlink.Config, s *chainlink.Secrets) {
c.Database.Dialect = dialects.Postgres
c.Database.Dialect = pgcommon.Postgres
if overrideFn != nil {
overrideFn(c, s)
}
Expand All @@ -65,7 +66,7 @@ func (c Kind) PrepareDB(t testing.TB, overrideFn func(c *chainlink.Config, s *ch
require.NoError(t, os.MkdirAll(gcfg.RootDir(), 0700))
migrationTestDBURL, err := testdb.CreateOrReplace(gcfg.Database().URL(), generateName(), c != KindEmpty)
require.NoError(t, err)
db, err := pg.NewConnection(tests.Context(t), migrationTestDBURL, dialects.Postgres, gcfg.Database())
db, err := pg.NewConnection(tests.Context(t), migrationTestDBURL, pgcommon.Postgres, gcfg.Database())
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, db.Close()) // must close before dropping
Expand All @@ -74,7 +75,7 @@ func (c Kind) PrepareDB(t testing.TB, overrideFn func(c *chainlink.Config, s *ch
})

gcfg = configtest.NewGeneralConfigSimulated(t, func(c *chainlink.Config, s *chainlink.Secrets) {
c.Database.Dialect = dialects.Postgres
c.Database.Dialect = pgcommon.Postgres
s.Database.URL = models.MustSecretURL(migrationTestDBURL)
if overrideFn != nil {
overrideFn(c, s)
Expand Down
6 changes: 3 additions & 3 deletions internal/testdb/testdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"net/url"
"strings"

"github.com/smartcontractkit/chainlink/v2/core/store/dialects"
pgcommon "github.com/smartcontractkit/chainlink-common/pkg/pg"
)

const (
Expand All @@ -33,7 +33,7 @@ func CreateOrReplace(parsed url.URL, suffix string, withTemplate bool) (string,
// Cannot drop test database if we are connected to it, so we must connect
// to a different one. 'postgres' should be present on all postgres installations
parsed.Path = "/postgres"
db, err := sql.Open(string(dialects.Postgres), parsed.String())
db, err := sql.Open(string(pgcommon.Postgres), parsed.String())
if err != nil {
return "", fmt.Errorf("in order to drop the test database, we need to connect to a separate database"+
" called 'postgres'. But we are unable to open 'postgres' database: %+v\n", err)
Expand Down Expand Up @@ -66,7 +66,7 @@ func Drop(dbURL url.URL) error {
// Cannot drop test database if we are connected to it, so we must connect
// to a different one. 'postgres' should be present on all postgres installations
dbURL.Path = "/postgres"
db, err := sql.Open(string(dialects.Postgres), dbURL.String())
db, err := sql.Open(string(pgcommon.Postgres), dbURL.String())
if err != nil {
return fmt.Errorf("in order to drop the test database, we need to connect to a separate database"+
" called 'postgres'. But we are unable to open 'postgres' database: %+v\n", err)
Expand Down

0 comments on commit 6d01bb7

Please sign in to comment.