From 6d7bb59790b685c7fa10a9c1638dfea2637e7b07 Mon Sep 17 00:00:00 2001 From: Andrew Farries Date: Thu, 19 Dec 2024 12:07:48 +0000 Subject: [PATCH] Fall back to raw SQL for `DEFERRABLE` constraints Deferrable constraints can't be represented by `pgroll` operations, so fall back to raw SQL for them. --- pkg/sql2pgroll/create_table.go | 16 ++++++++++++++++ pkg/sql2pgroll/create_table_test.go | 11 +++++++++++ 2 files changed, 27 insertions(+) diff --git a/pkg/sql2pgroll/create_table.go b/pkg/sql2pgroll/create_table.go index 40e77541..3c055963 100644 --- a/pkg/sql2pgroll/create_table.go +++ b/pkg/sql2pgroll/create_table.go @@ -139,12 +139,28 @@ func convertColumnDef(tableName string, col *pgq.ColumnDef) (*migrations.Column, if foreignKey == nil { return nil, nil } + case pgq.ConstrType_CONSTR_ATTR_NOT_DEFERRABLE: + // NOT DEFERRABLE constraints are the default and are supported, but no + // extra annotation is needed + continue case pgq.ConstrType_CONSTR_GENERATED: // Generated columns are not supported return nil, nil case pgq.ConstrType_CONSTR_IDENTITY: // Identity columns are not supported return nil, nil + case pgq.ConstrType_CONSTR_ATTR_DEFERRABLE: + // Deferrable constraints are not supported + return nil, nil + case pgq.ConstrType_CONSTR_ATTR_IMMEDIATE: + // Initially immediate deferred constraints are not supported + return nil, nil + case pgq.ConstrType_CONSTR_ATTR_DEFERRED: + // Initially deferred deferred constraints are not supported + return nil, nil + default: + // Any other type of constraint is not supported + return nil, nil } } diff --git a/pkg/sql2pgroll/create_table_test.go b/pkg/sql2pgroll/create_table_test.go index 032a3c37..31b6a914 100644 --- a/pkg/sql2pgroll/create_table_test.go +++ b/pkg/sql2pgroll/create_table_test.go @@ -235,6 +235,17 @@ func TestUnconvertableCreateTableStatements(t *testing.T) { // Generated columns are not supported "CREATE TABLE foo(a int GENERATED ALWAYS AS (1) STORED)", "CREATE TABLE foo(a int GENERATED ALWAYS AS IDENTITY)", + + // Deferrable constraints are not supported + "CREATE TABLE foo(a int UNIQUE DEFERRABLE)", + "CREATE TABLE foo(a int PRIMARY KEY DEFERRABLE)", + "CREATE TABLE foo(a int REFERENCES bar(b) DEFERRABLE)", + "CREATE TABLE foo(a int UNIQUE DEFERRABLE INITIALLY IMMEDIATE)", + "CREATE TABLE foo(a int PRIMARY KEY DEFERRABLE INITIALLY IMMEDIATE)", + "CREATE TABLE foo(a int REFERENCES bar(b) DEFERRABLE INITIALLY IMMEDIATE)", + "CREATE TABLE foo(a int UNIQUE DEFERRABLE INITIALLY DEFERRED)", + "CREATE TABLE foo(a int PRIMARY KEY DEFERRABLE INITIALLY DEFERRED)", + "CREATE TABLE foo(a int REFERENCES bar(b) DEFERRABLE INITIALLY DEFERRED)", } for _, sql := range tests {