Skip to content
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

Convert RENAME COLUMN to pgroll operation #511

Merged
merged 1 commit into from
Dec 5, 2024
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
2 changes: 2 additions & 0 deletions pkg/sql2pgroll/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ func convert(sql string) (migrations.Operations, error) {
return convertCreateStmt(node.CreateStmt)
case *pgq.Node_AlterTableStmt:
return convertAlterTableStmt(node.AlterTableStmt)
case *pgq.Node_RenameStmt:
return convertRenameStmt(node.RenameStmt)
default:
return makeRawSQLOperation(sql), nil
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/sql2pgroll/expect/alter_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ var AlterTableOp5 = &migrations.OpCreateConstraint{
},
}

var AlterTableOp6 = &migrations.OpAlterColumn{
Table: "foo",
Column: "a",
Name: ptr("b"),
}

func ptr[T any](v T) *T {
return &v
}
25 changes: 25 additions & 0 deletions pkg/sql2pgroll/rename.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SPDX-License-Identifier: Apache-2.0

package sql2pgroll

import (
pgq "github.com/pganalyze/pg_query_go/v6"
"github.com/xataio/pgroll/pkg/migrations"
)

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 {
return nil, nil
}

return migrations.Operations{
&migrations.OpAlterColumn{
Table: stmt.GetRelation().GetRelname(),
Column: stmt.GetSubname(),
Name: ptr(stmt.GetNewname()),
},
}, nil
}
42 changes: 42 additions & 0 deletions pkg/sql2pgroll/rename_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// SPDX-License-Identifier: Apache-2.0

package sql2pgroll_test

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/xataio/pgroll/pkg/migrations"
"github.com/xataio/pgroll/pkg/sql2pgroll"
"github.com/xataio/pgroll/pkg/sql2pgroll/expect"
)

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

tests := []struct {
sql string
expectedOp migrations.Operation
}{
{
sql: "ALTER TABLE foo RENAME COLUMN a TO b",
expectedOp: expect.AlterTableOp6,
},
{
sql: "ALTER TABLE foo RENAME a TO b",
expectedOp: expect.AlterTableOp6,
},
}

for _, tc := range tests {
t.Run(tc.sql, func(t *testing.T) {
ops, err := sql2pgroll.Convert(tc.sql)
require.NoError(t, err)

require.Len(t, ops, 1)

assert.Equal(t, tc.expectedOp, ops[0])
})
}
}
Loading