2022-01-07 19:41:40 +00:00
|
|
|
ifndef GOPATH
|
|
|
|
override GOPATH = $(HOME)/go
|
|
|
|
endif
|
|
|
|
|
2021-09-10 18:52:11 +00:00
|
|
|
BIN = $(GOPATH)/bin
|
|
|
|
|
|
|
|
# Tools
|
|
|
|
## Migration tool
|
|
|
|
GOOSE = $(BIN)/goose
|
|
|
|
$(BIN)/goose:
|
2022-01-07 19:07:57 +00:00
|
|
|
GO111MODULE=off go get -u github.com/pressly/goose/cmd/goose
|
2021-09-10 18:52:11 +00:00
|
|
|
|
|
|
|
.PHONY: installtools
|
|
|
|
installtools: | $(GOOSE)
|
|
|
|
echo "Installing tools"
|
|
|
|
|
|
|
|
#Database
|
|
|
|
HOST_NAME = localhost
|
|
|
|
PORT = 5432
|
|
|
|
NAME =
|
|
|
|
USER = postgres
|
|
|
|
PASSWORD = password
|
|
|
|
CONNECT_STRING=postgresql://$(USER):$(PASSWORD)@$(HOST_NAME):$(PORT)/$(NAME)?sslmode=disable
|
|
|
|
|
|
|
|
# Parameter checks
|
|
|
|
## Check that DB variables are provided
|
|
|
|
.PHONY: checkdbvars
|
|
|
|
checkdbvars:
|
|
|
|
test -n "$(HOST_NAME)" # $$HOST_NAME
|
|
|
|
test -n "$(PORT)" # $$PORT
|
|
|
|
test -n "$(NAME)" # $$NAME
|
|
|
|
@echo $(CONNECT_STRING)
|
|
|
|
|
|
|
|
## Check that the migration variable (id/timestamp) is provided
|
|
|
|
.PHONY: checkmigration
|
|
|
|
checkmigration:
|
|
|
|
test -n "$(MIGRATION)" # $$MIGRATION
|
|
|
|
|
|
|
|
# Check that the migration name is provided
|
|
|
|
.PHONY: checkmigname
|
|
|
|
checkmigname:
|
|
|
|
test -n "$(NAME)" # $$NAME
|
|
|
|
|
|
|
|
# Migration operations
|
|
|
|
## Rollback the last migration
|
|
|
|
.PHONY: rollback
|
|
|
|
rollback: $(GOOSE) checkdbvars
|
2021-10-12 11:36:14 +00:00
|
|
|
$(GOOSE) -dir db/migrations postgres "$(CONNECT_STRING)" down
|
2021-09-10 18:52:11 +00:00
|
|
|
pg_dump -O -s $(CONNECT_STRING) > schema.sql
|
|
|
|
|
|
|
|
## Rollback to a select migration (id/timestamp)
|
|
|
|
.PHONY: rollback_to
|
|
|
|
rollback_to: $(GOOSE) checkmigration checkdbvars
|
2021-10-12 11:36:14 +00:00
|
|
|
$(GOOSE) -dir db/migrations postgres "$(CONNECT_STRING)" down-to "$(MIGRATION)"
|
2021-09-10 18:52:11 +00:00
|
|
|
|
2022-01-10 18:13:37 +00:00
|
|
|
## Rollback pre_batch_set
|
|
|
|
.PHONY: `rollback_pre_batch_set`
|
|
|
|
rollback_pre_batch_set: $(GOOSE) checkdbvars
|
|
|
|
$(GOOSE) -dir db/pre_batch_processing_migrations postgres "$(CONNECT_STRING)" down
|
|
|
|
|
|
|
|
## Rollback post_batch_set
|
|
|
|
.PHONY: rollback_post_batch_set
|
|
|
|
rollback_post_batch_set: $(GOOSE) checkdbvars
|
|
|
|
$(GOOSE) -dir db/post_batch_processing_migrations postgres "$(CONNECT_STRING)" down
|
|
|
|
|
2022-01-07 18:53:12 +00:00
|
|
|
## Apply the next up migration
|
|
|
|
.PHONY: migrate_up_by_one
|
|
|
|
migrate_up_by_one: $(GOOSE) checkdbvars
|
|
|
|
$(GOOSE) -dir db/migrations postgres "$(CONNECT_STRING)" up-by-one
|
|
|
|
|
2021-09-10 18:52:11 +00:00
|
|
|
## Apply all migrations not already run
|
|
|
|
.PHONY: migrate
|
|
|
|
migrate: $(GOOSE) checkdbvars
|
2021-10-12 11:36:14 +00:00
|
|
|
$(GOOSE) -dir db/migrations postgres "$(CONNECT_STRING)" up
|
2021-09-10 18:52:11 +00:00
|
|
|
pg_dump -O -s $(CONNECT_STRING) > schema.sql
|
|
|
|
|
2021-11-19 01:03:37 +00:00
|
|
|
## Apply migrations to be ran before a batch processing
|
|
|
|
.PHONY: migrate_pre_batch_set
|
|
|
|
migrate_pre_batch_set: $(GOOSE) checkdbvars
|
|
|
|
$(GOOSE) -dir db/pre_batch_processing_migrations postgres "$(CONNECT_STRING)" up
|
|
|
|
|
2022-01-07 18:53:12 +00:00
|
|
|
## Apply migrations to be ran after a batch processing, one-by-one
|
|
|
|
.PHONY: migrate_post_batch_set_up_by_one
|
|
|
|
migrate_post_batch_set_up_by_one: $(GOOSE) checkdbvars
|
|
|
|
$(GOOSE) -dir db/post_batch_processing_migrations postgres "$(CONNECT_STRING)" up-by-one
|
|
|
|
|
2021-11-19 01:03:37 +00:00
|
|
|
## Apply migrations to be ran after a batch processing
|
|
|
|
.PHONY: migrate_post_batch_set
|
|
|
|
migrate_post_batch_set: $(GOOSE) checkdbvars
|
|
|
|
$(GOOSE) -dir db/post_batch_processing_migrations postgres "$(CONNECT_STRING)" up
|
2021-11-18 23:46:50 +00:00
|
|
|
|
2021-09-10 18:52:11 +00:00
|
|
|
## Create a new migration file
|
|
|
|
.PHONY: new_migration
|
|
|
|
new_migration: $(GOOSE) checkmigname
|
2021-10-12 11:36:14 +00:00
|
|
|
$(GOOSE) -dir db/migrations create $(NAME) sql
|
2021-09-10 18:52:11 +00:00
|
|
|
|
|
|
|
## Check which migrations are applied at the moment
|
|
|
|
.PHONY: migration_status
|
|
|
|
migration_status: $(GOOSE) checkdbvars
|
2021-10-12 11:36:14 +00:00
|
|
|
$(GOOSE) -dir db/migrations postgres "$(CONNECT_STRING)" status
|
2021-09-10 18:52:11 +00:00
|
|
|
|
|
|
|
# Convert timestamped migrations to versioned (to be run in CI);
|
|
|
|
# merge timestamped files to prevent conflict
|
|
|
|
.PHONY: version_migrations
|
|
|
|
version_migrations:
|
2021-10-12 11:36:14 +00:00
|
|
|
$(GOOSE) -dir db/migrations fix
|
2021-09-10 18:52:11 +00:00
|
|
|
|
|
|
|
# Import a psql schema to the database
|
|
|
|
.PHONY: import
|
|
|
|
import:
|
|
|
|
test -n "$(NAME)" # $$NAME
|
|
|
|
psql $(NAME) < schema.sql
|
2021-09-30 12:39:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
## Build docker image with schema
|
|
|
|
.PHONY: docker-build
|
|
|
|
docker-build:
|
|
|
|
docker-compose build
|
|
|
|
|
|
|
|
## Build docker image for migration
|
|
|
|
.PHONY: docker-concise-migration-build
|
|
|
|
docker-concise-migration-build:
|
2021-10-13 07:37:25 +00:00
|
|
|
docker build -t vulcanize/concise-migration-build -f ./db/Dockerfile .
|
|
|
|
|
|
|
|
.PHONY: test-migrations
|
|
|
|
test-migrations: $(GOOSE)
|
2021-11-18 23:46:50 +00:00
|
|
|
./scripts/test_migration.sh
|