这是indexloc提供的服务,不要输入任何密码
Skip to content

Conversation

@shahidhk
Copy link
Member

@shahidhk shahidhk commented Apr 9, 2019

Description

This PR adds a new admin-only API for getting schema dumps from the connected Postgres instance.

POST /v1alpha1/pg_dump
Content-Type: application/json

{
  "opts": ["-O", "-x", "--schema", "public", "--schema-only"],
  "clean_output": true
}

response:

HTTP/1.1 200 OK
Content-Type: application/sql

CREATE TABLE public.author (
    id integer NOT NULL,
    name text NOT NULL
);
CREATE SEQUENCE public.author_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;
ALTER SEQUENCE public.author_id_seq OWNED BY public.author.id;
ALTER TABLE ONLY public.author ALTER COLUMN id SET DEFAULT nextval('public.author_id_seq'::regclass);

If there is an error, the response will be JSON with the right error code and error messages.

Since this is an API executing a shell script on the server, an option is provided to enable/disable the API. By default, the API is enabled. To disable, use the --enabled-apis by excluding pgdump out of the default value graphql,metadata,pgdump.

Affected components

  • Server
  • Build System
  • Tests

Related Issues

close #1939

Solution and Design

A new admin-only POST endpoint which takes schema as a JSON key. The handler executes a shell script (https://github.com/shahidhk/graphql-engine/blob/pg-dump-on-server/server/src-rsr/run_pg_dump.sh) which executes pg_dump and writes the output to file. This file is then read and sent to the client in the response body.

The clean: true argument will clean the output to remove:

  1. SQL front matter, like SET statements.
  2. CREATE SCHEMA public.
  3. COMMENT ON SCHMEA public is 'standard public schema';
  4. Comments (--) and empty newlines.
  5. Postgres triggers created by Hasura for event triggers.

Steps to test and verify

curl -d'{"opts": ["-O", "-x", "--schema", "public", "--schema-only"],"clean_output": true}' http://localhost:8080/v1alpha1/pg_dump

Limitations, known bugs & workarounds

The pg_dump binary is not yet included in the docker image, the docker image will not work.

Tests are pending.

NA

@shahidhk shahidhk added c/server Related to server c/build-system Related to the build-release system labels Apr 9, 2019
@netlify
Copy link

netlify bot commented Apr 9, 2019

Deploy preview for hasura-docs ready!

Built with commit ea02b56

https://deploy-preview-1967--hasura-docs.netlify.com

@hasura-bot
Copy link
Contributor

Review app for commit cd7ef59 deployed to Heroku: https://hge-ci-pull-1967.herokuapp.com
Docker image for server: hasura/graphql-engine:pull1967-cd7ef59

amanvishnani
amanvishnani previously approved these changes Apr 11, 2019
@dsandip dsandip added this to the release-candidates milestone Apr 15, 2019
@mnlbox
Copy link
Contributor

mnlbox commented Apr 15, 2019

@shahidhk Can you add Console support for this too please?

@hasura-bot
Copy link
Contributor

Review app for commit c70d9f9 deployed to Heroku: https://hge-ci-pull-1967.herokuapp.com
Docker image for server: hasura/graphql-engine:pull1967-c70d9f9

@hasura-bot
Copy link
Contributor

Review app for commit dff389c deployed to Heroku: https://hge-ci-pull-1967.herokuapp.com
Docker image for server: hasura/graphql-engine:pull1967-dff389c

rikinsk-zz
rikinsk-zz previously approved these changes Apr 24, 2019
Copy link

@rikinsk-zz rikinsk-zz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docs approved

rikinsk-zz
rikinsk-zz previously approved these changes Apr 24, 2019
Copy link

@rikinsk-zz rikinsk-zz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docs approved

@hasura-bot
Copy link
Contributor

Review app for commit fe547c9 deployed to Heroku: https://hge-ci-pull-1967.herokuapp.com
Docker image for server: hasura/graphql-engine:pull1967-fe547c9

-> Q.ConnInfo
-> m BL.ByteString
execPGDump b ci = do
output <- liftIO $ runScript dbUrl opts clean
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

runScript is prone to IO exception when the filename is not found in disk or maybe other reasons. Use try to catch IO exception explicitly.

@mnlbox
Copy link
Contributor

mnlbox commented Apr 30, 2019

@hasura-bot
Copy link
Contributor

Review app for commit 8b44a52 deployed to Heroku: https://hge-ci-pull-1967.herokuapp.com
Docker image for server: hasura/graphql-engine:pull1967-8b44a52b

Copy link
Member

@rakeshkky rakeshkky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

server LGTM

@hasura-bot
Copy link
Contributor

Review app for commit ea02b56 deployed to Heroku: https://hge-ci-pull-1967.herokuapp.com
Docker image for server: hasura/graphql-engine:pull1967-ea02b566

@0x777 0x777 merged commit 71cf017 into hasura:master Apr 30, 2019
@hasura-bot
Copy link
Contributor

Review app https://hge-ci-pull-1967.herokuapp.com is deleted

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c/build-system Related to the build-release system c/server Related to server

Projects

None yet

Development

Successfully merging this pull request may close these issues.

add an api which can execute pg_dump on the database

10 participants