Merge pull request #66 from 8thlight/postgres

Refactor postgres to be initialized with config
This commit is contained in:
Matt K 2017-11-09 12:47:34 -06:00 committed by GitHub
commit c0f737868b
5 changed files with 28 additions and 34 deletions

View File

@ -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)
}

View File

@ -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,

View File

@ -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`)

View File

@ -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
})
})

View File

@ -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")
}