Skip to content

Commit b727101

Browse files
Handle more unconvertible CREATE TABLE statements (#547)
Build on #546 and ensure that more `CREATE TABLE` statements with options and clauses that are not representable as `pgroll` `OpCreateTable` operations fall back to raw SQL. Part of #504
1 parent a849534 commit b727101

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

pkg/sql2pgroll/create_table.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,22 @@ func convertCreateStmt(stmt *pgq.CreateStmt) (migrations.Operations, error) {
1717
return nil, nil
1818
}
1919

20-
// Convert the column definitions
21-
columns := make([]migrations.Column, 0, len(stmt.TableElts))
20+
// Convert the table elements - table elements can be:
21+
// - Column definitions
22+
// - Constraints
23+
// - LIKE clauses (not supported)
24+
var columns []migrations.Column
2225
for _, elt := range stmt.TableElts {
23-
column, err := convertColumnDef(elt.GetColumnDef())
24-
if err != nil {
25-
return nil, fmt.Errorf("error converting column definition: %w", err)
26+
switch elt.Node.(type) {
27+
case *pgq.Node_ColumnDef:
28+
column, err := convertColumnDef(elt.GetColumnDef())
29+
if err != nil {
30+
return nil, fmt.Errorf("error converting column definition: %w", err)
31+
}
32+
columns = append(columns, *column)
33+
default:
34+
return nil, nil
2635
}
27-
columns = append(columns, *column)
2836
}
2937

3038
return migrations.Operations{
@@ -63,6 +71,9 @@ func canConvertCreateStatement(stmt *pgq.CreateStmt) bool {
6371
// Setting a tablespace is not supported
6472
case stmt.GetTablespacename() != "":
6573
return false
74+
// CREATE TABLE OF type_name is not supported
75+
case stmt.GetOfTypename() != nil:
76+
return false
6677
default:
6778
return true
6879
}

pkg/sql2pgroll/create_table_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ func TestUnconvertableCreateTableStatements(t *testing.T) {
9090
// Any kind of partitioning is not supported
9191
"CREATE TABLE foo(a int) PARTITION BY RANGE (a)",
9292
"CREATE TABLE foo(a int) PARTITION BY LIST (a)",
93+
"CREATE TABLE foo PARTITION OF bar FOR VALUES FROM (1) to (10)",
9394

9495
// Specifying a table access method is not supported
9596
"CREATE TABLE foo(a int) USING bar",
@@ -104,6 +105,13 @@ func TestUnconvertableCreateTableStatements(t *testing.T) {
104105

105106
// Specifying a tablespace is not supported
106107
"CREATE TABLE foo(a int) TABLESPACE bar",
108+
109+
// CREATE TABLE OF type_name is not supported
110+
"CREATE TABLE foo OF type_bar",
111+
112+
// The LIKE clause is not supported
113+
"CREATE TABLE foo(a int, LIKE bar)",
114+
"CREATE TABLE foo(LIKE bar)",
107115
}
108116

109117
for _, sql := range tests {

0 commit comments

Comments
 (0)