Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update regex to delete event triggers in edit-pg-dump (close #1959) #1960

Merged
merged 3 commits into from
Apr 8, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/graphql/manual/migrations/existing-database.rst
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ Step 3: Initialize the migrations as per your current state
# POST the SQL to a serverless function and save the response
curl --data-binary @public-schema.sql https://hasura-edit-pg-dump.now.sh > public-schema-edited.sql

*(The source code for this function can be found on `GitHub
<https://github.com/hasura/graphql-engine/tree/master/scripts/edit-pg-dump>`__
along with a bash script if you'd prefer that.)*

- Create a migration called ``init`` using this SQL file and the metadata that
is on the server right now:

Expand Down
7 changes: 4 additions & 3 deletions scripts/edit-pg-dump/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# hasura-edit-pg-dump

This is a serverless function written in Go and deployed onto now.sh which can
takes the output SQL from `pg_dump` and clean it up so that it can be used as a
take the SQL output from `pg_dump` and clean it up so that it can be used as a
migration file with Hasura.

1. SQL front matter, like `SET` statements are removed.
2. Comments and empty newlines are removed.
3. Postgres triggers created by Hasura are removed.
2. `CREATE SCHEMA public` is removed.
3. Comments (`--`) and empty newlines are removed.
4. Postgres triggers created by Hasura for event triggers are removed.

This app is available at https://hasura-edit-pg-dump.now.sh

Expand Down
3 changes: 2 additions & 1 deletion scripts/edit-pg-dump/edit-pg-dump.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ SET client_min_messages = warning;
SET row_security = off;
SET default_tablespace = '';
SET default_with_oids = false;
CREATE SCHEMA public
EOF

while read -r line; do
Expand All @@ -45,7 +46,7 @@ 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
sed -i -E '/^CREATE TRIGGER "?notify_hasura_.+"? AFTER \w+ ON .+ FOR EACH ROW EXECUTE PROCEDURE "?hdb_views"?\."?notify_hasura_.+"?\(\);$/d' $filename

# delete empty lines

Expand Down
7 changes: 5 additions & 2 deletions scripts/edit-pg-dump/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ var frontMatter = []string{
"SET row_security = off;",
"SET default_tablespace = '';",
"SET default_with_oids = false;",
"CREATE SCHEMA public",
}

const helpStr = `POST the SQL file contents to this URL:

curl --data-binary @filename.sql https://hasura-edit-pg-dump.now.sh > newfile.sql`
curl --data-binary @filename.sql https://hasura-edit-pg-dump.now.sh > newfile.sql

Source code can be found at https://github.com/hasura/graphql-engine/tree/master/scripts/edit-pg-dump`

// Handler is the now.sh serverless handler
func Handler(w http.ResponseWriter, r *http.Request) {
Expand All @@ -50,7 +53,7 @@ func Handler(w http.ResponseWriter, r *http.Request) {
}

// build the regular expression for matching triggers created by Hasura
triggerRe, err := regexp.Compile(`(?m)^CREATE TRIGGER "?notify_hasura_\w+"? AFTER \w+ ON "?\w+"?\."?\w+"? FOR EACH ROW EXECUTE PROCEDURE "?hdb_views"?\."?notify_hasura_\w+"?\(\);$`)
triggerRe, err := regexp.Compile(`(?m)^CREATE TRIGGER "?notify_hasura_.+"? AFTER \w+ ON .+ FOR EACH ROW EXECUTE PROCEDURE "?hdb_views"?\."?notify_hasura_.+"?\(\);$`)
if err != nil {
http.Error(w, err.Error(), 500)
return
Expand Down