Skip to content

Commit 5b7980d

Browse files
Make ALTER TABLE foo ALTER COLUMN a SET DATA TYPE text options unrepresentable (#508)
Ensure that SQL statements of the form: ```sql 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' ``` are converted to raw SQL operations instead of an `OpAlterColumn` operation. The change of collation is not currently representable by an `OpAlterColumn` operation and neither is the `USING` clause. Part of #504
1 parent 06bfebd commit 5b7980d

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

pkg/sql2pgroll/alter_table.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ func convertAlterTableAlterColumnType(stmt *pgq.AlterTableStmt, cmd *pgq.AlterTa
7878
return nil, fmt.Errorf("expected column definition, got %T", cmd.GetDef().Node)
7979
}
8080

81+
if !canConvertColumnForSetDataType(node.ColumnDef) {
82+
return nil, nil
83+
}
84+
8185
return &migrations.OpAlterColumn{
8286
Table: stmt.GetRelation().GetRelname(),
8387
Column: cmd.GetName(),
@@ -170,6 +174,19 @@ func canConvertUniqueConstraint(constraint *pgq.Constraint) bool {
170174
return true
171175
}
172176

177+
// canConvertColumnForSetDataType checks if `column` can be faithfully
178+
// converted as part of an OpAlterColumn operation to set a new type for the
179+
// column.
180+
func canConvertColumnForSetDataType(column *pgq.ColumnDef) bool {
181+
if column.GetCollClause() != nil {
182+
return false
183+
}
184+
if column.GetRawDefault() != nil {
185+
return false
186+
}
187+
return true
188+
}
189+
173190
func ptr[T any](x T) *T {
174191
return &x
175192
}

pkg/sql2pgroll/alter_table_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ func TestUnconvertableAlterTableAddConstraintStatements(t *testing.T) {
6767
"ALTER TABLE foo ADD CONSTRAINT bar UNIQUE (a) INCLUDE (b)",
6868
"ALTER TABLE foo ADD CONSTRAINT bar UNIQUE (a) WITH (fillfactor=70)",
6969
"ALTER TABLE foo ADD CONSTRAINT bar UNIQUE (a) USING INDEX TABLESPACE baz",
70+
71+
// COLLATE and USING clauses are not representable by `OpAlterColumn`
72+
// operations when changing data type.
73+
`ALTER TABLE foo ALTER COLUMN a SET DATA TYPE text COLLATE "en_US"`,
74+
"ALTER TABLE foo ALTER COLUMN a SET DATA TYPE text USING 'foo'",
7075
}
7176

7277
for _, sql := range tests {

0 commit comments

Comments
 (0)