refactor sql constructors
This commit is contained in:
parent
2e70a798e2
commit
75701bb45c
@ -70,12 +70,12 @@ func NewStateDiffIndexer(
|
|||||||
var driver sql.Driver
|
var driver sql.Driver
|
||||||
switch pgc.Driver {
|
switch pgc.Driver {
|
||||||
case postgres.PGX:
|
case postgres.PGX:
|
||||||
driver, err = postgres.NewPGXDriver(ctx, pgc, nodeInfo)
|
driver, err = postgres.ConnectPGXDriver(ctx, pgc, nodeInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
case postgres.SQLX:
|
case postgres.SQLX:
|
||||||
driver, err = postgres.NewSQLXDriver(ctx, pgc, nodeInfo)
|
driver, err = postgres.ConnectSQLXDriver(ctx, pgc, nodeInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
@ -37,7 +37,6 @@ type PGXDriver struct {
|
|||||||
ctx context.Context
|
ctx context.Context
|
||||||
pool *pgxpool.Pool
|
pool *pgxpool.Pool
|
||||||
nodeInfo node.Info
|
nodeInfo node.Info
|
||||||
nodeID string
|
|
||||||
config Config
|
config Config
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,21 +49,25 @@ func ConnectPGX(ctx context.Context, config Config) (*pgxpool.Pool, error) {
|
|||||||
return pgxpool.ConnectConfig(ctx, pgConf)
|
return pgxpool.ConnectConfig(ctx, pgConf)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewPGXDriver returns a new pgx driver
|
// ConnectPGXDriver returns a new pgx driver
|
||||||
// it initializes the connection pool and creates the node info table
|
// it initializes the connection pool and creates the node info table
|
||||||
func NewPGXDriver(ctx context.Context, config Config, node node.Info) (*PGXDriver, error) {
|
func ConnectPGXDriver(ctx context.Context, config Config, node node.Info) (*PGXDriver, error) {
|
||||||
dbPool, err := ConnectPGX(ctx, config)
|
dbPool, err := ConnectPGX(ctx, config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, ErrDBConnectionFailed(err)
|
return nil, ErrDBConnectionFailed(err)
|
||||||
}
|
}
|
||||||
pg := &PGXDriver{ctx: ctx, pool: dbPool, nodeInfo: node, config: config}
|
pg := NewPGXDriver(ctx, dbPool, config)
|
||||||
nodeErr := pg.createNode()
|
nodeErr := pg.createNode(node)
|
||||||
if nodeErr != nil {
|
if nodeErr != nil {
|
||||||
return &PGXDriver{}, ErrUnableToSetNode(nodeErr)
|
return &PGXDriver{}, ErrUnableToSetNode(nodeErr)
|
||||||
}
|
}
|
||||||
return pg, nil
|
return pg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewPGXDriver(ctx context.Context, pool *pgxpool.Pool, config Config) *PGXDriver {
|
||||||
|
return &PGXDriver{ctx: ctx, pool: pool, config: config}
|
||||||
|
}
|
||||||
|
|
||||||
// MakeConfig creates a pgxpool.Config from the provided Config
|
// MakeConfig creates a pgxpool.Config from the provided Config
|
||||||
func MakeConfig(config Config) (*pgxpool.Config, error) {
|
func MakeConfig(config Config) (*pgxpool.Config, error) {
|
||||||
conf, err := pgxpool.ParseConfig("")
|
conf, err := pgxpool.ParseConfig("")
|
||||||
@ -102,19 +105,19 @@ func MakeConfig(config Config) (*pgxpool.Config, error) {
|
|||||||
return conf, nil
|
return conf, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pgx *PGXDriver) createNode() error {
|
func (pgx *PGXDriver) createNode(nodeInfo node.Info) error {
|
||||||
_, err := pgx.pool.Exec(
|
_, err := pgx.pool.Exec(
|
||||||
pgx.ctx,
|
pgx.ctx,
|
||||||
createNodeStm,
|
createNodeStm,
|
||||||
pgx.nodeInfo.GenesisBlock,
|
nodeInfo.GenesisBlock,
|
||||||
pgx.nodeInfo.NetworkID,
|
nodeInfo.NetworkID,
|
||||||
pgx.nodeInfo.ID,
|
nodeInfo.ID,
|
||||||
pgx.nodeInfo.ClientName,
|
nodeInfo.ClientName,
|
||||||
pgx.nodeInfo.ChainID)
|
nodeInfo.ChainID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ErrUnableToSetNode(err)
|
return ErrUnableToSetNode(err)
|
||||||
}
|
}
|
||||||
pgx.nodeID = pgx.nodeInfo.ID
|
pgx.nodeInfo = nodeInfo
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -155,7 +158,7 @@ func (pgx *PGXDriver) Stats() metrics.DbStats {
|
|||||||
|
|
||||||
// NodeID satisfies sql.Database
|
// NodeID satisfies sql.Database
|
||||||
func (pgx *PGXDriver) NodeID() string {
|
func (pgx *PGXDriver) NodeID() string {
|
||||||
return pgx.nodeID
|
return pgx.nodeInfo.ID
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close satisfies sql.Database/io.Closer
|
// Close satisfies sql.Database/io.Closer
|
||||||
|
@ -94,7 +94,7 @@ func TestPostgresPGX(t *testing.T) {
|
|||||||
|
|
||||||
t.Run("throws error when can't connect to the database", func(t *testing.T) {
|
t.Run("throws error when can't connect to the database", func(t *testing.T) {
|
||||||
goodInfo := node.Info{GenesisBlock: "GENESIS", NetworkID: "1", ID: "x123", ClientName: "geth"}
|
goodInfo := node.Info{GenesisBlock: "GENESIS", NetworkID: "1", ID: "x123", ClientName: "geth"}
|
||||||
_, err := postgres.NewPGXDriver(ctx, postgres.Config{}, goodInfo)
|
_, err := postgres.ConnectPGXDriver(ctx, postgres.Config{}, goodInfo)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatal("Expected an error")
|
t.Fatal("Expected an error")
|
||||||
}
|
}
|
||||||
@ -106,7 +106,7 @@ func TestPostgresPGX(t *testing.T) {
|
|||||||
badHash := fmt.Sprintf("x %s", strings.Repeat("1", 100))
|
badHash := fmt.Sprintf("x %s", strings.Repeat("1", 100))
|
||||||
badInfo := node.Info{GenesisBlock: badHash, NetworkID: "1", ID: "x123", ClientName: "geth"}
|
badInfo := node.Info{GenesisBlock: badHash, NetworkID: "1", ID: "x123", ClientName: "geth"}
|
||||||
|
|
||||||
_, err := postgres.NewPGXDriver(ctx, pgConfig, badInfo)
|
_, err := postgres.ConnectPGXDriver(ctx, pgConfig, badInfo)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatal("Expected an error")
|
t.Fatal("Expected an error")
|
||||||
}
|
}
|
||||||
|
@ -34,7 +34,6 @@ type SQLXDriver struct {
|
|||||||
ctx context.Context
|
ctx context.Context
|
||||||
db *sqlx.DB
|
db *sqlx.DB
|
||||||
nodeInfo node.Info
|
nodeInfo node.Info
|
||||||
nodeID string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ConnectSQLX initializes and returns a SQLX connection pool for postgres
|
// ConnectSQLX initializes and returns a SQLX connection pool for postgres
|
||||||
@ -53,32 +52,36 @@ func ConnectSQLX(ctx context.Context, config Config) (*sqlx.DB, error) {
|
|||||||
return db, nil
|
return db, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewSQLXDriver returns a new sqlx driver for Postgres
|
// ConnectSQLXDriver returns a new sqlx driver for Postgres
|
||||||
// it initializes the connection pool and creates the node info table
|
// it initializes the connection pool and creates the node info table
|
||||||
func NewSQLXDriver(ctx context.Context, config Config, node node.Info) (*SQLXDriver, error) {
|
func ConnectSQLXDriver(ctx context.Context, config Config, node node.Info) (*SQLXDriver, error) {
|
||||||
db, err := ConnectSQLX(ctx, config)
|
db, err := ConnectSQLX(ctx, config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
driver := &SQLXDriver{ctx: ctx, db: db, nodeInfo: node}
|
driver := NewSQLXDriver(ctx, db)
|
||||||
if err := driver.createNode(); err != nil {
|
if err := driver.createNode(node); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return driver, nil
|
return driver, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (driver *SQLXDriver) createNode() error {
|
func NewSQLXDriver(ctx context.Context, db *sqlx.DB) *SQLXDriver {
|
||||||
|
return &SQLXDriver{ctx: ctx, db: db}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (driver *SQLXDriver) createNode(nodeInfo node.Info) error {
|
||||||
_, err := driver.db.Exec(
|
_, err := driver.db.Exec(
|
||||||
createNodeStm,
|
createNodeStm,
|
||||||
driver.nodeInfo.GenesisBlock,
|
nodeInfo.GenesisBlock,
|
||||||
driver.nodeInfo.NetworkID,
|
nodeInfo.NetworkID,
|
||||||
driver.nodeInfo.ID,
|
nodeInfo.ID,
|
||||||
driver.nodeInfo.ClientName,
|
nodeInfo.ClientName,
|
||||||
driver.nodeInfo.ChainID)
|
nodeInfo.ChainID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ErrUnableToSetNode(err)
|
return ErrUnableToSetNode(err)
|
||||||
}
|
}
|
||||||
driver.nodeID = driver.nodeInfo.ID
|
driver.nodeInfo = nodeInfo
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -118,7 +121,7 @@ func (driver *SQLXDriver) Stats() metrics.DbStats {
|
|||||||
|
|
||||||
// NodeID satisfies sql.Database
|
// NodeID satisfies sql.Database
|
||||||
func (driver *SQLXDriver) NodeID() string {
|
func (driver *SQLXDriver) NodeID() string {
|
||||||
return driver.nodeID
|
return driver.nodeInfo.ID
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close satisfies sql.Database/io.Closer
|
// Close satisfies sql.Database/io.Closer
|
||||||
|
@ -97,7 +97,7 @@ func TestPostgresSQLX(t *testing.T) {
|
|||||||
|
|
||||||
t.Run("throws error when can't connect to the database", func(t *testing.T) {
|
t.Run("throws error when can't connect to the database", func(t *testing.T) {
|
||||||
goodInfo := node.Info{GenesisBlock: "GENESIS", NetworkID: "1", ID: "x123", ClientName: "geth"}
|
goodInfo := node.Info{GenesisBlock: "GENESIS", NetworkID: "1", ID: "x123", ClientName: "geth"}
|
||||||
_, err := postgres.NewSQLXDriver(ctx, postgres.Config{}, goodInfo)
|
_, err := postgres.ConnectSQLXDriver(ctx, postgres.Config{}, goodInfo)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatal("Expected an error")
|
t.Fatal("Expected an error")
|
||||||
}
|
}
|
||||||
@ -109,7 +109,7 @@ func TestPostgresSQLX(t *testing.T) {
|
|||||||
badHash := fmt.Sprintf("x %s", strings.Repeat("1", 100))
|
badHash := fmt.Sprintf("x %s", strings.Repeat("1", 100))
|
||||||
badInfo := node.Info{GenesisBlock: badHash, NetworkID: "1", ID: "x123", ClientName: "geth"}
|
badInfo := node.Info{GenesisBlock: badHash, NetworkID: "1", ID: "x123", ClientName: "geth"}
|
||||||
|
|
||||||
_, err := postgres.NewSQLXDriver(ctx, pgConfig, badInfo)
|
_, err := postgres.ConnectSQLXDriver(ctx, pgConfig, badInfo)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatal("Expected an error")
|
t.Fatal("Expected an error")
|
||||||
}
|
}
|
||||||
|
@ -30,7 +30,7 @@ func SetupSQLXDB() (sql.Database, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
conf.MaxIdle = 0
|
conf.MaxIdle = 0
|
||||||
driver, err := NewSQLXDriver(context.Background(), conf, node.Info{})
|
driver, err := ConnectSQLXDriver(context.Background(), conf, node.Info{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -39,7 +39,7 @@ func SetupSQLXDB() (sql.Database, error) {
|
|||||||
|
|
||||||
// SetupPGXDB is used to setup a pgx db for tests
|
// SetupPGXDB is used to setup a pgx db for tests
|
||||||
func SetupPGXDB(config Config) (sql.Database, error) {
|
func SetupPGXDB(config Config) (sql.Database, error) {
|
||||||
driver, err := NewPGXDriver(context.Background(), config, node.Info{})
|
driver, err := ConnectPGXDriver(context.Background(), config, node.Info{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user