Skip to content

Instantly share code, notes, and snippets.

@dimitrismistriotis
Created October 14, 2014 12:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dimitrismistriotis/7872b6034b59356a6ed3 to your computer and use it in GitHub Desktop.
Save dimitrismistriotis/7872b6034b59356a6ed3 to your computer and use it in GitHub Desktop.
#!/bin/zsh
#
# Inspired from
# http://stackoverflow.com/questions/3327312/drop-all-tables-in-postgresql
# and modified for heroku CLI.
#
if [[ $# -ne 1 ]]; then
echo "Supply heroku app as first argument" >&2
return 1
fi
#
# tuples-only command line option is not supported for heroku pg:psql :-(.
# Prefixed with a '#' and then cut up to that.
#
RETRIEVE_TABLES_COMMAND="SELECT '#' || string_agg(table_name, ',') \
FROM information_schema.tables \
WHERE table_schema='public'"
TABLES=`heroku pg:psql --command $RETRIEVE_TABLES_COMMAND --app=$1 \
| sed -n 's/[^#]*#//p'`
# echo "Retrieved: $TABLES"
for t ("${(s/,/)TABLES}"); do
echo "Dropping ${t}"
heroku pg:psql --command "DROP TABLE IF EXISTS ${t} CASCADE" --app=$1
done
@dimitrismistriotis
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment