Skip to content

Commit

Permalink
Fall back to raw SQL for DEFERRABLE constraints
Browse files Browse the repository at this point in the history
Deferrable constraints can't be represented by `pgroll` operations, so
fall back to raw SQL for them.
  • Loading branch information
andrew-farries committed Dec 19, 2024
1 parent fe66776 commit 6d7bb59
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
16 changes: 16 additions & 0 deletions pkg/sql2pgroll/create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
11 changes: 11 additions & 0 deletions pkg/sql2pgroll/create_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 6d7bb59

Please sign in to comment.