From a290477481a4e50d8d2a37266b64dc9ab9641e68 Mon Sep 17 00:00:00 2001 From: Andrew Farries Date: Wed, 18 Dec 2024 08:37:19 +0000 Subject: [PATCH 1/3] Fall back to raw SQL for `OF` clause `OF type_name` is not representable as an `OpCreateTable` operation. --- pkg/sql2pgroll/create_table.go | 3 +++ pkg/sql2pgroll/create_table_test.go | 3 +++ 2 files changed, 6 insertions(+) diff --git a/pkg/sql2pgroll/create_table.go b/pkg/sql2pgroll/create_table.go index 4ea6baea..6c2e3977 100644 --- a/pkg/sql2pgroll/create_table.go +++ b/pkg/sql2pgroll/create_table.go @@ -63,6 +63,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..5906b4fc 100644 --- a/pkg/sql2pgroll/create_table_test.go +++ b/pkg/sql2pgroll/create_table_test.go @@ -104,6 +104,9 @@ 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", } for _, sql := range tests { From 10f9df4e9d0eefbaf792ed1ae6f1b816490d011e Mon Sep 17 00:00:00 2001 From: Andrew Farries Date: Wed, 18 Dec 2024 08:49:28 +0000 Subject: [PATCH 2/3] Fall back to raw SQL for `PARTITION OF` clause Partitioned tables are not representable as an `OpCreateTable` operation. The testcase passes because tables that are partitions of other tables have `inhRelations` set to the table they are a partition of, and table inheritance already falls back to raw SQL. --- pkg/sql2pgroll/create_table_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/sql2pgroll/create_table_test.go b/pkg/sql2pgroll/create_table_test.go index 5906b4fc..c5f4a317 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", From fc83197494f3c9adb324c38c4960e2407bbc32cf Mon Sep 17 00:00:00 2001 From: Andrew Farries Date: Wed, 18 Dec 2024 09:06:57 +0000 Subject: [PATCH 3/3] Fall back to raw SQL for `LIKE` clause `LIKE` clauses are not representable as an `OpCreateTable` operation. --- pkg/sql2pgroll/create_table.go | 20 ++++++++++++++------ pkg/sql2pgroll/create_table_test.go | 4 ++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/pkg/sql2pgroll/create_table.go b/pkg/sql2pgroll/create_table.go index 6c2e3977..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{ diff --git a/pkg/sql2pgroll/create_table_test.go b/pkg/sql2pgroll/create_table_test.go index c5f4a317..f705a7d2 100644 --- a/pkg/sql2pgroll/create_table_test.go +++ b/pkg/sql2pgroll/create_table_test.go @@ -108,6 +108,10 @@ func TestUnconvertableCreateTableStatements(t *testing.T) { // 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 {