Add the concept of multiple environments

* Right now, the two environments are Public and Private
    * This was to reflect the two blockchains we run against
This commit is contained in:
Eric Meyer 2017-11-01 10:17:01 -05:00
parent 121511f10d
commit 102beb11e3
12 changed files with 113 additions and 43 deletions

View File

@ -5,8 +5,8 @@ go:
services: services:
- postgresql - postgresql
before_script: before_script:
- createdb vulcanize - createdb vulcanize_private
- psql vulcanize < migrations/schema.sql - psql vulcanize_private < migrations/schema.sql
script: script:
- go test -v ./core/... - go test -v ./core/...
notifications: notifications:

View File

@ -3,29 +3,70 @@ package main
import ( import (
"log" "log"
"fmt"
cfg "github.com/8thlight/vulcanizedb/config"
"github.com/8thlight/vulcanizedb/core" "github.com/8thlight/vulcanizedb/core"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
do "gopkg.in/godo.v2" do "gopkg.in/godo.v2"
) )
func parseIpcPath(context *do.Context) string {
ipcPath := context.Args.MayString("", "ipc-path", "i")
if ipcPath == "" {
log.Fatalln("--ipc-path required")
}
return ipcPath
}
func startBlockchainListener(config cfg.Config, ipcPath string) {
port := config.Database.Port
host := config.Database.Hostname
databaseName := config.Database.Name
var blockchain core.Blockchain = core.NewGethBlockchain(ipcPath)
blockchain.RegisterObserver(core.BlockchainLoggingObserver{})
pgConfig := fmt.Sprintf("host=%s port=%d dbname=%s sslmode=disable", host, port, databaseName)
db, err := sqlx.Connect("postgres", pgConfig)
if err != nil {
log.Fatalf("Error connecting to DB: %v\n", err)
}
blockchain.RegisterObserver(core.BlockchainDBObserver{Db: db})
blockchain.SubscribeToEvents()
}
func tasks(p *do.Project) { func tasks(p *do.Project) {
p.Task("run", nil, func(context *do.Context) { p.Task("runPublic", nil, func(context *do.Context) {
ipcPath := context.Args.MayString("", "ipc-path", "i") startBlockchainListener(cfg.Public(), parseIpcPath(context))
})
port := "5432" p.Task("runPrivate", nil, func(context *do.Context) {
host := "localhost" startBlockchainListener(cfg.Private(), parseIpcPath(context))
databaseName := "vulcanize" })
var blockchain core.Blockchain = core.NewGethBlockchain(ipcPath) p.Task("migratePublic", nil, func(context *do.Context) {
blockchain.RegisterObserver(core.BlockchainLoggingObserver{}) connectString := cfg.DbConnectionString(cfg.Public().Database)
pgConfig := "host=" + host + " port=" + port + " dbname=" + databaseName + " sslmode=disable" context.Bash(fmt.Sprintf("migrate -database '%s' -path ./migrations up", connectString))
db, err := sqlx.Connect("postgres", pgConfig) context.Bash(fmt.Sprintf("pg_dump -O -s %s > migrations/schema.sql", cfg.Public().Database.Name))
if err != nil { })
log.Fatalf("Error connecting to DB: %v\n", err)
} p.Task("migratePrivate", nil, func(context *do.Context) {
blockchain.RegisterObserver(core.BlockchainDBObserver{Db: db}) connectString := cfg.DbConnectionString(cfg.Private().Database)
blockchain.SubscribeToEvents() context.Bash(fmt.Sprintf("migrate -database '%s' -path ./migrations up", connectString))
context.Bash(fmt.Sprintf("pg_dump -O -s %s > migrations/schema.sql", cfg.Private().Database.Name))
})
p.Task("rollbackPublic", nil, func(context *do.Context) {
connectString := cfg.DbConnectionString(cfg.Public().Database)
context.Bash(fmt.Sprintf("migrate -database '%s' -path ./migrations down 1", connectString))
context.Bash("pg_dump -O -s vulcanize_public > migrations/schema.sql")
})
p.Task("rollbackPrivate", nil, func(context *do.Context) {
connectString := cfg.DbConnectionString(cfg.Private().Database)
context.Bash(fmt.Sprintf("migrate -database '%s' -path ./migrations down 1", connectString))
context.Bash("pg_dump -O -s vulcanize_private > migrations/schema.sql")
}) })
} }

View File

@ -9,6 +9,8 @@
- Go 1.9+ - Go 1.9+
- https://github.com/golang/dep - https://github.com/golang/dep
- `go get -u github.com/golang/dep/cmd/dep` - `go get -u github.com/golang/dep/cmd/dep`
- https://github.com/go-godo/godo
- `go get -u gopkg.in/godo.v2/cmd/godo`
- Postgres 10 - Postgres 10
### Cloning the Repository ### Cloning the Repository
@ -22,15 +24,17 @@
5. `cd $GOPATH/src/github.com/8thlight/vulcanizedb` 5. `cd $GOPATH/src/github.com/8thlight/vulcanizedb`
6. `dep ensure` 6. `dep ensure`
### Setting up the Development Database ### Setting up the Databases
1. Install Postgres 1. Install Postgres
2. Create a superuser for yourself and make sure `psql --list` works without prompting for a password. 2. Create a superuser for yourself and make sure `psql --list` works without prompting for a password.
3. `go get -u -d github.com/mattes/migrate/cli github.com/lib/pq` 3. `go get -u -d github.com/mattes/migrate/cli github.com/lib/pq`
4. `go build -tags 'postgres' -o /usr/local/bin/migrate github.com/mattes/migrate/cli` 4. `go build -tags 'postgres' -o /usr/local/bin/migrate github.com/mattes/migrate/cli`
5. `createdb vulcanize` 5. `createdb vulcanize_public`
6. `cd $GOPATH/src/github.com/8thlight/vulcanizedb` 6. `createdb vulcanize_private`
7. `./scripts/migrate` 7. `cd $GOPATH/src/github.com/8thlight/vulcanizedb`
8. `godo migratePublic`
9. `godo migratePrivate`
Adding a new migration: `./scripts/create_migration <migration-name>` Adding a new migration: `./scripts/create_migration <migration-name>`
@ -60,7 +64,7 @@ The default location for Ethereum is:
1. Start a blockchain. 1. Start a blockchain.
2. In a separate terminal start listener (ipcDir location) 2. In a separate terminal start listener (ipcDir location)
- `go run main.go --ipcPath /path/to/file.ipc` - `godo runPublic -- --ipc-path /path/to/file.ipc`
## Running the Tests ## Running the Tests

5
config/config.go Normal file
View File

@ -0,0 +1,5 @@
package config
type Config struct {
Database Database
}

13
config/database.go Normal file
View File

@ -0,0 +1,13 @@
package config
import "fmt"
type Database struct {
Hostname string
Name string
Port int
}
func DbConnectionString(dbConfig Database) string {
return fmt.Sprintf("postgresql://%s:%d/%s?sslmode=disable", dbConfig.Hostname, dbConfig.Port, dbConfig.Name)
}

11
config/private.go Normal file
View File

@ -0,0 +1,11 @@
package config
func Private() Config {
return Config{
Database: Database{
Name: "vulcanize_private",
Hostname: "localhost",
Port: 5432,
},
}
}

11
config/public.go Normal file
View File

@ -0,0 +1,11 @@
package config
func Public() Config {
return Config{
Database: Database{
Name: "vulcanize_public",
Hostname: "localhost",
Port: 5432,
},
}
}

View File

@ -1,8 +1,7 @@
package core_test package core_test
import ( import (
"fmt" "github.com/8thlight/vulcanizedb/config"
"github.com/8thlight/vulcanizedb/core" "github.com/8thlight/vulcanizedb/core"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
_ "github.com/lib/pq" _ "github.com/lib/pq"
@ -10,28 +9,22 @@ import (
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
) )
const (
host = "localhost"
port = 5432
user = "postgres"
password = "postgres"
dbname = "vulcanize"
)
var _ = Describe("Saving blocks to the database", func() { var _ = Describe("Saving blocks to the database", func() {
var db *sqlx.DB var db *sqlx.DB
var err error var err error
pgConfig := fmt.Sprintf(
"host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
host, port, user, password, dbname)
BeforeEach(func() { BeforeEach(func() {
pgConfig := config.DbConnectionString(config.Private().Database)
db, err = sqlx.Connect("postgres", pgConfig) db, err = sqlx.Connect("postgres", pgConfig)
db.MustExec("DELETE FROM transactions") db.MustExec("DELETE FROM transactions")
db.MustExec("DELETE FROM blocks") db.MustExec("DELETE FROM blocks")
}) })
AfterEach(func() {
db.Close()
})
It("implements the observer interface", func() { It("implements the observer interface", func() {
var observer core.BlockchainObserver = core.BlockchainDBObserver{Db: db} var observer core.BlockchainObserver = core.BlockchainDBObserver{Db: db}
Expect(observer).NotTo(BeNil()) Expect(observer).NotTo(BeNil())

View File

@ -2,7 +2,7 @@
if [ $# -eq 1 ] if [ $# -eq 1 ]
then then
migrate -database postgresql://localhost:5432/postgres create -dir ./migrations -ext sql $1 migrate create -dir ./migrations -ext sql $1
else else
echo "**An Error Occurred**" echo "**An Error Occurred**"
echo "Usage: ./scripts/create_migration <migration-name>" echo "Usage: ./scripts/create_migration <migration-name>"

View File

@ -1,2 +0,0 @@
#!/bin/bash
pg_dump -O -s vulcanize > migrations/schema.sql

View File

@ -1,3 +0,0 @@
#!/bin/bash
migrate -database 'postgresql://localhost:5432/vulcanize?sslmode=disable' -path ./migrations up
./scripts/dump_schema

View File

@ -1,3 +0,0 @@
#!/bin/bash
migrate -database 'postgresql://localhost:5432/vulcanize?sslmode=disable' -path ./migrations down 1
./scripts/dump_schema