Created
October 9, 2016 13:54
-
-
Save deinspanjer/c54979498d63112f92430b77ec4d9353 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
| usage() { | |
| echo "Usage: $0 <type> [<schema>.]<name> [r=<requirement>[,<requirement>]...] [<param_name>=<param_value>[,<param_value>]...] [<param1_name>:<param2_name>=<param1_value>:<param2_value>[,<param1_value>:<param2_value>]...]" | |
| echo " arguments must be SQL safe strings (A-z 0-9 _ .)" | |
| echo " schema defaults to public if not specified" | |
| echo " specify 'schema' as <type> and the name of the schema as <name> to add a schema" | |
| } | |
| if [ "$BASH_VERSINFO" -lt 4 ]; then | |
| echo "Bash 4 is required for this script." | |
| usage | |
| exit 1 | |
| fi | |
| if [ $# -lt 2 ]; then | |
| echo "Not enough arguments" | |
| usage | |
| exit 1 | |
| fi | |
| declare -r VALID_PARAM='^[0-9a-z_]+((=[0-9a-z_.]+(,[0-9a-z_.]+)*)|(:[0-9a-z_]+=[0-9a-z_]+:[][)(0-9a-z_.]+(,[0-9a-z_.]+:[][)(0-9a-z_.]+)*))$' | |
| declare -r SIMPLE_PARAM='^[0-9a-z_]+=' | |
| declare TYPE | |
| TYPE=$1; shift | |
| declare SCHEMA | |
| declare SCHEMA_PARAM | |
| declare NAME | |
| SCHEMA=${1%%.*} | |
| NAME=${1##*.} | |
| shift | |
| if [ "$SCHEMA" == "public" ] || [ "$SCHEMA" == "$NAME" ]; then | |
| SCHEMA="public" | |
| SCHEMA_PARAM="" | |
| else | |
| SCHEMA_PARAM="-s schema=$SCHEMA"; | |
| fi | |
| declare -a VARARGS | |
| until [ -z "$1" ]; do | |
| if egrep -iq "$VALID_PARAM" <<< "$1"; then | |
| if egrep -iq "$SIMPLE_PARAM" <<< "$1"; then | |
| n=${1%%=*} | |
| vs=${1##*=} | |
| va=(${vs//,/ }) | |
| for v in "${va[@]}"; do | |
| if [ "$n" == "r" ]; then | |
| VARARGS+=("-r $v") | |
| else | |
| VARARGS+=("-s $n=$v") | |
| fi | |
| done | |
| else | |
| n=${1%%=*} | |
| n1=${n%%:*} | |
| n2=${n##*:} | |
| vs=${1##*=} | |
| va=(${vs//,/ }) | |
| for v in "${va[@]}"; do | |
| v1=${v%%:*} | |
| v2=${v##*:} | |
| VARARGS+=("-s $n1=$v1 -s $n2=$v2") | |
| done | |
| fi | |
| else | |
| echo "Invalid pattern option: '$1'" | |
| usage | |
| exit 1 | |
| fi | |
| shift | |
| done | |
| if [ "$TYPE" == "schema" ]; then | |
| set -x | |
| sqitch add ${NAME}__${TYPE} -m "Add ${NAME} ${TYPE}" -t create_${TYPE} -s name=${NAME} ${VARARGS[@]} | |
| elif [ "$TYPE" == "other" ]; then | |
| set -x | |
| sqitch add ${SCHEMA}_${NAME} -m "Add ${SCHEMA}.${NAME}" -t ${TYPE} ${SCHEMA_PARAM} -s name=${NAME} ${VARARGS[@]} | |
| else | |
| set -x | |
| sqitch add ${SCHEMA}_${TYPE}_${NAME} -m "Add ${SCHEMA}.${NAME} ${TYPE}" -t create_${TYPE} ${SCHEMA_PARAM} -s name=${NAME} ${VARARGS[@]} | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment