Skip to content

Commit

Permalink
sql/ast: Implement ALTER TABLE RENAME (#398)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleconroy authored Mar 11, 2020
1 parent 9fede32 commit 00bc70f
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 26 deletions.
44 changes: 18 additions & 26 deletions internal/postgresql/catalog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,26 +231,20 @@ func TestUpdate(t *testing.T) {
`,
nil,
},
/*
{
`
CREATE TABLE venues ();
ALTER TABLE venues RENAME TO arenas;
`,
pg.Catalog{
Schemas: map[string]pg.Schema{
"public": {
Types: map[string]pg.Type{},
Tables: map[string]pg.Table{
"arenas": pg.Table{
Name: "arenas",
},
},
},
{
`
CREATE TABLE venues ();
ALTER TABLE venues RENAME TO arenas;
`,
&catalog.Schema{
Name: "public",
Tables: []*catalog.Table{
{
Rel: &ast.TableName{Name: "arenas"},
},
},
},
*/
},
{
`
CREATE TYPE status AS ENUM ('open', 'closed');
Expand All @@ -273,15 +267,13 @@ func TestUpdate(t *testing.T) {
`,
nil,
},
/*
{
`
CREATE TYPE status AS ENUM ('open', 'closed');
DROP TYPE public.status;
`,
catalog.New("public"),
},
*/
{
`
CREATE TYPE status AS ENUM ('open', 'closed');
DROP TYPE public.status;
`,
nil,
},
{
`
CREATE SCHEMA foo;
Expand Down
16 changes: 16 additions & 0 deletions internal/postgresql/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,22 @@ func translate(node nodes.Node) (ast.Node, error) {
}
return nil, errSkip

case nodes.RenameStmt:
switch n.RenameType {

case nodes.OBJECT_TABLE:
tbl, err := parseTableName(*n.Relation)
if err != nil {
return nil, err
}
return &ast.RenameTableStmt{
Table: tbl,
NewName: n.Newname,
}, nil

}
return nil, errSkip

default:
return nil, errSkip
}
Expand Down
9 changes: 9 additions & 0 deletions internal/sql/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,12 @@ type CommentOnColumnStmt struct {
func (n *CommentOnColumnStmt) Pos() int {
return 0
}

type RenameTableStmt struct {
Table *TableName
NewName *string
}

func (n *RenameTableStmt) Pos() int {
return 0
}
2 changes: 2 additions & 0 deletions internal/sql/catalog/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ func (c *Catalog) Build(stmts []ast.Statement) error {
err = c.dropTable(n)
case *ast.DropTypeStmt:
err = c.dropType(n)
case *ast.RenameTableStmt:
err = c.renameTable(n)
}
if err != nil {
return err
Expand Down
11 changes: 11 additions & 0 deletions internal/sql/catalog/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,14 @@ func (c *Catalog) dropTable(stmt *ast.DropTableStmt) error {
}
return nil
}

func (c *Catalog) renameTable(stmt *ast.RenameTableStmt) error {
_, tbl, err := c.getTable(stmt.Table)
if err != nil {
return err
}
if stmt.NewName != nil {
tbl.Rel.Name = *stmt.NewName
}
return nil
}

0 comments on commit 00bc70f

Please sign in to comment.