Skip to content

Commit 675bfd2

Browse files
authored
New DBAction: AlterSequenceOwnerAction (#848)
Relates to #742
1 parent 0290c11 commit 675bfd2

File tree

6 files changed

+52
-37
lines changed

6 files changed

+52
-37
lines changed

pkg/migrations/dbactions.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,3 +487,51 @@ func (a *createFKConstraintAction) Execute(ctx context.Context) error {
487487
_, err := a.conn.ExecContext(ctx, sql)
488488
return err
489489
}
490+
491+
type alterSequenceOwnerAction struct {
492+
conn db.DB
493+
table string
494+
from string
495+
to string
496+
}
497+
498+
func NewAlterSequenceOwnerAction(conn db.DB, table, from, to string) *alterSequenceOwnerAction {
499+
return &alterSequenceOwnerAction{
500+
conn: conn,
501+
table: table,
502+
from: from,
503+
to: to,
504+
}
505+
}
506+
507+
func (a *alterSequenceOwnerAction) Execute(ctx context.Context) error {
508+
sequence := getSequenceNameForColumn(ctx, a.conn, a.table, a.from)
509+
if sequence == "" {
510+
return nil
511+
}
512+
_, err := a.conn.ExecContext(ctx, fmt.Sprintf("ALTER SEQUENCE IF EXISTS %s OWNED BY %s.%s",
513+
sequence,
514+
pq.QuoteIdentifier(a.table),
515+
pq.QuoteIdentifier(a.to),
516+
))
517+
518+
return err
519+
}
520+
521+
func getSequenceNameForColumn(ctx context.Context, conn db.DB, tableName, columnName string) string {
522+
var sequenceName string
523+
query := fmt.Sprintf(`
524+
SELECT pg_get_serial_sequence('%s', '%s')
525+
`, pq.QuoteIdentifier(tableName), columnName)
526+
rows, err := conn.QueryContext(ctx, query)
527+
if err != nil {
528+
return ""
529+
}
530+
defer rows.Close()
531+
532+
if err := db.ScanFirstValue(rows, &sequenceName); err != nil {
533+
return ""
534+
}
535+
536+
return sequenceName
537+
}

pkg/migrations/duplicate.go

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -355,36 +355,3 @@ func errorIgnoringErrorCode(err error, code pq.ErrorCode) error {
355355

356356
return err
357357
}
358-
359-
func alterSequenceOwnerToDuplicatedColumn(ctx context.Context, conn db.DB, tableName, columnName string) error {
360-
sequenceName := getSequenceNameForColumn(ctx, conn, tableName, columnName)
361-
if sequenceName == "" {
362-
// No sequence for the column
363-
return nil
364-
}
365-
_, err := conn.ExecContext(ctx, fmt.Sprintf("ALTER SEQUENCE IF EXISTS %s OWNED BY %s.%s",
366-
sequenceName,
367-
pq.QuoteIdentifier(tableName),
368-
pq.QuoteIdentifier(TemporaryName(columnName)),
369-
))
370-
371-
return err
372-
}
373-
374-
func getSequenceNameForColumn(ctx context.Context, conn db.DB, tableName, columnName string) string {
375-
var sequenceName string
376-
query := fmt.Sprintf(`
377-
SELECT pg_get_serial_sequence('%s', '%s')
378-
`, pq.QuoteIdentifier(tableName), columnName)
379-
rows, err := conn.QueryContext(ctx, query)
380-
if err != nil {
381-
return ""
382-
}
383-
defer rows.Close()
384-
385-
if err := db.ScanFirstValue(rows, &sequenceName); err != nil {
386-
return ""
387-
}
388-
389-
return sequenceName
390-
}

pkg/migrations/op_alter_column.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func (o *OpAlterColumn) Complete(ctx context.Context, l Logger, conn db.DB, s *s
103103
}
104104
}
105105

106-
if err := alterSequenceOwnerToDuplicatedColumn(ctx, conn, o.Table, o.Column); err != nil {
106+
if err := NewAlterSequenceOwnerAction(conn, o.Table, o.Column, TemporaryName(o.Column)).Execute(ctx); err != nil {
107107
return err
108108
}
109109

pkg/migrations/op_create_constraint.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func (o *OpCreateConstraint) Complete(ctx context.Context, l Logger, conn db.DB,
147147
}
148148

149149
for _, col := range o.Columns {
150-
if err := alterSequenceOwnerToDuplicatedColumn(ctx, conn, o.Table, col); err != nil {
150+
if err := NewAlterSequenceOwnerAction(conn, o.Table, col, TemporaryName(col)).Execute(ctx); err != nil {
151151
return err
152152
}
153153
}

pkg/migrations/op_drop_constraint.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func (o *OpDropConstraint) Complete(ctx context.Context, l Logger, conn db.DB, s
9292
return err
9393
}
9494

95-
if err := alterSequenceOwnerToDuplicatedColumn(ctx, conn, o.Table, column.Name); err != nil {
95+
if err := NewAlterSequenceOwnerAction(conn, o.Table, column.Name, TemporaryName(column.Name)).Execute(ctx); err != nil {
9696
return err
9797
}
9898

pkg/migrations/op_drop_multicolumn_constraint.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func (o *OpDropMultiColumnConstraint) Complete(ctx context.Context, l Logger, co
109109
return err
110110
}
111111

112-
if err := alterSequenceOwnerToDuplicatedColumn(ctx, conn, o.Table, columnName); err != nil {
112+
if err := NewAlterSequenceOwnerAction(conn, o.Table, columnName, TemporaryName(columnName)).Execute(ctx); err != nil {
113113
return err
114114
}
115115

0 commit comments

Comments
 (0)