Skip to content

Commit e01a51f

Browse files
authored
New DBAction for setting replica identity (#938)
Relates to #742
1 parent 12dd5a2 commit e01a51f

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

pkg/migrations/dbactions.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,3 +736,33 @@ func (a *rawSQLAction) Execute(ctx context.Context) error {
736736
_, err := a.conn.ExecContext(ctx, a.sql)
737737
return err
738738
}
739+
740+
type setReplicaIdentityAction struct {
741+
conn db.DB
742+
table string
743+
identity string
744+
index string
745+
}
746+
747+
func NewSetReplicaIdentityAction(conn db.DB, table string, identityType, index string) *setReplicaIdentityAction {
748+
return &setReplicaIdentityAction{
749+
conn: conn,
750+
table: table,
751+
identity: strings.ToUpper(identityType),
752+
index: index,
753+
}
754+
}
755+
756+
func (a *setReplicaIdentityAction) Execute(ctx context.Context) error {
757+
// build the correct form of the `SET REPLICA IDENTITY` statement based on the`identity type
758+
identitySQL := a.identity
759+
if identitySQL == "INDEX" {
760+
identitySQL = fmt.Sprintf("USING INDEX %s", pq.QuoteIdentifier(a.index))
761+
}
762+
763+
// set the replica identity on the underlying table
764+
_, err := a.conn.ExecContext(ctx, fmt.Sprintf("ALTER TABLE %s REPLICA IDENTITY %s",
765+
pq.QuoteIdentifier(a.table),
766+
identitySQL))
767+
return err
768+
}

pkg/migrations/op_set_replica_identity.go

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@ package migrations
44

55
import (
66
"context"
7-
"fmt"
87
"slices"
98
"strings"
109

11-
"github.com/lib/pq"
12-
1310
"github.com/xataio/pgroll/pkg/backfill"
1411
"github.com/xataio/pgroll/pkg/db"
1512
"github.com/xataio/pgroll/pkg/schema"
@@ -20,17 +17,7 @@ var _ Operation = (*OpSetReplicaIdentity)(nil)
2017
func (o *OpSetReplicaIdentity) Start(ctx context.Context, l Logger, conn db.DB, latestSchema string, s *schema.Schema) (*backfill.Task, error) {
2118
l.LogOperationStart(o)
2219

23-
// build the correct form of the `SET REPLICA IDENTITY` statement based on the`identity type
24-
identitySQL := strings.ToUpper(o.Identity.Type)
25-
if identitySQL == "INDEX" {
26-
identitySQL = fmt.Sprintf("USING INDEX %s", pq.QuoteIdentifier(o.Identity.Index))
27-
}
28-
29-
// set the replica identity on the underlying table
30-
_, err := conn.ExecContext(ctx, fmt.Sprintf("ALTER TABLE %s REPLICA IDENTITY %s",
31-
pq.QuoteIdentifier(o.Table),
32-
identitySQL))
33-
return nil, err
20+
return nil, NewSetReplicaIdentityAction(conn, o.Table, o.Identity.Type, o.Identity.Index).Execute(ctx)
3421
}
3522

3623
func (o *OpSetReplicaIdentity) Complete(l Logger, conn db.DB, s *schema.Schema) ([]DBAction, error) {

0 commit comments

Comments
 (0)