Add Makefile for goose migrations, update README accordingly

This commit is contained in:
Edvard 2019-01-24 11:54:25 +01:00
parent 4c87ef44b8
commit e5f3826633
2 changed files with 43 additions and 10 deletions

View File

@ -11,10 +11,10 @@ GINKGO = $(BIN)/ginkgo
$(BIN)/ginkgo: $(BIN)/ginkgo:
go get -u github.com/onsi/ginkgo/ginkgo go get -u github.com/onsi/ginkgo/ginkgo
MIGRATE = $(BIN)/migrate GOOSE = $(BIN)/goose
$(BIN)/migrate: $(BIN)/goose:
go get -u -d github.com/mattes/migrate/cli github.com/lib/pq go get -u -d github.com/pressly/goose/cmd/goose
go build -tags 'postgres' -o $(BIN)/migrate github.com/mattes/migrate/cli go build -tags='no_mysql no_sqlite' -o $(BIN)/goose github.com/pressly/goose
LINT = $(BIN)/golint LINT = $(BIN)/golint
$(BIN)/golint: $(BIN)/golint:
@ -74,16 +74,43 @@ checkdbvars:
@echo $(CONNECT_STRING) @echo $(CONNECT_STRING)
# Goose defaults down migrations to 1 step
.PHONY: rollback .PHONY: rollback
rollback: $(MIGRATE) checkdbvars rollback: $(GOOSE) checkdbvars
$(MIGRATE) -database $(CONNECT_STRING) -path ./db/migrations down 1 cd db/migrations;\
$(GOOSE) postgres "$(CONNECT_STRING)" down
pg_dump -O -s $(CONNECT_STRING) > db/schema.sql pg_dump -O -s $(CONNECT_STRING) > db/schema.sql
.PHONY: checkmigration
checkmigration:
test -n "$(MIGRATION)" # $$MIGRATION
# MIGRATION is either a version integer or a timestamp
.PHONY: rollback_to
rollback_to: $(GOOSE) checkmigration checkdbvars
cd db/migrations;\
$(GOOSE) postgres "$(CONNECT_STRING)" down-to "$(MIGRATION)"
.PHONY: migrate .PHONY: migrate
migrate: $(MIGRATE) checkdbvars migrate: $(GOOSE) checkdbvars
$(MIGRATE) -database $(CONNECT_STRING) -path ./db/migrations up cd db/migrations;\
$(GOOSE) postgres "$(CONNECT_STRING)" up
pg_dump -O -s $(CONNECT_STRING) > db/schema.sql pg_dump -O -s $(CONNECT_STRING) > db/schema.sql
.PHONY: checkmigname
checkmigname:
test -n "$(NAME)" # $$NAME
.PHONY: new_migration
new_migration: $(GOOSE) checkmigname
cd db/migrations;\
$(GOOSE) create $(NAME) sql
.PHONY: migration_status
migration_status: $(GOOSE) checkdbvars
cd db/migrations;\
$(GOOSE) postgres "$(CONNECT_STRING)" status
.PHONY: import .PHONY: import
import: import:
test -n "$(NAME)" # $$NAME test -n "$(NAME)" # $$NAME

View File

@ -25,11 +25,17 @@ Vulcanize DB is a set of tools that make it easier for developers to write appli
1. `createdb vulcanize_public` 1. `createdb vulcanize_public`
1. `cd $GOPATH/src/github.com/vulcanize/vulcanizedb` 1. `cd $GOPATH/src/github.com/vulcanize/vulcanizedb`
1. Run the migrations: `make migrate HOST_NAME=localhost NAME=vulcanize_public PORT=5432` 1. Run the migrations: `make migrate HOST_NAME=localhost NAME=vulcanize_public PORT=5432`
- To rollback a single step: `make rollback NAME=vulcanize_public`
- To rollback to a certain migration: `make rollback_to MIGRATION=n NAME=vulcanize_public`
- To see status of migrations: `make migration_status NAME=vulcanize_public`
* See below for configuring additional environments * See below for configuring additional environments
## Create a migration file (up and down) ## Create a migration file
1. ./script/create_migrate create_bite_table 1. `make new_migration NAME=add_columnA_to_table1`
- This will create a new timestamped migration file in `db/migrations`
1. Write the migration code in the created file, under the respective `goose` pragma
- Goose automatically runs each migration in a transaction; don't add `BEGIN` and `COMMIT` statements.
## Configuration ## Configuration
- To use a local Ethereum node, copy `environments/public.toml.example` to - To use a local Ethereum node, copy `environments/public.toml.example` to