diff --git a/.travis.yml b/.travis.yml index ac077457..25d19f0e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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: diff --git a/Gododir/main.go b/Gododir/main.go index 21f35193..8254201f 100644 --- a/Gododir/main.go +++ b/Gododir/main.go @@ -3,29 +3,70 @@ 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("run", nil, func(context *do.Context) { - ipcPath := context.Args.MayString("", "ipc-path", "i") + p.Task("runPublic", nil, func(context *do.Context) { + startBlockchainListener(cfg.Public(), parseIpcPath(context)) + }) - port := "5432" - host := "localhost" - databaseName := "vulcanize" + p.Task("runPrivate", nil, func(context *do.Context) { + startBlockchainListener(cfg.Private(), parseIpcPath(context)) + }) - var blockchain core.Blockchain = core.NewGethBlockchain(ipcPath) - blockchain.RegisterObserver(core.BlockchainLoggingObserver{}) - pgConfig := "host=" + host + " port=" + port + " dbname=" + databaseName + " 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() + 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") }) } diff --git a/README.md b/README.md index 07549e74..ef98e797 100644 --- a/README.md +++ b/README.md @@ -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 ` @@ -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 diff --git a/config/config.go b/config/config.go new file mode 100644 index 00000000..154c69df --- /dev/null +++ b/config/config.go @@ -0,0 +1,5 @@ +package config + +type Config struct { + Database Database +} diff --git a/config/database.go b/config/database.go new file mode 100644 index 00000000..7e82fb97 --- /dev/null +++ b/config/database.go @@ -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) +} diff --git a/config/private.go b/config/private.go new file mode 100644 index 00000000..53027c5b --- /dev/null +++ b/config/private.go @@ -0,0 +1,11 @@ +package config + +func Private() Config { + return Config{ + Database: Database{ + Name: "vulcanize_private", + Hostname: "localhost", + Port: 5432, + }, + } +} diff --git a/config/public.go b/config/public.go new file mode 100644 index 00000000..420fd9a1 --- /dev/null +++ b/config/public.go @@ -0,0 +1,11 @@ +package config + +func Public() Config { + return Config{ + Database: Database{ + Name: "vulcanize_public", + Hostname: "localhost", + Port: 5432, + }, + } +} diff --git a/core/blockchain_db_observer_test.go b/core/blockchain_db_observer_test.go index 983554f8..d781606e 100644 --- a/core/blockchain_db_observer_test.go +++ b/core/blockchain_db_observer_test.go @@ -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()) diff --git a/scripts/create_migration b/scripts/create_migration index 5a8d66a9..b51a0cae 100755 --- a/scripts/create_migration +++ b/scripts/create_migration @@ -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 " diff --git a/scripts/dump_schema b/scripts/dump_schema deleted file mode 100755 index aad61aaa..00000000 --- a/scripts/dump_schema +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/bash -pg_dump -O -s vulcanize > migrations/schema.sql diff --git a/scripts/migrate b/scripts/migrate deleted file mode 100755 index 34826ab0..00000000 --- a/scripts/migrate +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash -migrate -database 'postgresql://localhost:5432/vulcanize?sslmode=disable' -path ./migrations up -./scripts/dump_schema diff --git a/scripts/rollback b/scripts/rollback deleted file mode 100755 index 85b6656d..00000000 --- a/scripts/rollback +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash -migrate -database 'postgresql://localhost:5432/vulcanize?sslmode=disable' -path ./migrations down 1 -./scripts/dump_schema