From 58fbeb32fda5086e3936afdfdae2f0db42ab7d3e Mon Sep 17 00:00:00 2001 From: Eric Meyer Date: Thu, 9 Nov 2017 08:51:14 -0600 Subject: [PATCH] Refactor postgres to be initialized with config --- cmd/populate_blocks/main.go | 8 +------- cmd/run/main.go | 8 +------- pkg/repositories/postgres.go | 15 +++++++++++---- pkg/repositories/repository_test.go | 23 +++++++---------------- pkg/repositories/testing/helpers.go | 8 ++++++++ 5 files changed, 28 insertions(+), 34 deletions(-) create mode 100644 pkg/repositories/testing/helpers.go diff --git a/cmd/populate_blocks/main.go b/cmd/populate_blocks/main.go index 19250fa7..8d34d2ef 100644 --- a/cmd/populate_blocks/main.go +++ b/cmd/populate_blocks/main.go @@ -10,7 +10,6 @@ import ( "github.com/8thlight/vulcanizedb/pkg/geth" "github.com/8thlight/vulcanizedb/pkg/history" "github.com/8thlight/vulcanizedb/pkg/repositories" - "github.com/jmoiron/sqlx" ) func main() { @@ -23,12 +22,7 @@ func main() { } blockchain := geth.NewGethBlockchain(cfg.Client.IPCPath) - connectString := config.DbConnectionString(cfg.Database) - db, err := sqlx.Connect("postgres", connectString) - if err != nil { - log.Fatalf("Error connecting to DB: %v\n", err) - } - repository := repositories.NewPostgres(db) + repository := repositories.NewPostgres(cfg.Database) numberOfBlocksCreated := history.PopulateBlocks(blockchain, repository, int64(*startingBlockNumber)) fmt.Printf("Populated %d blocks", numberOfBlocksCreated) } diff --git a/cmd/run/main.go b/cmd/run/main.go index 4b63e52f..a0225e06 100644 --- a/cmd/run/main.go +++ b/cmd/run/main.go @@ -12,7 +12,6 @@ import ( "github.com/8thlight/vulcanizedb/pkg/geth" "github.com/8thlight/vulcanizedb/pkg/observers" "github.com/8thlight/vulcanizedb/pkg/repositories" - "github.com/jmoiron/sqlx" ) func main() { @@ -26,12 +25,7 @@ func main() { fmt.Println("Client Path ", cfg.Client.IPCPath) blockchain := geth.NewGethBlockchain(cfg.Client.IPCPath) loggingObserver := observers.BlockchainLoggingObserver{} - connectString := config.DbConnectionString(cfg.Database) - db, err := sqlx.Connect("postgres", connectString) - if err != nil { - log.Fatalf("Error connecting to DB: %v\n", err) - } - repository := repositories.NewPostgres(db) + repository := repositories.NewPostgres(cfg.Database) dbObserver := observers.NewBlockchainDbObserver(repository) listener := blockchain_listener.NewBlockchainListener(blockchain, []core.BlockchainObserver{ loggingObserver, diff --git a/pkg/repositories/postgres.go b/pkg/repositories/postgres.go index 57b88b09..72cdd660 100644 --- a/pkg/repositories/postgres.go +++ b/pkg/repositories/postgres.go @@ -2,11 +2,13 @@ package repositories import ( "database/sql" + "log" "context" "errors" + "github.com/8thlight/vulcanizedb/pkg/config" "github.com/8thlight/vulcanizedb/pkg/core" "github.com/jmoiron/sqlx" _ "github.com/lib/pq" @@ -16,6 +18,15 @@ type Postgres struct { Db *sqlx.DB } +func NewPostgres(databaseConfig config.Database) Postgres { + connectString := config.DbConnectionString(databaseConfig) + db, err := sqlx.Connect("postgres", connectString) + if err != nil { + log.Fatalf("Error connecting to DB: %v\n", err) + } + return Postgres{Db: db} +} + var ( ErrDBInsertFailed = errors.New("postgres: insert failed") ) @@ -40,10 +51,6 @@ func (repository Postgres) MissingBlockNumbers(startingBlockNumber int64, highes return numbers } -func NewPostgres(db *sqlx.DB) Postgres { - return Postgres{Db: db} -} - func (repository Postgres) FindBlockByNumber(blockNumber int64) *core.Block { blockRows, _ := repository.Db.Query( `SELECT id, block_number, block_gaslimit, block_gasused, block_time, block_difficulty, block_hash, block_nonce, block_parenthash, block_size, uncle_hash FROM blocks`) diff --git a/pkg/repositories/repository_test.go b/pkg/repositories/repository_test.go index 797697b0..98e0fe28 100644 --- a/pkg/repositories/repository_test.go +++ b/pkg/repositories/repository_test.go @@ -7,6 +7,7 @@ import ( "github.com/8thlight/vulcanizedb/pkg/config" "github.com/8thlight/vulcanizedb/pkg/core" "github.com/8thlight/vulcanizedb/pkg/repositories" + "github.com/8thlight/vulcanizedb/pkg/repositories/testing" "github.com/jmoiron/sqlx" _ "github.com/lib/pq" . "github.com/onsi/ginkgo" @@ -225,7 +226,6 @@ var _ = Describe("Repositories", func() { }) It("does not commit block if block is invalid", func() { - //badNonce violates db Nonce field length badNonce := fmt.Sprintf("x %s", strings.Repeat("1", 100)) badBlock := core.Block{ @@ -234,10 +234,7 @@ var _ = Describe("Repositories", func() { Transactions: []core.Transaction{}, } cfg, _ := config.NewConfig("private") - pgConfig := config.DbConnectionString(cfg.Database) - db, _ := sqlx.Connect("postgres", pgConfig) - Expect(db).ShouldNot(BeNil()) - repository := repositories.NewPostgres(db) + repository := repositories.NewPostgres(cfg.Database) err := repository.CreateBlock(badBlock) savedBlock := repository.FindBlockByNumber(123) @@ -247,19 +244,15 @@ var _ = Describe("Repositories", func() { }) It("does not commit block or transactions if transaction is invalid", func() { - //badHash violates db To field length badHash := fmt.Sprintf("x %s", strings.Repeat("1", 100)) badTransaction := core.Transaction{To: badHash} - cfg, _ := config.NewConfig("private") - pgConfig := config.DbConnectionString(cfg.Database) block := core.Block{ Number: 123, Transactions: []core.Transaction{badTransaction}, } - db, _ := sqlx.Connect("postgres", pgConfig) - Expect(db).ShouldNot(BeNil()) - repository := repositories.NewPostgres(db) + cfg, _ := config.NewConfig("private") + repository := repositories.NewPostgres(cfg.Database) err := repository.CreateBlock(block) savedBlock := repository.FindBlockByNumber(123) @@ -270,11 +263,9 @@ var _ = Describe("Repositories", func() { AssertRepositoryBehavior(func() repositories.Repository { cfg, _ := config.NewConfig("private") - pgConfig := config.DbConnectionString(cfg.Database) - db, _ := sqlx.Connect("postgres", pgConfig) - db.MustExec("DELETE FROM transactions") - db.MustExec("DELETE FROM blocks") - return repositories.NewPostgres(db) + repository := repositories.NewPostgres(cfg.Database) + testing.ClearData(repository) + return repository }) }) diff --git a/pkg/repositories/testing/helpers.go b/pkg/repositories/testing/helpers.go new file mode 100644 index 00000000..e95407ef --- /dev/null +++ b/pkg/repositories/testing/helpers.go @@ -0,0 +1,8 @@ +package testing + +import "github.com/8thlight/vulcanizedb/pkg/repositories" + +func ClearData(postgres repositories.Postgres) { + postgres.Db.MustExec("DELETE FROM transactions") + postgres.Db.MustExec("DELETE FROM blocks") +}