Skip to content

Commit

Permalink
Fall back to raw SQL for unrepresentable options
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanslade committed Dec 10, 2024
1 parent 8033a8b commit 6346c9c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
13 changes: 13 additions & 0 deletions pkg/sql2pgroll/alter_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,19 @@ func convertAlterTableAddUniqueConstraint(stmt *pgq.AlterTableStmt, constraint *
//
// to an OpDropColumn operation.
func convertAlterTableDropColumn(stmt *pgq.AlterTableStmt, cmd *pgq.AlterTableCmd) (migrations.Operation, error) {
switch cmd.Behavior {
case pgq.DropBehavior_DROP_RESTRICT, pgq.DropBehavior_DROP_BEHAVIOR_UNDEFINED:
// Supported
case pgq.DropBehavior_DROP_CASCADE:
// Fall back to SQL
return nil, nil
}

// IF EXISTS not supported
if cmd.MissingOk {
return nil, nil
}

return &migrations.OpDropColumn{
Table: stmt.GetRelation().GetRelname(),
Column: cmd.GetName(),
Expand Down
8 changes: 8 additions & 0 deletions pkg/sql2pgroll/alter_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ func TestConvertAlterTableStatements(t *testing.T) {
sql: "ALTER TABLE foo DROP COLUMN bar",
expectedOp: expect.DropColumnOp1,
},
{
sql: "ALTER TABLE foo DROP COLUMN bar RESTRICT ",
expectedOp: expect.DropColumnOp1,
},
}

for _, tc := range tests {
Expand Down Expand Up @@ -77,6 +81,10 @@ func TestUnconvertableAlterTableAddConstraintStatements(t *testing.T) {
// operations when changing data type.
`ALTER TABLE foo ALTER COLUMN a SET DATA TYPE text COLLATE "en_US"`,
"ALTER TABLE foo ALTER COLUMN a SET DATA TYPE text USING 'foo'",

// CASCADE and IF EXISTS clauses are not represented by OpDropColumn
"ALTER TABLE foo DROP COLUMN bar CASCADE",
"ALTER TABLE foo DROP COLUMN IF EXISTS bar",
}

for _, sql := range tests {
Expand Down

0 comments on commit 6346c9c

Please sign in to comment.