Skip to content

Commit

Permalink
sql/postgres: allow creating/modifying pk using existing index
Browse files Browse the repository at this point in the history
  • Loading branch information
a8m committed Apr 8, 2024
1 parent cca6adc commit 88e84c1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
22 changes: 20 additions & 2 deletions sql/postgres/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,13 +460,21 @@ func (s *state) modifyTable(modify *schema.ModifyTable) error {

type (
// AddUniqueConstraint to the table using the given index. Note, if the index
// name does not match the constraint name, PostgreSQL implicitly renames it to
// the constraint name.
// name does not match the unique constraint name, PostgreSQL implicitly renames
// it to the constraint name.
AddUniqueConstraint struct {
schema.Change
Name string // Name of the constraint.
Using *schema.Index // Index to use for the constraint.
}
// AddPKConstraint to the table using the given index. Note, if the index
// name does not match the primary-key constraint name, PostgreSQL implicitly
// renames it to the constraint name.
AddPKConstraint struct {
schema.Change
Name string // Name of the constraint.
Using *schema.Index // Index to use for the constraint.
}
)

// alterTable modifies the given table by executing on it a list of changes in one SQL statement.
Expand Down Expand Up @@ -514,6 +522,16 @@ func (s *state) alterTable(t *schema.Table, changes []schema.Change) error {
// Translated to the DROP CONSTRAINT below,
// which drops the index as well.
reverse = append(reverse, &schema.DropIndex{I: drop})
case *AddPKConstraint:
b.P("ADD CONSTRAINT").Ident(change.Name).P("PRIMARY KEY USING INDEX").Ident(change.Using.Name)
drop := change.Using
if drop.Name != change.Name {
drop = sqlx.P(*change.Using)
drop.Name = change.Name
}
// Translated to the DROP CONSTRAINT below,
// which drops the index as well.
reverse = append(reverse, &schema.DropIndex{I: drop})
case *schema.AddIndex:
b.P("ADD")
if err := s.unique(b, change.I); err != nil {
Expand Down
32 changes: 32 additions & 0 deletions sql/postgres/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,38 @@ func TestPlanChanges(t *testing.T) {
},
},
},
// Add a primary key using index.
{
changes: []schema.Change{
func() schema.Change {
users := schema.NewTable("users").
SetSchema(schema.New("test")).
AddColumns(
schema.NewIntColumn("id", "bigint"),
)
users.SetPrimaryKey(schema.NewPrimaryKey(users.Columns...))
return &schema.ModifyTable{
T: users,
Changes: []schema.Change{
&AddPKConstraint{
Name: "users_pkey",
Using: schema.NewUniqueIndex("users_pkey"),
},
},
}
}(),
},
wantPlan: &migrate.Plan{
Reversible: true,
Transactional: true,
Changes: []*migrate.Change{
{
Cmd: `ALTER TABLE "test"."users" ADD CONSTRAINT "users_pkey" PRIMARY KEY USING INDEX "users_pkey"`,
Reverse: `ALTER TABLE "test"."users" DROP CONSTRAINT "users_pkey"`,
},
},
},
},
// Modify a primary key.
{
changes: []schema.Change{
Expand Down

0 comments on commit 88e84c1

Please sign in to comment.