Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle more unconvertible CREATE TABLE statements #547

Merged
merged 3 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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