diff --git a/pkg/sql2pgroll/create_table.go b/pkg/sql2pgroll/create_table.go index 4ea6baea..5f055fef 100644 --- a/pkg/sql2pgroll/create_table.go +++ b/pkg/sql2pgroll/create_table.go @@ -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{ @@ -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 } diff --git a/pkg/sql2pgroll/create_table_test.go b/pkg/sql2pgroll/create_table_test.go index 62388a0f..f705a7d2 100644 --- a/pkg/sql2pgroll/create_table_test.go +++ b/pkg/sql2pgroll/create_table_test.go @@ -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", @@ -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 {