Skip to content

Commit 92bb10a

Browse files
authored
Combine the return values of Start into a single data class (#945)
New data structure holds the return value of `Start` function of operations. ```golang type StartOperation struct { Actions []DBAction BackfillTask *backfill.Task } ```
1 parent 470b5ef commit 92bb10a

25 files changed

+120
-104
lines changed

pkg/migrations/migrations.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type Operation interface {
2020
// version in the database (through a view)
2121
// update the given views to expose the new schema version
2222
// Returns the table that requires backfilling, if any.
23-
Start(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) ([]DBAction, *backfill.Task, error)
23+
Start(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) (*StartResult, error)
2424

2525
// Complete will update the database schema to match the current version
2626
// after calling Start.
@@ -78,6 +78,11 @@ type (
7878
VersionSchema string `json:"version_schema,omitempty"`
7979
Operations json.RawMessage `json:"operations"`
8080
}
81+
82+
StartResult struct {
83+
Actions []DBAction
84+
BackfillTask *backfill.Task
85+
}
8186
)
8287

8388
// VersionSchemaName returns the version schema name for the migration.
@@ -118,7 +123,7 @@ func (m *Migration) UpdateVirtualSchema(ctx context.Context, s *schema.Schema) e
118123
// Run `Start` on each operation using the fake DB. Updates will be made to
119124
// the in-memory schema `s` without touching the physical database.
120125
for _, op := range m.Operations {
121-
if _, _, err := op.Start(ctx, NewNoopLogger(), db, s); err != nil {
126+
if _, err := op.Start(ctx, NewNoopLogger(), db, s); err != nil {
122127
return err
123128
}
124129
}

pkg/migrations/op_add_column.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ var (
1919
_ Createable = (*OpAddColumn)(nil)
2020
)
2121

22-
func (o *OpAddColumn) Start(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) ([]DBAction, *backfill.Task, error) {
22+
func (o *OpAddColumn) Start(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) (*StartResult, error) {
2323
l.LogOperationStart(o)
2424

2525
table := s.GetTable(o.Table)
2626
if table == nil {
27-
return nil, nil, TableDoesNotExistError{Name: o.Table}
27+
return nil, TableDoesNotExistError{Name: o.Table}
2828
}
2929

3030
// If the column has a DEFAULT, check if it can be added using the fast path
@@ -33,14 +33,14 @@ func (o *OpAddColumn) Start(ctx context.Context, l Logger, conn db.DB, s *schema
3333
if o.Column.HasDefault() {
3434
v, err := defaults.UsesFastPath(ctx, conn, table.Name, o.Column.Type, *o.Column.Default)
3535
if err != nil {
36-
return nil, nil, fmt.Errorf("failed to check for fast path default optimization: %w", err)
36+
return nil, fmt.Errorf("failed to check for fast path default optimization: %w", err)
3737
}
3838
fastPathDefault = v
3939
}
4040

4141
action, err := addColumn(conn, *o, table, fastPathDefault)
4242
if err != nil {
43-
return nil, nil, err
43+
return nil, err
4444
}
4545
dbActions := []DBAction{action}
4646

@@ -96,7 +96,7 @@ func (o *OpAddColumn) Start(ctx context.Context, l Logger, conn db.DB, s *schema
9696
// value for the column.
9797
if o.Column.HasDefault() && !fastPathDefault {
9898
if o.Up != *o.Column.Default {
99-
return nil, nil, UpSQLMustBeColumnDefaultError{Column: o.Column.Name}
99+
return nil, UpSQLMustBeColumnDefaultError{Column: o.Column.Name}
100100
}
101101
}
102102

@@ -118,7 +118,7 @@ func (o *OpAddColumn) Start(ctx context.Context, l Logger, conn db.DB, s *schema
118118
tmpColumn.Name = TemporaryName(o.Column.Name)
119119
table.AddColumn(o.Column.Name, tmpColumn)
120120

121-
return dbActions, task, nil
121+
return &StartResult{Actions: dbActions, BackfillTask: task}, nil
122122
}
123123

124124
func toSchemaColumn(c Column) *schema.Column {

pkg/migrations/op_alter_column.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,24 @@ var (
1818
_ Createable = (*OpAlterColumn)(nil)
1919
)
2020

21-
func (o *OpAlterColumn) Start(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) ([]DBAction, *backfill.Task, error) {
21+
func (o *OpAlterColumn) Start(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) (*StartResult, error) {
2222
l.LogOperationStart(o)
2323

2424
table := s.GetTable(o.Table)
2525
if table == nil {
26-
return nil, nil, TableDoesNotExistError{Name: o.Table}
26+
return nil, TableDoesNotExistError{Name: o.Table}
2727
}
2828
column := table.GetColumn(o.Column)
2929
if column == nil {
30-
return nil, nil, ColumnDoesNotExistError{Table: o.Table, Name: o.Column}
30+
return nil, ColumnDoesNotExistError{Table: o.Table, Name: o.Column}
3131
}
3232
ops := o.subOperations()
3333

3434
// Duplicate the column on the underlying table.
3535
d := duplicatorForOperations(ops, conn, table, column).
3636
WithName(column.Name, TemporaryName(o.Column))
3737
if err := d.Execute(ctx); err != nil {
38-
return nil, nil, fmt.Errorf("failed to duplicate column: %w", err)
38+
return nil, fmt.Errorf("failed to duplicate column: %w", err)
3939
}
4040

4141
// Copy the columns from table columns, so we can use it later
@@ -83,15 +83,15 @@ func (o *OpAlterColumn) Start(ctx context.Context, l Logger, conn db.DB, s *sche
8383
var dbActions []DBAction
8484
// perform any operation specific start steps
8585
for _, op := range ops {
86-
actions, bf, err := op.Start(ctx, l, conn, s)
86+
startOp, err := op.Start(ctx, l, conn, s)
8787
if err != nil {
88-
return nil, nil, err
88+
return nil, err
8989
}
90-
task.AddTriggers(bf)
91-
dbActions = append(dbActions, actions...)
90+
task.AddTriggers(startOp.BackfillTask)
91+
dbActions = append(dbActions, startOp.Actions...)
9292
}
9393

94-
return dbActions, task, nil
94+
return &StartResult{Actions: dbActions, BackfillTask: task}, nil
9595
}
9696

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

pkg/migrations/op_change_type.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ type OpChangeType struct {
2020

2121
var _ Operation = (*OpChangeType)(nil)
2222

23-
func (o *OpChangeType) Start(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) ([]DBAction, *backfill.Task, error) {
23+
func (o *OpChangeType) Start(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) (*StartResult, error) {
2424
l.LogOperationStart(o)
2525

2626
table := s.GetTable(o.Table)
2727
if table == nil {
28-
return nil, nil, TableDoesNotExistError{Name: o.Table}
28+
return nil, TableDoesNotExistError{Name: o.Table}
2929
}
3030

31-
return nil, backfill.NewTask(table), nil
31+
return &StartResult{BackfillTask: backfill.NewTask(table)}, nil
3232
}
3333

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

pkg/migrations/op_create_constraint.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ var (
1515
_ Createable = (*OpCreateConstraint)(nil)
1616
)
1717

18-
func (o *OpCreateConstraint) Start(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) ([]DBAction, *backfill.Task, error) {
18+
func (o *OpCreateConstraint) Start(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) (*StartResult, error) {
1919
l.LogOperationStart(o)
2020

2121
table := s.GetTable(o.Table)
2222
if table == nil {
23-
return nil, nil, TableDoesNotExistError{Name: o.Table}
23+
return nil, TableDoesNotExistError{Name: o.Table}
2424
}
2525

2626
columns := make([]*schema.Column, len(o.Columns))
2727
for i, colName := range o.Columns {
2828
columns[i] = table.GetColumn(colName)
2929
if columns[i] == nil {
30-
return nil, nil, ColumnDoesNotExistError{Table: o.Table, Name: colName}
30+
return nil, ColumnDoesNotExistError{Table: o.Table, Name: colName}
3131
}
3232
}
3333

@@ -89,22 +89,22 @@ func (o *OpCreateConstraint) Start(ctx context.Context, l Logger, conn db.DB, s
8989
dbActions = append(dbActions,
9090
NewCreateUniqueIndexConcurrentlyAction(conn, s.Name, o.Name, table.Name, temporaryNames(o.Columns)...),
9191
)
92-
return dbActions, task, nil
92+
return &StartResult{Actions: dbActions, BackfillTask: task}, nil
9393

9494
case OpCreateConstraintTypeCheck:
9595
dbActions = append(dbActions,
9696
NewCreateCheckConstraintAction(conn, table.Name, o.Name, *o.Check, o.Columns, o.NoInherit, true),
9797
)
98-
return dbActions, task, nil
98+
return &StartResult{Actions: dbActions, BackfillTask: task}, nil
9999

100100
case OpCreateConstraintTypeForeignKey:
101101
dbActions = append(dbActions,
102102
NewCreateFKConstraintAction(conn, table.Name, o.Name, temporaryNames(o.Columns), o.References, false, false, true),
103103
)
104-
return dbActions, task, nil
104+
return &StartResult{Actions: dbActions, BackfillTask: task}, nil
105105
}
106106

107-
return dbActions, task, nil
107+
return &StartResult{Actions: dbActions, BackfillTask: task}, nil
108108
}
109109

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

pkg/migrations/op_create_index.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
"github.com/lib/pq"
1010

11-
"github.com/xataio/pgroll/pkg/backfill"
1211
"github.com/xataio/pgroll/pkg/db"
1312
"github.com/xataio/pgroll/pkg/schema"
1413
)
@@ -18,12 +17,12 @@ var (
1817
_ Createable = (*OpCreateIndex)(nil)
1918
)
2019

21-
func (o *OpCreateIndex) Start(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) ([]DBAction, *backfill.Task, error) {
20+
func (o *OpCreateIndex) Start(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) (*StartResult, error) {
2221
l.LogOperationStart(o)
2322

2423
table := s.GetTable(o.Table)
2524
if table == nil {
26-
return nil, nil, TableDoesNotExistError{Name: o.Table}
25+
return nil, TableDoesNotExistError{Name: o.Table}
2726
}
2827

2928
cols := make(map[string]IndexField, len(o.Columns))
@@ -45,7 +44,7 @@ func (o *OpCreateIndex) Start(ctx context.Context, l Logger, conn db.DB, s *sche
4544
),
4645
}
4746

48-
return dbActions, nil, nil
47+
return &StartResult{Actions: dbActions}, nil
4948
}
5049

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

pkg/migrations/op_create_table.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"slices"
99
"strings"
1010

11-
"github.com/xataio/pgroll/pkg/backfill"
1211
"github.com/xataio/pgroll/pkg/db"
1312
"github.com/xataio/pgroll/pkg/schema"
1413
)
@@ -18,18 +17,18 @@ var (
1817
_ Createable = (*OpCreateTable)(nil)
1918
)
2019

21-
func (o *OpCreateTable) Start(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) ([]DBAction, *backfill.Task, error) {
20+
func (o *OpCreateTable) Start(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) (*StartResult, error) {
2221
l.LogOperationStart(o)
2322

2423
// Generate SQL for the columns in the table
2524
columnsSQL, err := columnsToSQL(o.Columns)
2625
if err != nil {
27-
return nil, nil, fmt.Errorf("failed to create columns SQL: %w", err)
26+
return nil, fmt.Errorf("failed to create columns SQL: %w", err)
2827
}
2928

3029
constraintsSQL, err := constraintsToSQL(o.Constraints)
3130
if err != nil {
32-
return nil, nil, fmt.Errorf("failed to create constraints SQL: %w", err)
31+
return nil, fmt.Errorf("failed to create constraints SQL: %w", err)
3332
}
3433

3534
dbActions := make([]DBAction, 0)
@@ -50,7 +49,7 @@ func (o *OpCreateTable) Start(ctx context.Context, l Logger, conn db.DB, s *sche
5049
// Update the in-memory schema representation with the new table
5150
o.updateSchema(s)
5251

53-
return dbActions, nil, nil
52+
return &StartResult{Actions: dbActions}, nil
5453
}
5554

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

pkg/migrations/op_drop_column.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var (
1515
_ Createable = (*OpDropColumn)(nil)
1616
)
1717

18-
func (o *OpDropColumn) Start(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) ([]DBAction, *backfill.Task, error) {
18+
func (o *OpDropColumn) Start(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) (*StartResult, error) {
1919
l.LogOperationStart(o)
2020

2121
var task *backfill.Task
@@ -34,16 +34,16 @@ func (o *OpDropColumn) Start(ctx context.Context, l Logger, conn db.DB, s *schem
3434

3535
table := s.GetTable(o.Table)
3636
if table == nil {
37-
return nil, nil, TableDoesNotExistError{Name: o.Table}
37+
return nil, TableDoesNotExistError{Name: o.Table}
3838
}
3939
column := table.GetColumn(o.Column)
4040
if column == nil {
41-
return nil, nil, ColumnDoesNotExistError{Table: o.Table, Name: o.Column}
41+
return nil, ColumnDoesNotExistError{Table: o.Table, Name: o.Column}
4242
}
4343

4444
s.GetTable(o.Table).RemoveColumn(o.Column)
4545

46-
return nil, task, nil
46+
return &StartResult{BackfillTask: task}, nil
4747
}
4848

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

pkg/migrations/op_drop_constraint.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ import (
1414

1515
var _ Operation = (*OpDropConstraint)(nil)
1616

17-
func (o *OpDropConstraint) Start(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) ([]DBAction, *backfill.Task, error) {
17+
func (o *OpDropConstraint) Start(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) (*StartResult, error) {
1818
l.LogOperationStart(o)
1919

2020
table := s.GetTable(o.Table)
2121
if table == nil {
22-
return nil, nil, TableDoesNotExistError{Name: o.Table}
22+
return nil, TableDoesNotExistError{Name: o.Table}
2323
}
2424

2525
// By this point Validate() should have run which ensures the constraint exists and that we only have
2626
// one column associated with it.
2727
column := table.GetColumn(table.GetConstraintColumns(o.Name)[0])
2828
if column == nil {
29-
return nil, nil, ColumnDoesNotExistError{Table: o.Table, Name: table.GetConstraintColumns(o.Name)[0]}
29+
return nil, ColumnDoesNotExistError{Table: o.Table, Name: table.GetConstraintColumns(o.Name)[0]}
3030
}
3131

3232
// Create a copy of the column on the underlying table.
@@ -72,7 +72,7 @@ func (o *OpDropConstraint) Start(ctx context.Context, l Logger, conn db.DB, s *s
7272
SQL: o.Down,
7373
},
7474
)
75-
return dbActions, backfill.NewTask(table, triggers...), nil
75+
return &StartResult{Actions: dbActions, BackfillTask: backfill.NewTask(table, triggers...)}, nil
7676
}
7777

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

pkg/migrations/op_drop_index.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package migrations
55
import (
66
"context"
77

8-
"github.com/xataio/pgroll/pkg/backfill"
98
"github.com/xataio/pgroll/pkg/db"
109
"github.com/xataio/pgroll/pkg/schema"
1110
)
@@ -15,11 +14,11 @@ var (
1514
_ Createable = (*OpDropIndex)(nil)
1615
)
1716

18-
func (o *OpDropIndex) Start(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) ([]DBAction, *backfill.Task, error) {
17+
func (o *OpDropIndex) Start(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) (*StartResult, error) {
1918
l.LogOperationStart(o)
2019

2120
// no-op
22-
return nil, nil, nil
21+
return nil, nil
2322
}
2423

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

0 commit comments

Comments
 (0)