Skip to content

Commit 582a575

Browse files
authored
Add support for create-if-not-exists for 'create schema' (#362)
1 parent e433a4d commit 582a575

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

internal/catalog/build.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,10 +305,12 @@ func Update(c *pg.Catalog, stmt nodes.Node) error {
305305
case nodes.CreateSchemaStmt:
306306
name := *n.Schemaname
307307
if _, exists := c.Schemas[name]; exists {
308-
return wrap(pg.ErrorSchemaAlreadyExists(name), raw.StmtLocation)
308+
if !n.IfNotExists {
309+
return wrap(pg.ErrorSchemaAlreadyExists(name), raw.StmtLocation)
310+
}
311+
} else {
312+
c.Schemas[name] = pg.NewSchema()
309313
}
310-
c.Schemas[name] = pg.NewSchema()
311-
312314
case nodes.DropStmt:
313315
for _, obj := range n.Objects.Items {
314316
if n.RemoveType == nodes.OBJECT_TABLE || n.RemoveType == nodes.OBJECT_TYPE {

internal/endtoend/testdata/builtins/query.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ SELECT trunc(42.8);
2222
SELECT trunc(42.4382, 2);
2323
SELECT width_bucket(5.35, 0.024, 10.06, 5);
2424
SELECT width_bucket(now(), array['yesterday', 'today', 'tomorrow']::timestamptz[]);
25+
create schema if not exists sqlc;

0 commit comments

Comments
 (0)