Skip to content

Commit 9f84662

Browse files
authored
sql/ast: Implement ALTER TABLE SET SCHEMA (#400)
1 parent 3fb980f commit 9f84662

File tree

5 files changed

+64
-21
lines changed

5 files changed

+64
-21
lines changed

internal/postgresql/catalog_test.go

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -193,29 +193,21 @@ func TestUpdate(t *testing.T) {
193193
},
194194
},
195195
},
196-
/*
197-
{
198-
`
199-
CREATE SCHEMA foo;
200-
CREATE SCHEMA bar;
201-
CREATE TABLE foo.baz ();
202-
ALTER TABLE foo.baz SET SCHEMA bar;
203-
`,
204-
pg.Catalog{
205-
Schemas: map[string]pg.Schema{
206-
"public": {},
207-
"foo": {},
208-
"bar": {
209-
Tables: map[string]pg.Table{
210-
"baz": pg.Table{
211-
Name: "baz",
212-
},
213-
},
214-
},
196+
{
197+
`
198+
CREATE SCHEMA foo;
199+
CREATE TABLE bar ();
200+
ALTER TABLE bar SET SCHEMA foo;
201+
`,
202+
&catalog.Schema{
203+
Name: "foo",
204+
Tables: []*catalog.Table{
205+
{
206+
Rel: &ast.TableName{Name: "bar"},
215207
},
216208
},
217209
},
218-
*/
210+
},
219211
{
220212
`
221213
CREATE TABLE venues ();

internal/postgresql/parse.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,21 @@ func (p *Parser) Parse(r io.Reader) ([]ast.Statement, error) {
167167
func translate(node nodes.Node) (ast.Node, error) {
168168
switch n := node.(type) {
169169

170+
case nodes.AlterObjectSchemaStmt:
171+
switch n.ObjectType {
172+
173+
case nodes.OBJECT_TABLE:
174+
tbl, err := parseTableName(*n.Relation)
175+
if err != nil {
176+
return nil, err
177+
}
178+
return &ast.AlterTableSetSchemaStmt{
179+
Table: tbl,
180+
NewSchema: n.Newschema,
181+
}, nil
182+
}
183+
return nil, errSkip
184+
170185
case nodes.AlterTableStmt:
171186
name, err := parseTableName(*n.Relation)
172187
if err != nil {
@@ -271,7 +286,6 @@ func translate(node nodes.Node) (ast.Node, error) {
271286
}, nil
272287

273288
}
274-
275289
return nil, errSkip
276290

277291
case nodes.CreateStmt:

internal/sql/ast/ast.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ func (n *AlterTableCmd) Pos() int {
5757
return 0
5858
}
5959

60+
type AlterTableSetSchemaStmt struct {
61+
Table *TableName
62+
NewSchema *string
63+
}
64+
65+
func (n *AlterTableSetSchemaStmt) Pos() int {
66+
return 0
67+
}
68+
6069
type CreateEnumStmt struct {
6170
TypeName *TypeName
6271
Vals *List

internal/sql/catalog/catalog.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ func (c *Catalog) Build(stmts []ast.Statement) error {
145145
switch n := stmts[i].Raw.Stmt.(type) {
146146
case *ast.AlterTableStmt:
147147
err = c.alterTable(n)
148+
case *ast.AlterTableSetSchemaStmt:
149+
err = c.alterTableSetSchema(n)
148150
case *ast.CommentOnColumnStmt:
149151
err = c.commentOnColumn(n)
150152
case *ast.CommentOnSchemaStmt:

internal/sql/catalog/table.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,32 @@ func (c *Catalog) alterTable(stmt *ast.AlterTableStmt) error {
9393

9494
return nil
9595
}
96+
97+
func (c *Catalog) alterTableSetSchema(stmt *ast.AlterTableSetSchemaStmt) error {
98+
ns := stmt.Table.Schema
99+
if ns == "" {
100+
ns = c.DefaultSchema
101+
}
102+
oldSchema, err := c.getSchema(ns)
103+
if err != nil {
104+
return err
105+
}
106+
tbl, idx, err := oldSchema.getTable(stmt.Table)
107+
if err != nil {
108+
return err
109+
}
110+
newSchema, err := c.getSchema(*stmt.NewSchema)
111+
if err != nil {
112+
return err
113+
}
114+
if _, _, err := newSchema.getTable(stmt.Table); err == nil {
115+
return sqlerr.RelationExists(stmt.Table.Name)
116+
}
117+
oldSchema.Tables = append(oldSchema.Tables[:idx], oldSchema.Tables[idx+1:]...)
118+
newSchema.Tables = append(newSchema.Tables, tbl)
119+
return nil
120+
}
121+
96122
func (c *Catalog) createTable(stmt *ast.CreateTableStmt) error {
97123
ns := stmt.Name.Schema
98124
if ns == "" {

0 commit comments

Comments
 (0)