Skip to content

Commit

Permalink
Convert RENAME CONSTRAINT SQL to pgroll operation (#514)
Browse files Browse the repository at this point in the history
Convert SQL statements like:

```sql
ALTER TABLE foo RENAME CONSTRAINT bar TO baz
```

to an `OpRenameConstraint` operation:

```json
[
  {
    "rename_constraint": {
      "table": "foo",
      "from": "bar",
      "to": "baz"
    }
  }
]
```

Part of #504
  • Loading branch information
andrew-farries authored Dec 5, 2024
1 parent 3512f76 commit 34b623f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
11 changes: 11 additions & 0 deletions pkg/sql2pgroll/expect/rename_constraint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// SPDX-License-Identifier: Apache-2.0

package expect

import "github.com/xataio/pgroll/pkg/migrations"

var RenameConstraintOp1 = &migrations.OpRenameConstraint{
Table: "foo",
From: "bar",
To: "baz",
}
17 changes: 17 additions & 0 deletions pkg/sql2pgroll/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ func convertRenameStmt(stmt *pgq.RenameStmt) (migrations.Operations, error) {
return convertRenameTable(stmt)
case pgq.ObjectType_OBJECT_COLUMN:
return convertRenameColumn(stmt)
case pgq.ObjectType_OBJECT_TABCONSTRAINT:
return convertRenameConstraint(stmt)
default:
return nil, nil
}
Expand Down Expand Up @@ -48,3 +50,18 @@ func convertRenameTable(stmt *pgq.RenameStmt) (migrations.Operations, error) {
},
}, nil
}

// convertRenameConstraint converts SQL statements like:
//
// `ALTER TABLE foo RENAME CONSTRAINT a TO b`
//
// to an OpRenameConstraint operation.
func convertRenameConstraint(stmt *pgq.RenameStmt) (migrations.Operations, error) {
return migrations.Operations{
&migrations.OpRenameConstraint{
Table: stmt.GetRelation().GetRelname(),
From: stmt.GetSubname(),
To: stmt.GetNewname(),
},
}, nil
}
4 changes: 4 additions & 0 deletions pkg/sql2pgroll/rename_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ func TestConvertRenameStatements(t *testing.T) {
sql: "ALTER TABLE foo RENAME TO bar",
expectedOp: expect.RenameTableOp1,
},
{
sql: "ALTER TABLE foo RENAME CONSTRAINT bar TO baz",
expectedOp: expect.RenameConstraintOp1,
},
}

for _, tc := range tests {
Expand Down

0 comments on commit 34b623f

Please sign in to comment.