Skip to content

Commit 51f1808

Browse files
authored
fix(sql/catalog): Support pg_dump output (#2508)
CREATE SCHEMA <default_schema> will be treated as CREATE SCHEMA IF NOT EXISTS <default_schema> to support pg_dump output.
1 parent 5c72923 commit 51f1808

File tree

7 files changed

+253
-0
lines changed

7 files changed

+253
-0
lines changed

internal/endtoend/testdata/pg_dump/db/db.go

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/pg_dump/db/models.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/pg_dump/db/query.sql.go

Lines changed: 82 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
-- name: GetAuthor :one
2+
SELECT * FROM authors
3+
WHERE id = $1 LIMIT 1;
4+
5+
-- name: ListAuthors :many
6+
SELECT * FROM authors
7+
ORDER BY name;
8+
9+
-- name: CreateAuthor :one
10+
INSERT INTO authors (
11+
name, bio
12+
) VALUES (
13+
$1, $2
14+
)
15+
RETURNING *;
16+
17+
-- name: DeleteAuthor :exec
18+
DELETE FROM authors
19+
WHERE id = $1;
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
--
2+
-- PostgreSQL database dump
3+
--
4+
5+
-- Dumped from database version 15.3 (Debian 15.3-1.pgdg120+1)
6+
-- Dumped by pg_dump version 15.3
7+
8+
SET statement_timeout = 0;
9+
SET lock_timeout = 0;
10+
SET idle_in_transaction_session_timeout = 0;
11+
SET client_encoding = 'UTF8';
12+
SET standard_conforming_strings = on;
13+
SELECT pg_catalog.set_config('search_path', '', false);
14+
SET check_function_bodies = false;
15+
SET xmloption = content;
16+
SET client_min_messages = warning;
17+
SET row_security = off;
18+
19+
--
20+
-- Name: public; Type: SCHEMA; Schema: -; Owner: pg_database_owner
21+
--
22+
23+
CREATE SCHEMA public;
24+
25+
26+
ALTER SCHEMA public OWNER TO pg_database_owner;
27+
28+
--
29+
-- Name: SCHEMA public; Type: COMMENT; Schema: -; Owner: pg_database_owner
30+
--
31+
32+
COMMENT ON SCHEMA public IS 'standard public schema';
33+
34+
35+
SET default_table_access_method = heap;
36+
37+
--
38+
-- Name: authors; Type: TABLE; Schema: public; Owner: postgres
39+
--
40+
41+
CREATE TABLE public.authors (
42+
id bigint NOT NULL,
43+
name text NOT NULL,
44+
bio text
45+
);
46+
47+
48+
ALTER TABLE public.authors OWNER TO postgres;
49+
50+
--
51+
-- Name: authors_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
52+
--
53+
54+
CREATE SEQUENCE public.authors_id_seq
55+
START WITH 1
56+
INCREMENT BY 1
57+
NO MINVALUE
58+
NO MAXVALUE
59+
CACHE 1;
60+
61+
62+
ALTER TABLE public.authors_id_seq OWNER TO postgres;
63+
64+
--
65+
-- Name: authors_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: postgres
66+
--
67+
68+
ALTER SEQUENCE public.authors_id_seq OWNED BY public.authors.id;
69+
70+
71+
--
72+
-- Name: authors id; Type: DEFAULT; Schema: public; Owner: postgres
73+
--
74+
75+
ALTER TABLE ONLY public.authors ALTER COLUMN id SET DEFAULT nextval('public.authors_id_seq'::regclass);
76+
77+
78+
--
79+
-- Name: authors authors_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres
80+
--
81+
82+
ALTER TABLE ONLY public.authors
83+
ADD CONSTRAINT authors_pkey PRIMARY KEY (id);
84+
85+
86+
--
87+
-- PostgreSQL database dump complete
88+
--
89+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"path": "db",
6+
"engine": "postgresql",
7+
"schema": "schema.sql",
8+
"queries": "query.sql"
9+
}
10+
]
11+
}
12+

internal/sql/catalog/schema.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ func (c *Catalog) createSchema(stmt *ast.CreateSchemaStmt) error {
100100
return fmt.Errorf("create schema: empty name")
101101
}
102102
if _, err := c.getSchema(*stmt.Name); err == nil {
103+
// If the default schema already exists, treat additional CREATE SCHEMA
104+
// statements as no-ops.
105+
if *stmt.Name == c.DefaultSchema {
106+
return nil
107+
}
103108
if !stmt.IfNotExists {
104109
return sqlerr.SchemaExists(*stmt.Name)
105110
}

0 commit comments

Comments
 (0)