Skip to content

Commit

Permalink
Handle more unconvertible CREATE TABLE statements (#547)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
andrew-farries authored Dec 18, 2024
1 parent a849534 commit b727101
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
23 changes: 17 additions & 6 deletions pkg/sql2pgroll/create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,22 @@ func convertCreateStmt(stmt *pgq.CreateStmt) (migrations.Operations, error) {
return nil, nil
}

// Convert the column definitions
columns := make([]migrations.Column, 0, len(stmt.TableElts))
// Convert the table elements - table elements can be:
// - Column definitions
// - Constraints
// - LIKE clauses (not supported)
var columns []migrations.Column
for _, elt := range stmt.TableElts {
column, err := convertColumnDef(elt.GetColumnDef())
if err != nil {
return nil, fmt.Errorf("error converting column definition: %w", err)
switch elt.Node.(type) {
case *pgq.Node_ColumnDef:
column, err := convertColumnDef(elt.GetColumnDef())
if err != nil {
return nil, fmt.Errorf("error converting column definition: %w", err)
}
columns = append(columns, *column)
default:
return nil, nil
}
columns = append(columns, *column)
}

return migrations.Operations{
Expand Down Expand Up @@ -63,6 +71,9 @@ func canConvertCreateStatement(stmt *pgq.CreateStmt) bool {
// Setting a tablespace is not supported
case stmt.GetTablespacename() != "":
return false
// CREATE TABLE OF type_name is not supported
case stmt.GetOfTypename() != nil:
return false
default:
return true
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/sql2pgroll/create_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func TestUnconvertableCreateTableStatements(t *testing.T) {
// Any kind of partitioning is not supported
"CREATE TABLE foo(a int) PARTITION BY RANGE (a)",
"CREATE TABLE foo(a int) PARTITION BY LIST (a)",
"CREATE TABLE foo PARTITION OF bar FOR VALUES FROM (1) to (10)",

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

// Specifying a tablespace is not supported
"CREATE TABLE foo(a int) TABLESPACE bar",

// CREATE TABLE OF type_name is not supported
"CREATE TABLE foo OF type_bar",

// The LIKE clause is not supported
"CREATE TABLE foo(a int, LIKE bar)",
"CREATE TABLE foo(LIKE bar)",
}

for _, sql := range tests {
Expand Down

0 comments on commit b727101

Please sign in to comment.