Created
August 23, 2023 17:23
-
-
Save thiagozs/7519e0fd9583fece3eb6dc5b940e38e4 to your computer and use it in GitHub Desktop.
Makefile with golang-migrate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
BINARY_NAMES="golang-migrate|migrate" | |
DATABASE="mysql://root:password@tcp(localhost:3306)/test" | |
.PHONY: checkdeps help | |
help: ## Displays the help for each command. | |
@grep -E '^[a-zA-Z_-]+:.*## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' | |
dbu: ## Migrates the database to the latest version. | |
migrate -database="$(DATABASE)" -path=migrations -lock-timeout=20 -verbose up | |
dbd: ## Migrates the database down by first version. | |
migrate -database="$(DATABASE)" -path=migrations -lock-timeout=20 -verbose down | |
dbdrop: ## Drops the database. | |
migrate -database="$(DATABASE)" -path=migrations -lock-timeout=20 -verbose drop -f | |
dbseq: ## Creates a new migration file with the given name. ex: name=create_users_table | |
migrate create -ext sql -dir migrations $(name) | |
dbver: ## Prints the current migration version. | |
migrate -database="$(DATABASE)" -path=migrations -lock-timeout=20 version | |
dbf: ## Force the migrates the database to the given version. ex: version=1 | |
migrate -database="$(DATABASE)" -path=migrations -lock-timeout=20 -verbose force $(version) | |
dbt: ## Goto migrates the database to the given version. ex: version=1 | |
migrate -database="$(DATABASE)" -path=migrations -lock-timeout=20 -verbose goto $(version) | |
dbreset: dbdrop dbu ## Reset database to the latest version. | |
checkdeps: ## Checks if the necessary binaries are installed, and installs them if necessary. | |
@echo "Checking dependencies..." | |
@if ! [ -x "$$(command -v brew)" ]; then \ | |
echo "brew is not installed."; \ | |
echo "Installing brew..."; \ | |
/bin/bash -c "$$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"; \ | |
else \ | |
echo "brew is already installed."; \ | |
fi; \ | |
@for BINARY in $(BINARY_NAMES); do \ | |
PACKAGE_NAME=$$(echo $$BINARY | cut -d '|' -f 1); \ | |
BINARY_NAME=$$(echo $$BINARY | cut -d '|' -f 2); \ | |
if ! [ -x "$$(command -v $$BINARY_NAME)" ]; then \ | |
echo "$$BINARY_NAME is not installed."; \ | |
echo "Installing $$PACKAGE_NAME..."; \ | |
brew install $$PACKAGE_NAME; \ | |
else \ | |
echo "$$BINARY_NAME is already installed."; \ | |
fi; \ | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment