Merge pull request #44 from 8thlight/go-task

Add multiple environments
This commit is contained in:
ericmeyer 2017-11-01 12:21:49 -05:00 committed by GitHub
commit 60a8be67f4
14 changed files with 135 additions and 54 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
.idea
Gododir/godobin-*
test_data_dir/
vendor/

View File

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

76
Gododir/main.go Normal file
View File

@ -0,0 +1,76 @@
package main
import (
"log"
"fmt"
cfg "github.com/8thlight/vulcanizedb/config"
"github.com/8thlight/vulcanizedb/core"
"github.com/jmoiron/sqlx"
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) {
p.Task("runPublic", nil, func(context *do.Context) {
startBlockchainListener(cfg.Public(), parseIpcPath(context))
})
p.Task("runPrivate", nil, func(context *do.Context) {
startBlockchainListener(cfg.Private(), parseIpcPath(context))
})
p.Task("migratePublic", nil, func(context *do.Context) {
connectString := cfg.DbConnectionString(cfg.Public().Database)
context.Bash(fmt.Sprintf("migrate -database '%s' -path ./migrations up", connectString))
context.Bash(fmt.Sprintf("pg_dump -O -s %s > migrations/schema.sql", cfg.Public().Database.Name))
})
p.Task("migratePrivate", nil, func(context *do.Context) {
connectString := cfg.DbConnectionString(cfg.Private().Database)
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")
})
}
func main() {
do.Godo(tasks)
}

View File

@ -9,6 +9,8 @@
- Go 1.9+
- https://github.com/golang/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
### Cloning the Repository
@ -22,15 +24,17 @@
5. `cd $GOPATH/src/github.com/8thlight/vulcanizedb`
6. `dep ensure`
### Setting up the Development Database
### Setting up the Databases
1. Install Postgres
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`
4. `go build -tags 'postgres' -o /usr/local/bin/migrate github.com/mattes/migrate/cli`
5. `createdb vulcanize`
6. `cd $GOPATH/src/github.com/8thlight/vulcanizedb`
7. `./scripts/migrate`
5. `createdb vulcanize_public`
6. `createdb vulcanize_private`
7. `cd $GOPATH/src/github.com/8thlight/vulcanizedb`
8. `godo migratePublic`
9. `godo migratePrivate`
Adding a new migration: `./scripts/create_migration <migration-name>`
@ -60,7 +64,7 @@ The default location for Ethereum is:
1. Start a blockchain.
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

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

25
main.go
View File

@ -1,25 +0,0 @@
package main
import (
"flag"
"log"
"github.com/8thlight/vulcanizedb/core"
"github.com/jmoiron/sqlx"
)
func main() {
ipcPath := flag.String("ipcPath", "", "location geth.ipc")
flag.Parse()
var blockchain core.Blockchain = core.NewGethBlockchain(*ipcPath)
blockchain.RegisterObserver(core.BlockchainLoggingObserver{})
pgConfig := "host=localhost port=5432 dbname=vulcanize sslmode=disable"
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()
}

View File

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