Skip to content

Commit 934a20e

Browse files
authored
Add new DBAction to rename tables (#893)
Related to #742
1 parent 6f73c9e commit 934a20e

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

pkg/migrations/dbactions.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,29 @@ func (a *dropColumnAction) dropMultipleColumns() string {
4949
return strings.Join(cols, ", ")
5050
}
5151

52+
// renameTableAction is a DBAction that renames a table.
53+
type renameTableAction struct {
54+
conn db.DB
55+
56+
from string
57+
to string
58+
}
59+
60+
func NewRenameTableAction(conn db.DB, from, to string) *renameTableAction {
61+
return &renameTableAction{
62+
conn: conn,
63+
from: from,
64+
to: to,
65+
}
66+
}
67+
68+
func (a *renameTableAction) Execute(ctx context.Context) error {
69+
_, err := a.conn.ExecContext(ctx, fmt.Sprintf("ALTER TABLE IF EXISTS %s RENAME TO %s",
70+
pq.QuoteIdentifier(a.from),
71+
pq.QuoteIdentifier(a.to)))
72+
return err
73+
}
74+
5275
// renameColumnAction is a DBAction that renames a column in a table.
5376
type renameColumnAction struct {
5477
conn db.DB

pkg/migrations/op_drop_table.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ func (o *OpDropTable) Start(ctx context.Context, l Logger, conn db.DB, latestSch
2525

2626
// Soft-delete the table in order that a create table operation in the same
2727
// migration can create a table with the same name
28-
_, err := conn.ExecContext(ctx, fmt.Sprintf("ALTER TABLE IF EXISTS %s RENAME TO %s",
29-
table.Name,
30-
DeletionName(table.Name)))
28+
err := NewRenameTableAction(conn, table.Name, DeletionName(table.Name)).Execute(ctx)
3129
if err != nil {
3230
return nil, fmt.Errorf("failed to rename table %s: %w", o.Name, err)
3331
}
@@ -54,9 +52,7 @@ func (o *OpDropTable) Rollback(ctx context.Context, l Logger, conn db.DB, s *sch
5452

5553
// Rename the table back to its original name from its soft-deleted name
5654
table := s.GetTable(o.Name)
57-
_, err := conn.ExecContext(ctx, fmt.Sprintf("ALTER TABLE IF EXISTS %s RENAME TO %s",
58-
DeletionName(table.Name),
59-
table.Name))
55+
err := NewRenameTableAction(conn, DeletionName(table.Name), table.Name).Execute(ctx)
6056
if err != nil {
6157
return fmt.Errorf("failed to rename table %s: %w", o.Name, err)
6258
}

pkg/migrations/op_rename_table.go

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

55
import (
66
"context"
7-
"fmt"
8-
9-
"github.com/lib/pq"
107

118
"github.com/xataio/pgroll/pkg/db"
129
"github.com/xataio/pgroll/pkg/schema"
@@ -23,11 +20,7 @@ func (o *OpRenameTable) Start(ctx context.Context, l Logger, conn db.DB, latestS
2320
func (o *OpRenameTable) Complete(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) error {
2421
l.LogOperationComplete(o)
2522

26-
_, err := conn.ExecContext(ctx, fmt.Sprintf("ALTER TABLE IF EXISTS %s RENAME TO %s",
27-
pq.QuoteIdentifier(o.From),
28-
pq.QuoteIdentifier(o.To)))
29-
30-
return err
23+
return NewRenameTableAction(conn, o.From, o.To).Execute(ctx)
3124
}
3225

3326
func (o *OpRenameTable) Rollback(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) error {

0 commit comments

Comments
 (0)