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

Add pkg/pg with dialects.go & txdb.go #910

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
14 changes: 13 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ require (
github.com/hashicorp/go-plugin v1.6.2-0.20240829161738-06afb6d7ae99
github.com/iancoleman/strcase v0.3.0
github.com/invopop/jsonschema v0.12.0
github.com/jackc/pgx/v4 v4.18.3
github.com/jmoiron/sqlx v1.4.0
github.com/jonboulle/clockwork v0.4.0
github.com/jpillora/backoff v1.0.0
Expand All @@ -31,6 +32,7 @@ require (
github.com/prometheus/client_golang v1.17.0
github.com/riferrei/srclient v0.5.4
github.com/santhosh-tekuri/jsonschema/v5 v5.2.0
github.com/scylladb/go-reflectx v1.0.1
github.com/shopspring/decimal v1.4.0
github.com/smartcontractkit/grpc-proxy v0.0.0-20240830132753-a7e17fec5ab7
github.com/smartcontractkit/libocr v0.0.0-20241007185508-adbe57025f12
Expand Down Expand Up @@ -63,6 +65,16 @@ require (
sigs.k8s.io/yaml v1.4.0
)

require (
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.14.3 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.3 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgtype v1.14.0 // indirect
)

require (
github.com/bahlo/generic-list-go v0.2.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
Expand Down Expand Up @@ -100,7 +112,7 @@ require (
github.com/x448/float16 v0.8.4 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.30.0 // indirect
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/multierr v1.11.0
golang.org/x/mod v0.21.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sync v0.8.0 // indirect
Expand Down
132 changes: 132 additions & 0 deletions go.sum

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions pkg/pg/dialects.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package pg

import (
// need to make sure pgx driver is registered before opening connection
_ "github.com/jackc/pgx/v4/stdlib"
)

// DialectName is a compiler enforced type used that maps to database dialect names
type DialectName string

const (
// Postgres represents the postgres dialect.
Postgres DialectName = "pgx"
// TransactionWrappedPostgres is useful for tests.
// When the connection is opened, it starts a transaction and all
// operations performed on the DB will be within that transaction.
TransactionWrappedPostgres DialectName = "txdb"
)
28 changes: 28 additions & 0 deletions pkg/pg/pg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package pg

import (
"testing"

"github.com/google/uuid"
"github.com/jmoiron/sqlx"
"github.com/scylladb/go-reflectx"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

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

func NewSqlxDB(t testing.TB, dbURL string) *sqlx.DB {
tests.SkipShortDB(t)
err := RegisterTxDb(dbURL)
if err != nil {
t.Errorf("failed to register txdb dialect: %s", err.Error())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should not this be a fatal error?
Otherwise caller has to check if returned db is nil.

return nil
}
db, err := sqlx.Open(string(TransactionWrappedPostgres), uuid.New().String())
require.NoError(t, err)
t.Cleanup(func() { assert.NoError(t, db.Close()) })
db.MapperFunc(reflectx.CamelToSnakeASCII)

return db
}
Loading
Loading