Last active
October 16, 2020 18:34
-
-
Save takakd/5e37bee44f36630c03d1e8e2887db8ce to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env bash | |
# golang-migrate helper script | |
# https://github.com/golang-migrate/migrate | |
usage() { | |
echo "Usage: migrate.sh COMMAND | |
COMMAND: | |
Input golang-migrate command and options. | |
'create' is limited to SQL. | |
e.g. | |
migrate.sh create NAME | |
migrate.sh up | |
migrate.sh down 2 | |
" | |
} | |
if [[ "$1" = "help" ]]; then | |
usage | |
exit 0 | |
fi | |
SCRIPT_DIR=$(cd $(dirname "$0"); pwd) | |
ENV_FILE=${SCRIPT_DIR}/../configs/.env | |
if [[ -e "$ENV_FILE" ]]; then | |
source "$ENV_FILE" | |
fi | |
# Limit create to SQL | |
if [[ "$1" = "create" ]]; then | |
# Exclude options | |
param=$(echo "$@" | sed -e 's/create //') | |
# Remove schema from env value | |
dir=$(echo "$MIGRATION_DIR" | sed -e 's/file:\/\///') | |
migrate -source "$MIGRATION_DIR" -database "$DATABASE_URL" create -dir $dir -ext sql $param | |
else | |
migrate -source "$MIGRATION_DIR" -database "$DATABASE_URL" $@ | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment