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

Make ALTER TABLE foo ALTER COLUMN a SET DATA TYPE text options unrepresentable #508

Merged
merged 2 commits into from
Dec 4, 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
17 changes: 17 additions & 0 deletions pkg/sql2pgroll/alter_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ func convertAlterTableAlterColumnType(stmt *pgq.AlterTableStmt, cmd *pgq.AlterTa
return nil, fmt.Errorf("expected column definition, got %T", cmd.GetDef().Node)
}

if !canConvertColumnForSetDataType(node.ColumnDef) {
return nil, nil
}

return &migrations.OpAlterColumn{
Table: stmt.GetRelation().GetRelname(),
Column: cmd.GetName(),
Expand Down Expand Up @@ -170,6 +174,19 @@ func canConvertUniqueConstraint(constraint *pgq.Constraint) bool {
return true
}

// canConvertColumnForSetDataType checks if `column` can be faithfully
// converted as part of an OpAlterColumn operation to set a new type for the
// column.
func canConvertColumnForSetDataType(column *pgq.ColumnDef) bool {
if column.GetCollClause() != nil {
return false
}
if column.GetRawDefault() != nil {
return false
}
return true
}

func ptr[T any](x T) *T {
return &x
}
5 changes: 5 additions & 0 deletions pkg/sql2pgroll/alter_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ func TestUnconvertableAlterTableAddConstraintStatements(t *testing.T) {
"ALTER TABLE foo ADD CONSTRAINT bar UNIQUE (a) INCLUDE (b)",
"ALTER TABLE foo ADD CONSTRAINT bar UNIQUE (a) WITH (fillfactor=70)",
"ALTER TABLE foo ADD CONSTRAINT bar UNIQUE (a) USING INDEX TABLESPACE baz",

// COLLATE and USING clauses are not representable by `OpAlterColumn`
// 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'",
}

for _, sql := range tests {
Expand Down
Loading