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

Commit

Permalink
Merge pull request #44 from 8thlight/go-task
Browse files Browse the repository at this point in the history
Add multiple environments
  • Loading branch information
ericmeyer authored Nov 1, 2017
2 parents a08e16e + 8c23c61 commit 323187c
Showing 14 changed files with 135 additions and 54 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
Gododir/godobin-*
test_data_dir/
vendor/
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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 changes: 76 additions & 0 deletions Gododir/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -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 changes: 5 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package config

type Config struct {
Database Database
}
13 changes: 13 additions & 0 deletions config/database.go
Original file line number Diff line number Diff line change
@@ -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 changes: 11 additions & 0 deletions config/private.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package config

func Private() Config {
return Config{
Database: Database{
Name: "vulcanize_private",
Hostname: "localhost",
Port: 5432,
},
}
}
11 changes: 11 additions & 0 deletions config/public.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package config

func Public() Config {
return Config{
Database: Database{
Name: "vulcanize_public",
Hostname: "localhost",
Port: 5432,
},
}
}
19 changes: 6 additions & 13 deletions core/blockchain_db_observer_test.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,30 @@
package core_test

import (
"fmt"

"github.com/8thlight/vulcanizedb/config"
"github.com/8thlight/vulcanizedb/core"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
. "github.com/onsi/ginkgo"
. "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 changes: 0 additions & 25 deletions main.go

This file was deleted.

2 changes: 1 addition & 1 deletion scripts/create_migration
Original file line number Diff line number Diff line change
@@ -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>"
2 changes: 0 additions & 2 deletions scripts/dump_schema

This file was deleted.

3 changes: 0 additions & 3 deletions scripts/migrate

This file was deleted.

3 changes: 0 additions & 3 deletions scripts/rollback

This file was deleted.

0 comments on commit 323187c

Please sign in to comment.