Skip to content

Commit

Permalink
Convert ALTER TABLE foo RENAME TO bar SQL to pgroll operation (#513)
Browse files Browse the repository at this point in the history
Convert SQL statements of the form:

```sql
ALTER TABLE foo RENAME TO bar
```

to the corresponding `OpRenameTable` operation:

```json
[
  {
    "rename_table": {
      "from": "foo",
      "to": "bar"
    }
  }
]
```

Part of #504
  • Loading branch information
andrew-farries authored Dec 5, 2024
1 parent 3b8c56e commit 3512f76
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
10 changes: 10 additions & 0 deletions pkg/sql2pgroll/expect/rename_table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// SPDX-License-Identifier: Apache-2.0

package expect

import "github.com/xataio/pgroll/pkg/migrations"

var RenameTableOp1 = &migrations.OpRenameTable{
From: "foo",
To: "bar",
}
33 changes: 29 additions & 4 deletions pkg/sql2pgroll/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,25 @@ import (
"github.com/xataio/pgroll/pkg/migrations"
)

// convertRenameStmt converts RenameStmt nodes to pgroll operations.
func convertRenameStmt(stmt *pgq.RenameStmt) (migrations.Operations, error) {
if stmt.GetRelationType() != pgq.ObjectType_OBJECT_TABLE {
return nil, nil
}
if stmt.GetRenameType() != pgq.ObjectType_OBJECT_COLUMN {
switch stmt.GetRenameType() {
case pgq.ObjectType_OBJECT_TABLE:
return convertRenameTable(stmt)
case pgq.ObjectType_OBJECT_COLUMN:
return convertRenameColumn(stmt)
default:
return nil, nil
}
}

// convertRenameColumn converts SQL statements like:
//
// `ALTER TABLE foo RENAME COLUMN a TO b`
// `ALTER TABLE foo RENAME a TO b`
//
// to an OpAlterColumn operation.
func convertRenameColumn(stmt *pgq.RenameStmt) (migrations.Operations, error) {
return migrations.Operations{
&migrations.OpAlterColumn{
Table: stmt.GetRelation().GetRelname(),
Expand All @@ -23,3 +34,17 @@ func convertRenameStmt(stmt *pgq.RenameStmt) (migrations.Operations, error) {
},
}, nil
}

// convertRenameTable converts SQL statements like:
//
// `ALTER TABLE foo RENAME TO bar`
//
// to an OpRenameTable operation.
func convertRenameTable(stmt *pgq.RenameStmt) (migrations.Operations, error) {
return migrations.Operations{
&migrations.OpRenameTable{
From: stmt.GetRelation().GetRelname(),
To: stmt.GetNewname(),
},
}, nil
}
6 changes: 5 additions & 1 deletion pkg/sql2pgroll/rename_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/xataio/pgroll/pkg/sql2pgroll/expect"
)

func TestConvertRenameColumnStatements(t *testing.T) {
func TestConvertRenameStatements(t *testing.T) {
t.Parallel()

tests := []struct {
Expand All @@ -27,6 +27,10 @@ func TestConvertRenameColumnStatements(t *testing.T) {
sql: "ALTER TABLE foo RENAME a TO b",
expectedOp: expect.AlterColumnOp4,
},
{
sql: "ALTER TABLE foo RENAME TO bar",
expectedOp: expect.RenameTableOp1,
},
}

for _, tc := range tests {
Expand Down

0 comments on commit 3512f76

Please sign in to comment.