Skip to content

Instantly share code, notes, and snippets.

@zzpwestlife
Last active May 15, 2024 06:30
Show Gist options
  • Save zzpwestlife/82800109f94897c77e36e4ff13ecc7d2 to your computer and use it in GitHub Desktop.
Save zzpwestlife/82800109f94897c77e36e4ff13ecc7d2 to your computer and use it in GitHub Desktop.
golang-migrate Makefile
include .env
export $(shell sed 's/=.*//' .env)
V?=
N?=
MYSQL_DSN?="$$MYSQL_USER:$$MYSQL_PASSWORD@tcp($$MYSQL_HOST:$$MYSQL_PORT)/$$MYSQL_DATABASE?multiStatements=true"
local-db:
@ docker-compose up -d
@ until mysql --host=$$MYSQL_HOST --port=$$MYSQL_PORT --user=$$MYSQL_USER -p$$MYSQL_PASSWORD --protocol=tcp -e 'SELECT 1' >/dev/null 2>&1 && exit 0; do \
>&2 echo "MySQL is unavailable - sleeping"; \
sleep 5 ; \
done
@ echo "MySQL is up and running!"
migrate-setup: local-db create_db
@if [ -z "$$(which migrate)" ]; then echo "Installing migrate command..."; go install -tags 'mysql' github.com/golang-migrate/migrate/v4/cmd/migrate; fi
create_db: ## Create empty database
@echo "create database if not exists..."
@docker exec -i $$MYSQL_CONTAINER_NAME mysql -u$$MYSQL_USER -p$$MYSQL_PASSWORD <<< "CREATE DATABASE IF NOT EXISTS $$MYSQL_DATABASE"
migrate-up: migrate-setup
@ migrate -database "mysql://"$(MYSQL_DSN) -path db/migrations up $$N
migrate-down: migrate-setup
@ migrate -database "mysql://"$(MYSQL_DSN) -path db/migrations down $$N
migrate-to-version: migrate-setup
@ migrate -database "mysql://"$(MYSQL_DSN) -path db/migrations goto $$V
drop-db: migrate-setup
@ migrate -database "mysql://"$(MYSQL_DSN) -path db/migrations drop
force-version: migrate-setup
@ migrate -database "mysql://"$(MYSQL_DSN) -path db/migrations force $$V
migrate-version: migrate-setup
@ migrate -database "mysql://"$(MYSQL_DSN) -path db/migrations version
unittest: dep migrate-up local-db create_db
@echo "gosum test..."
@time CGO_CFLAGS=-Wno-undef-prefix gotestsum --packages="`go list ./... | grep -v stub_test`" -- -count=1 -failfast -gcflags="all=-l -N"
static-check:
@echo "run static check..."
@staticcheck -checks="all","-ST1000","-ST1003","-ST1016","-ST1020","-ST1021","-ST1022","-SA1019" -tests=false -f=text `go list ./... | grep -E -v "cmd|stub_test|pb|client"`
nil-check:
@echo "run nil check..."
@nilaway ./internal/app/...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment