graphql-engine/scripts/edit-pg-dump/edit-pg-dump.sh

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

56 lines
1.2 KiB
Bash
Raw Normal View History

add script and now.sh function to edit pg_dump output (close #1861) (#1884) <!-- Thank you for submitting this PR! :) --> <!-- Provide a general summary of your changes in the Title above ^, end with (close #<issue-no>) or (fix #<issue-no>) --> ### Description <!-- The title might not be enough to convey how this change affects the user. --> <!-- Describe the changes from a user's perspective --> This PR adds a bash script and a serverless function to clean up the output of `pg_dump` so that it can be used as a migration file for Hasura. This can be later integrated with the CLI so that the cleanup is handled by CLI. ### Affected components <!-- Remove non-affected components from the list --> - Scripts ### Related Issues <!-- Please make sure you have an issue associated with this Pull Request --> <!-- And then add `(close #<issue-no>)` to the pull request title --> <!-- Add the issue number below (e.g. #234) --> #1861 ### Solution and Design <!-- How is this issue solved/fixed? What is the design? --> <!-- It's better if we elaborate --> - A serverless function written in Go gets the SQL content though HTTP POST. - A set of pre-defined lines are removed from this SQL string. - SQL comments are removed using regex matching. - Postgres triggers created by Hasura for use with event triggers are removed with regex matching. - Empty newlines are removed by regex matching. - Resulting string is returned in the HTTP response. ### Steps to test and verify <!-- If this is a feature, what are the steps to try them out? --> <!-- If this is a bug-fix, how do we verify the fix? --> ```bash curl --data-binary @filename.sql https://hasura-edit-pg-dump.now.sh > newfile.sql ``` ### Limitations, known bugs & workarounds <!-- Limitations of the PR, known bugs and suggested workarounds --> NA <!-- Feel free to delete these comment lines -->
2019-03-27 19:06:58 +03:00
#! /usr/bin/env bash
#
# Usage: ./edit-pg-dump.sh <file-name.sql>
#
if [ "$#" -ne 1 ]; then
echo "Invalid usage: ./edit-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;
2019-04-09 06:54:59 +03:00
CREATE SCHEMA public;
add script and now.sh function to edit pg_dump output (close #1861) (#1884) <!-- Thank you for submitting this PR! :) --> <!-- Provide a general summary of your changes in the Title above ^, end with (close #<issue-no>) or (fix #<issue-no>) --> ### Description <!-- The title might not be enough to convey how this change affects the user. --> <!-- Describe the changes from a user's perspective --> This PR adds a bash script and a serverless function to clean up the output of `pg_dump` so that it can be used as a migration file for Hasura. This can be later integrated with the CLI so that the cleanup is handled by CLI. ### Affected components <!-- Remove non-affected components from the list --> - Scripts ### Related Issues <!-- Please make sure you have an issue associated with this Pull Request --> <!-- And then add `(close #<issue-no>)` to the pull request title --> <!-- Add the issue number below (e.g. #234) --> #1861 ### Solution and Design <!-- How is this issue solved/fixed? What is the design? --> <!-- It's better if we elaborate --> - A serverless function written in Go gets the SQL content though HTTP POST. - A set of pre-defined lines are removed from this SQL string. - SQL comments are removed using regex matching. - Postgres triggers created by Hasura for use with event triggers are removed with regex matching. - Empty newlines are removed by regex matching. - Resulting string is returned in the HTTP response. ### Steps to test and verify <!-- If this is a feature, what are the steps to try them out? --> <!-- If this is a bug-fix, how do we verify the fix? --> ```bash curl --data-binary @filename.sql https://hasura-edit-pg-dump.now.sh > newfile.sql ``` ### Limitations, known bugs & workarounds <!-- Limitations of the PR, known bugs and suggested workarounds --> NA <!-- Feel free to delete these comment lines -->
2019-03-27 19:06:58 +03:00
EOF
while read -r line; do
sed -i '/^'"$line"'$/d' $filename
done <<< $lines
# delete notify triggers
sed -i -E '/^CREATE TRIGGER "?notify_hasura_.+"? AFTER \w+ ON .+ FOR EACH ROW EXECUTE PROCEDURE "?hdb_catalog"?\."?notify_hasura_.+"?\(\);$/d' $filename
add script and now.sh function to edit pg_dump output (close #1861) (#1884) <!-- Thank you for submitting this PR! :) --> <!-- Provide a general summary of your changes in the Title above ^, end with (close #<issue-no>) or (fix #<issue-no>) --> ### Description <!-- The title might not be enough to convey how this change affects the user. --> <!-- Describe the changes from a user's perspective --> This PR adds a bash script and a serverless function to clean up the output of `pg_dump` so that it can be used as a migration file for Hasura. This can be later integrated with the CLI so that the cleanup is handled by CLI. ### Affected components <!-- Remove non-affected components from the list --> - Scripts ### Related Issues <!-- Please make sure you have an issue associated with this Pull Request --> <!-- And then add `(close #<issue-no>)` to the pull request title --> <!-- Add the issue number below (e.g. #234) --> #1861 ### Solution and Design <!-- How is this issue solved/fixed? What is the design? --> <!-- It's better if we elaborate --> - A serverless function written in Go gets the SQL content though HTTP POST. - A set of pre-defined lines are removed from this SQL string. - SQL comments are removed using regex matching. - Postgres triggers created by Hasura for use with event triggers are removed with regex matching. - Empty newlines are removed by regex matching. - Resulting string is returned in the HTTP response. ### Steps to test and verify <!-- If this is a feature, what are the steps to try them out? --> <!-- If this is a bug-fix, how do we verify the fix? --> ```bash curl --data-binary @filename.sql https://hasura-edit-pg-dump.now.sh > newfile.sql ``` ### Limitations, known bugs & workarounds <!-- Limitations of the PR, known bugs and suggested workarounds --> NA <!-- Feel free to delete these comment lines -->
2019-03-27 19:06:58 +03:00
# delete empty lines
sed -i '/^[[:space:]]*$/d' $filename
echo "done"