diff --git a/Makefile b/Makefile index 681f34b3..6a95a558 100644 --- a/Makefile +++ b/Makefile @@ -11,10 +11,10 @@ GINKGO = $(BIN)/ginkgo $(BIN)/ginkgo: go get -u github.com/onsi/ginkgo/ginkgo -MIGRATE = $(BIN)/migrate -$(BIN)/migrate: - go get -u -d github.com/mattes/migrate/cli github.com/lib/pq - go build -tags 'postgres' -o $(BIN)/migrate github.com/mattes/migrate/cli +GOOSE = $(BIN)/goose +$(BIN)/goose: + go get -u -d github.com/pressly/goose/cmd/goose + go build -tags='no_mysql no_sqlite' -o $(BIN)/goose github.com/pressly/goose LINT = $(BIN)/golint $(BIN)/golint: @@ -74,16 +74,43 @@ checkdbvars: @echo $(CONNECT_STRING) +# Goose defaults down migrations to 1 step .PHONY: rollback -rollback: $(MIGRATE) checkdbvars - $(MIGRATE) -database $(CONNECT_STRING) -path ./db/migrations down 1 +rollback: $(GOOSE) checkdbvars + cd db/migrations;\ + $(GOOSE) postgres "$(CONNECT_STRING)" down 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 -migrate: $(MIGRATE) checkdbvars - $(MIGRATE) -database $(CONNECT_STRING) -path ./db/migrations up +migrate: $(GOOSE) checkdbvars + cd db/migrations;\ + $(GOOSE) postgres "$(CONNECT_STRING)" up 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 import: test -n "$(NAME)" # $$NAME diff --git a/README.md b/README.md index 21810a7a..b940a87d 100644 --- a/README.md +++ b/README.md @@ -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. `cd $GOPATH/src/github.com/vulcanize/vulcanizedb` 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 -## Create a migration file (up and down) -1. ./script/create_migrate create_bite_table +## Create a migration file +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 - To use a local Ethereum node, copy `environments/public.toml.example` to