Skip to content

internal/dinosql: Support the pg_temp schema #183

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

Merged
merged 1 commit into from
Dec 16, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
29 changes: 26 additions & 3 deletions internal/catalog/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,27 @@ func TestUpdate(t *testing.T) {
},
},
},
{
`
CREATE TABLE pg_temp.migrate (val INT);
INSERT INTO pg_temp.migrate (val) SELECT val FROM old;
INSERT INTO new (val) SELECT val FROM pg_temp.migrate;
`,
pg.Catalog{
Schemas: map[string]pg.Schema{
"pg_temp": {
Tables: map[string]pg.Table{
"migrate": pg.Table{
Name: "migrate",
Columns: []pg.Column{
{Name: "val", DataType: "pg_catalog.int4", NotNull: false, Table: pg.FQN{Schema: "pg_temp", Rel: "migrate"}},
},
},
},
},
},
},
},
} {
test := tc
t.Run(strconv.Itoa(i), func(t *testing.T) {
Expand All @@ -490,10 +511,12 @@ func TestUpdate(t *testing.T) {
t.Fatal(err)
}

delete(c.Schemas, "pg_catalog")
delete(test.c.Schemas, "pg_catalog")
expected := pg.NewCatalog()
for name, schema := range test.c.Schemas {
expected.Schemas[name] = schema
}

if diff := cmp.Diff(test.c, c, cmpopts.EquateEmpty()); diff != "" {
if diff := cmp.Diff(expected, c, cmpopts.EquateEmpty()); diff != "" {
t.Log(test.stmt)
t.Errorf("catalog mismatch:\n%s", diff)
}
Expand Down
5 changes: 5 additions & 0 deletions internal/dinosql/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ func ParseCatalog(schema string) (core.Catalog, error) {
}
}
}

// The pg_temp schema is scoped to the current session. Remove it from the
// catalog so that other queries can not read from it.
delete(c.Schemas, "pg_temp")

if len(merr.Errs) > 0 {
return c, merr
}
Expand Down
9 changes: 9 additions & 0 deletions internal/pg/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ func NewCatalog() Catalog {
Schemas: map[string]Schema{
"public": NewSchema(),
"pg_catalog": pgCatalog(),
// Likewise, the current session's temporary-table schema, pg_temp_nnn, is
// always searched if it exists. It can be explicitly listed in the path by
// using the alias pg_temp. If it is not listed in the path then it is
// searched first (even before pg_catalog). However, the temporary schema is
// only searched for relation (table, view, sequence, etc) and data type
// names. It is never searched for function or operator names.
//
// https://www.postgresql.org/docs/current/runtime-config-client.html
"pg_temp": NewSchema(),
},
}
}
Expand Down