Skip to content
This repository has been archived by the owner on Sep 28, 2021. It is now read-only.

Commit

Permalink
added new param createNode bool to NewDB postgres function. (#79)
Browse files Browse the repository at this point in the history
Usually indexer instance add new entry to public.nodes tables and it used in eth.header_cid relationship to understand from which node header came from. But ipld-eth-server and tracing-api also uses ipld-eth-indexer as library but they don't need to insert this entity to database
  • Loading branch information
ramilexe authored Mar 8, 2021
1 parent bb02ce3 commit c14f623
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pkg/historical/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func NewConfig() (*Config, error) {

c.DBConfig.Init()
overrideDBConnConfig(&c.DBConfig)
db := utils.LoadPostgres(c.DBConfig, c.NodeInfo)
db := utils.LoadPostgres(c.DBConfig, c.NodeInfo, true)
c.DB = &db
return c, nil
}
Expand Down
12 changes: 8 additions & 4 deletions pkg/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type DB struct {
NodeID int64
}

func NewDB(databaseConfig Config, node node.Info) (*DB, error) {
func NewDB(databaseConfig Config, node node.Info, createNode bool) (*DB, error) {
connectString := DbConnectionString(databaseConfig)
db, connectErr := sqlx.Connect("postgres", connectString)
if connectErr != nil {
Expand All @@ -49,10 +49,14 @@ func NewDB(databaseConfig Config, node node.Info) (*DB, error) {
db.SetConnMaxLifetime(lifetime)
}
pg := DB{DB: db, Node: node}
nodeErr := pg.CreateNode(&node)
if nodeErr != nil {
return &DB{}, ErrUnableToSetNode(nodeErr)

if createNode {
nodeErr := pg.CreateNode(&node)
if nodeErr != nil {
return &DB{}, ErrUnableToSetNode(nodeErr)
}
}

return &pg, nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ var _ = Describe("Postgres DB", func() {
invalidDatabase := postgres.Config{}
node := node.Info{GenesisBlock: "GENESIS", NetworkID: "1", ID: "x123", ClientName: "geth"}

_, err := postgres.NewDB(invalidDatabase, node)
_, err := postgres.NewDB(invalidDatabase, node, true)

Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring(postgres.DbConnectionFailedMsg))
Expand Down
2 changes: 1 addition & 1 deletion pkg/resync/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func NewConfig() (*Config, error) {

c.DBConfig.Init()
overrideDBConnConfig(&c.DBConfig)
db := utils.LoadPostgres(c.DBConfig, c.NodeInfo)
db := utils.LoadPostgres(c.DBConfig, c.NodeInfo, true)
c.DB = &db
return c, nil
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/shared/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ import (

// SetupDB is use to setup a db for watcher tests
func SetupDB() (*postgres.DB, error) {
return postgres.NewDB(getTestConfig(), node.Info{})
return postgres.NewDB(getTestConfig(), node.Info{}, true)
}

func SetupDBWithNode(node node.Info) (*postgres.DB, error) {
return postgres.NewDB(getTestConfig(), node)
return postgres.NewDB(getTestConfig(), node, true)
}

func getTestConfig() postgres.Config {
Expand Down
2 changes: 1 addition & 1 deletion pkg/sync/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func NewConfig() (*Config, error) {

c.DBConfig.Init()
overrideDBConnConfig(&c.DBConfig)
syncDB := utils.LoadPostgres(c.DBConfig, c.NodeInfo)
syncDB := utils.LoadPostgres(c.DBConfig, c.NodeInfo, true)
c.DB = &syncDB
return c, nil
}
Expand Down
4 changes: 2 additions & 2 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
"github.com/vulcanize/ipld-eth-indexer/pkg/postgres"
)

func LoadPostgres(database postgres.Config, node node.Info) postgres.DB {
db, err := postgres.NewDB(database, node)
func LoadPostgres(database postgres.Config, node node.Info, createNode bool) postgres.DB {
db, err := postgres.NewDB(database, node, createNode)
if err != nil {
logrus.Fatal("Error loading postgres: ", err)
}
Expand Down

0 comments on commit c14f623

Please sign in to comment.