graphql-engine/scripts/edit-pg-dump/index.go

88 lines
2.3 KiB
Go
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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"regexp"
"strings"
)
var frontMatter = []string{
"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
}
const helpStr = `POST the SQL file contents to this URL:
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`
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
// Handler is the now.sh serverless handler
func Handler(w http.ResponseWriter, r *http.Request) {
// if method is not POST, respond with help message
if r.Method != "POST" {
fmt.Fprintf(w, helpStr)
return
}
// build the regular expression for matching empty newlines
emptyNewlineRe, err := regexp.Compile(`(?m)^\s*$[\r\n]*`)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
// build the regular expression for matching SQL comments
commentsRe, err := regexp.Compile(`(?m)^--.*$`)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
// build the regular expression for matching triggers created by Hasura
triggerRe, err := regexp.Compile(`(?m)^CREATE TRIGGER "?notify_hasura_.+"? AFTER \w+ ON .+ FOR EACH ROW EXECUTE PROCEDURE "?hdb_views"?\."?notify_hasura_.+"?\(\);$`)
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
if err != nil {
http.Error(w, err.Error(), 500)
return
}
// read the POST body
body, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
// convert bytes to string
bodyStr := string(body)
// remove the SQL front matter
for _, l := range frontMatter {
bodyStr = strings.Replace(bodyStr, l, "", 1)
}
// replace the regex matches
bodyStr = commentsRe.ReplaceAllLiteralString(bodyStr, "")
bodyStr = triggerRe.ReplaceAllLiteralString(bodyStr, "")
bodyStr = emptyNewlineRe.ReplaceAllLiteralString(bodyStr, "\n")
// prefix a message
bodyStr = "--\r\n-- SQL edited by https://hasura-edit-pg-dump.now.sh\r\n--\r\n" + bodyStr
// respond with the edited string
fmt.Fprintf(w, bodyStr)
}