-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert SQL statements of the form: ```sql ALTER TABLE foo RENAME COLUMN a TO b ALTER TABLE foo RENAME a TO b ``` to the equivalent `OpAlterColumn` operation: ```json [ { "alter_column": { "table": "foo", "column": "a", "name": "b" } } ] ``` Part of #504
- Loading branch information
1 parent
0d3e17d
commit d94d3b4
Showing
4 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
}) | ||
} | ||
} |