-
-
Save manboubird/716053e949dab4901c1635587902e78e 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
#!/bin/bash | |
SOURCEINSTANCE=${SOURCEINSTANCE:-''} | |
SOURCEUSER=${SOURCEUSER:-'admin'} | |
SOURCEPORT=${SOURCEPORT:-'5439'} | |
SOURCEDB=${SOURCEDB:-'db'} | |
SOURCESCHEMA=${SOURCESCHEMA:-'public'} | |
SCHEMA=${SCHEMA:-'public'} | |
echo "CREATE TABLE ${SCHEMA}.${TABLE} (" | |
psql -h ${SOURCEINSTANCE} -U ${SOURCEUSER} -p ${SOURCEPORT} ${SOURCEDB} -t -c "select (\"column\" || ' ' || type || ' ENCODE ' || encoding || ',' ) from pg_table_def where schemaname='$SCHEMA' and tablename = '$TABLE'" | sed 's/ENCODE none/ENCODE RAW/' | sed '$d' | sed '$ s/,$//' | |
echo ")" | |
SORTKEY=$(psql -h ${SOURCEINSTANCE} -U ${SOURCEUSER} -p ${SOURCEPORT} ${SOURCEDB} -t -c "select \"column\" from pg_table_def where schemaname='$SCHEMA' and tablename = '$TABLE' and sortkey > 0 order by sortkey" | tr "\n" "," | sed 's/\([,]*\)$//') | |
[ -n "$SORTKEY" ] && echo "sortkey ($SORTKEY)" | |
DESTKEY=$(psql -h ${SOURCEINSTANCE} -U ${SOURCEUSER} -p ${SOURCEPORT} ${SOURCEDB} -t -c "select \"column\" from pg_table_def where schemaname='$SCHEMA' and tablename = '$TABLE' and distkey = true" | tr "\n" "," | sed 's/\([,]*\)$//') | |
[ -n "$DESTKEY" ] && echo "distkey ($DESTKEY)" | |
echo ";" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment