Skip to content

Commit

Permalink
update regex to delete event triggers in edit-pg-dump (close hasura#1959
Browse files Browse the repository at this point in the history
  • Loading branch information
shahidhk authored Apr 8, 2019
1 parent 8c00579 commit 8836463
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
2 changes: 2 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,8 @@ 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

0 comments on commit 8836463

Please sign in to comment.