Skip to content

Commit d507dbe

Browse files
Handle unconvertible CREATE TABLE column defintions (#548)
Column definitions in `CREATE TABLE` statements offer [several options](https://www.postgresql.org/docs/current/sql-createtable.html), most of which aren't currently representable by `pgroll` `Column` definitions. Add tests and code to ensure that `CREATE TABLE` statements containing columns that use any of these unrepresentable options fall back to raw SQL operations. Part of #504
1 parent b727101 commit d507dbe

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

pkg/sql2pgroll/create_table.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ func convertCreateStmt(stmt *pgq.CreateStmt) (migrations.Operations, error) {
2929
if err != nil {
3030
return nil, fmt.Errorf("error converting column definition: %w", err)
3131
}
32+
if column == nil {
33+
return nil, nil
34+
}
3235
columns = append(columns, *column)
3336
default:
3437
return nil, nil
@@ -80,6 +83,10 @@ func canConvertCreateStatement(stmt *pgq.CreateStmt) bool {
8083
}
8184

8285
func convertColumnDef(col *pgq.ColumnDef) (*migrations.Column, error) {
86+
if !canConvertColumnDef(col) {
87+
return nil, nil
88+
}
89+
8390
// Convert the column type
8491
typeString, err := pgq.DeparseTypeName(col.TypeName)
8592
if err != nil {
@@ -111,3 +118,21 @@ func convertColumnDef(col *pgq.ColumnDef) (*migrations.Column, error) {
111118
Pk: pk,
112119
}, nil
113120
}
121+
122+
// canConvertColumnDef returns true iff `col` can be converted to a pgroll
123+
// `Column` definition.
124+
func canConvertColumnDef(col *pgq.ColumnDef) bool {
125+
switch {
126+
// Column storage options are not supported
127+
case col.GetStorageName() != "":
128+
return false
129+
// Column compression options are not supported
130+
case col.GetCompression() != "":
131+
return false
132+
// Column collation options are not supported
133+
case col.GetCollClause() != nil:
134+
return false
135+
default:
136+
return true
137+
}
138+
}

pkg/sql2pgroll/create_table_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,15 @@ func TestUnconvertableCreateTableStatements(t *testing.T) {
112112
// The LIKE clause is not supported
113113
"CREATE TABLE foo(a int, LIKE bar)",
114114
"CREATE TABLE foo(LIKE bar)",
115+
116+
// Column `STORAGE` options are not supported
117+
"CREATE TABLE foo(a int STORAGE PLAIN)",
118+
119+
// Column compression options are not supported
120+
"CREATE TABLE foo(a text COMPRESSION pglz)",
121+
122+
// Column collation is not supported
123+
"CREATE TABLE foo(a text COLLATE en_US)",
115124
}
116125

117126
for _, sql := range tests {

0 commit comments

Comments
 (0)