diff --git a/pkg/sql2pgroll/alter_table.go b/pkg/sql2pgroll/alter_table.go index c5b12b5b..6d155138 100644 --- a/pkg/sql2pgroll/alter_table.go +++ b/pkg/sql2pgroll/alter_table.go @@ -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(), @@ -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 } diff --git a/pkg/sql2pgroll/alter_table_test.go b/pkg/sql2pgroll/alter_table_test.go index 666661db..a0280b9c 100644 --- a/pkg/sql2pgroll/alter_table_test.go +++ b/pkg/sql2pgroll/alter_table_test.go @@ -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 {