Skip to content

Commit

Permalink
Merge pull request #1 from olenm/om/initial_script
Browse files Browse the repository at this point in the history
initial script
  • Loading branch information
olenm authored Apr 19, 2017
2 parents 2c3b843 + 959cbf0 commit e0642ea
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
\#*\#
*~
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,25 @@
# mysql-surface-schema
bash script pulling mysql tables and column names, output in yml

#### from the --help display
usage: mysql_surface-schema-to-yml.sh [-h | --help] [ U=[mysql-user] ] [ p=[mysql-password] ] [ D=[mysql-database] ] [ H=[mysql-host] ] [ P=[mysql-port] ]

Creates a yml friendly database 'schema' (ordered alphabetically) where only details are table and column names.
> Parameter order does not matter, but is case sensitive
Env Vars (params take precedence):
MYSQL_USER
MYSQL_PASSWORD
MYSQL_DATABASE
MYSQL_HOST
MYSQL_PORT

Requirements:
awk
bash 4+
mysql

example:
read -s PW
MYSQL_PASSWORD=$PW ./mysql_surface-schema-to-yml.sh U=user D=db H=localhost P=3306
unset PW
96 changes: 96 additions & 0 deletions mysql_surface-schema-to-yml.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/usr/bin/env bash

function help() {
cat <<EOF
usage: ${0##*/} [ U=[mysql-user] ] [ p=[mysql-password] ] [ D=[mysql-database] ] [ H=[mysql-host] ] [ P=[mysql-port] ]
Creates a yml friendly database 'schema' (ordered alphabetically) where only details are table and column names.
> Parameter order does not matter, but is case sensitive
Env Vars (params take precedence):
MYSQL_USER
MYSQL_PASSWORD
MYSQL_DATABASE
MYSQL_HOST
MYSQL_PORT
Requirements:
awk
bash 4+
mysql
example:
read -s PW
MYSQL_PASSWORD=\$PW ./mysql_surface-schema-to-yml.sh U=user D=db H=localhost P=3306
unset PW
EOF
}

function err_chk() {
if [[ 0 -ne $1 ]]; then
echo -e "ERROR:\n $2" >&2
echo "" >&2
help >&2
exit 1
fi

}

# regex arguments for combinations of --help (help, -h, ect)
for x in ${@}; do
if [[ "${x,,}" =~ ^(-+h(elp)?|-*help)$ ]]; then
help
exit 0
fi
done

args=(${@})
for ((i=0; i< ${#args[@]}; i++)); do
if [[ "${args[$i]:0:2}" == "U=" ]]; then
UN="${args[$i]:2}"
fi
if [[ "${args[$i]:0:2}" == "p=" ]]; then
PW="${args[$i]:2}"
fi
if [[ "${args[$i]:0:2}" == "D=" ]]; then
DB="${args[$i]:2}"
fi
if [[ "${args[$i]:0:2}" == "H=" ]]; then
HN="${args[$i]:2}"
fi
if [[ "${args[$i]:0:2}" == "P=" ]]; then
HP="${args[$i]:2}"
fi
done

[[ -z "$UN" ]] && UN=${MYSQL_USER}
[[ -z "$PW" ]] && PW=${MYSQL_PASSWORD}
[[ -z "$DB" ]] && DB=${MYSQL_DATABASE-mysql}
[[ -z "$HN" ]] && HN=${MYSQL_HOST-localhost}
[[ -z "$HP" ]] && HP=${MYSQL_HOST-3306}




function sqlexec() {
RES=$(mysql -u${UN} -p${PW} ${DB} -h ${HN} -P${HP} -Nse "${@}" 2>/dev/null)
if [[ 0 -ne $? ]]; then
echo "mysql -u${UN} -p<censored> ${DB} -h ${HN} -P${HP} -Nse "
echo " '${@}'"
exit 1
fi
echo "$RES" | sort -u
}


TABLES=$(sqlexec "show tables;" )
err_chk $? "${TABLES}"

echo "---"
for T in $TABLES; do
echo "${T}:"
RES=$(sqlexec "show columns from ${T};")
err_chk $? "${RES}"
echo "${RES}" | awk '{print " - "$1}'
done

0 comments on commit e0642ea

Please sign in to comment.