Skip to content

Commit bc40891

Browse files
authored
Turn Duplicator into a DBAction (#937)
This PR renames `Duplicate` to `Execute`, so `Duplicator` can be used as a `DBAction`. Relates to #742
1 parent 914582b commit bc40891

File tree

5 files changed

+14
-14
lines changed

5 files changed

+14
-14
lines changed

pkg/migrations/duplicate.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import (
1414
"github.com/xataio/pgroll/pkg/schema"
1515
)
1616

17-
// Duplicator duplicates a column in a table, including all constraints and
17+
// duplicator duplicates a column in a table, including all constraints and
1818
// comments.
19-
type Duplicator struct {
19+
type duplicator struct {
2020
stmtBuilder *duplicatorStmtBuilder
2121
conn db.DB
2222
columns map[string]*columnToDuplicate
@@ -42,7 +42,7 @@ const (
4242
)
4343

4444
// NewColumnDuplicator creates a new Duplicator for a column.
45-
func NewColumnDuplicator(conn db.DB, table *schema.Table, columns ...*schema.Column) *Duplicator {
45+
func NewColumnDuplicator(conn db.DB, table *schema.Table, columns ...*schema.Column) *duplicator {
4646
cols := make(map[string]*columnToDuplicate, len(columns))
4747
for _, column := range columns {
4848
cols[column.Name] = &columnToDuplicate{
@@ -51,7 +51,7 @@ func NewColumnDuplicator(conn db.DB, table *schema.Table, columns ...*schema.Col
5151
withType: column.Type,
5252
}
5353
}
54-
return &Duplicator{
54+
return &duplicator{
5555
stmtBuilder: &duplicatorStmtBuilder{
5656
table: table,
5757
},
@@ -62,32 +62,32 @@ func NewColumnDuplicator(conn db.DB, table *schema.Table, columns ...*schema.Col
6262
}
6363

6464
// WithType sets the type of the new column.
65-
func (d *Duplicator) WithType(columnName, t string) *Duplicator {
65+
func (d *duplicator) WithType(columnName, t string) *duplicator {
6666
d.columns[columnName].withType = t
6767
return d
6868
}
6969

7070
// WithoutConstraint excludes a constraint from being duplicated.
71-
func (d *Duplicator) WithoutConstraint(c string) *Duplicator {
71+
func (d *duplicator) WithoutConstraint(c string) *duplicator {
7272
d.withoutConstraint = append(d.withoutConstraint, c)
7373
return d
7474
}
7575

7676
// WithoutNotNull excludes the NOT NULL constraint from being duplicated.
77-
func (d *Duplicator) WithoutNotNull(columnName string) *Duplicator {
77+
func (d *duplicator) WithoutNotNull(columnName string) *duplicator {
7878
d.columns[columnName].withoutNotNull = true
7979
return d
8080
}
8181

8282
// WithName sets the name of the new column.
83-
func (d *Duplicator) WithName(columnName, asName string) *Duplicator {
83+
func (d *duplicator) WithName(columnName, asName string) *duplicator {
8484
d.columns[columnName].asName = asName
8585
return d
8686
}
8787

8888
// Duplicate duplicates a column in the table, including all constraints and
8989
// comments.
90-
func (d *Duplicator) Duplicate(ctx context.Context) error {
90+
func (d *duplicator) Execute(ctx context.Context) error {
9191
colNames := make([]string, 0, len(d.columns))
9292
for name, c := range d.columns {
9393
colNames = append(colNames, name)

pkg/migrations/op_alter_column.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (o *OpAlterColumn) Start(ctx context.Context, l Logger, conn db.DB, latestS
3434
// Duplicate the column on the underlying table.
3535
d := duplicatorForOperations(ops, conn, table, column).
3636
WithName(column.Name, TemporaryName(o.Column))
37-
if err := d.Duplicate(ctx); err != nil {
37+
if err := d.Execute(ctx); err != nil {
3838
return nil, fmt.Errorf("failed to duplicate column: %w", err)
3939
}
4040

@@ -279,7 +279,7 @@ func (o *OpAlterColumn) subOperations() []Operation {
279279
}
280280

281281
// duplicatorForOperations returns a Duplicator for the given operations
282-
func duplicatorForOperations(ops []Operation, conn db.DB, table *schema.Table, column *schema.Column) *Duplicator {
282+
func duplicatorForOperations(ops []Operation, conn db.DB, table *schema.Table, column *schema.Column) *duplicator {
283283
d := NewColumnDuplicator(conn, table, column)
284284

285285
for _, op := range ops {

pkg/migrations/op_create_constraint.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (o *OpCreateConstraint) Start(ctx context.Context, l Logger, conn db.DB, la
3737
for _, colName := range o.Columns {
3838
d = d.WithName(table.GetColumn(colName).Name, TemporaryName(colName))
3939
}
40-
if err := d.Duplicate(ctx); err != nil {
40+
if err := d.Execute(ctx); err != nil {
4141
return nil, fmt.Errorf("failed to duplicate columns for new constraint: %w", err)
4242
}
4343

pkg/migrations/op_drop_constraint.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (o *OpDropConstraint) Start(ctx context.Context, l Logger, conn db.DB, late
3232

3333
// Create a copy of the column on the underlying table.
3434
d := NewColumnDuplicator(conn, table, column).WithoutConstraint(o.Name)
35-
if err := d.Duplicate(ctx); err != nil {
35+
if err := d.Execute(ctx); err != nil {
3636
return nil, fmt.Errorf("failed to duplicate column: %w", err)
3737
}
3838

pkg/migrations/op_drop_multicolumn_constraint.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (o *OpDropMultiColumnConstraint) Start(ctx context.Context, l Logger, conn
4444
for _, colName := range constraintColumns {
4545
d = d.WithName(table.GetColumn(colName).Name, TemporaryName(colName))
4646
}
47-
if err := d.Duplicate(ctx); err != nil {
47+
if err := d.Execute(ctx); err != nil {
4848
return nil, fmt.Errorf("failed to duplicate column: %w", err)
4949
}
5050

0 commit comments

Comments
 (0)