Skip to content

Instantly share code, notes, and snippets.

@shahidhk
Last active March 26, 2019 10:47
Show Gist options
  • Save shahidhk/98da4438e3a8a50264c9ef21d89100df to your computer and use it in GitHub Desktop.
Save shahidhk/98da4438e3a8a50264c9ef21d89100df to your computer and use it in GitHub Desktop.
Script to clean up pg_dump to keep just the SQL statements - for use with Hasura migrations
#! /usr/bin/env bash
#
# Usage: ./process_pg_dump.sh <file-name.sql>
#
if [ "$#" -ne 1 ]; then
echo "Invalid usage: ./process_pg_dump.sh <file-name.sql>"
fi
filename=$1
if [ ! -f $filename ] || [ "$filename" == "" ]; then
echo "file $filename does not exist"
exit 1
fi
echo "making a copy"
cp $filename $filename.backup
echo "processing file"
# delete all comments
sed -i '/^--/d' $filename
# delete front matter
read -r -d '' lines << EOF
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET client_min_messages = warning;
SET row_security = off;
SET default_tablespace = '';
SET default_with_oids = false;
EOF
while read -r line; do
sed -i '/^'"$line"'$/d' $filename
done <<< $lines
# delete notify triggers
sed -i -E '/^CREATE TRIGGER "?notify_hasura_\w+"? AFTER \w+ ON "?\w+"?\."?\w+"? FOR EACH ROW EXECUTE PROCEDURE "?hdb_views"?\."?notify_hasura_\w+"?\(\);$/d' $filename
# delete empty lines
sed -i '/^[[:space:]]*$/d' $filename
echo "done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment