Skip to content

Commit acc59fc

Browse files
committed
Return list of DBAction from Complete functions
1 parent b117582 commit acc59fc

25 files changed

+163
-276
lines changed

pkg/migrations/migrations.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type Operation interface {
2525
// Complete will update the database schema to match the current version
2626
// after calling Start.
2727
// This method should be called once the previous version is no longer used.
28-
Complete(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) error
28+
Complete(l Logger, conn db.DB, s *schema.Schema) ([]DBAction, error)
2929

3030
// Rollback will revert the changes made by Start. It is not possible to
3131
// rollback a completed migration.

pkg/migrations/op_add_column.go

Lines changed: 17 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -141,69 +141,43 @@ func toSchemaColumn(c Column) *schema.Column {
141141
return tmpColumn
142142
}
143143

144-
func (o *OpAddColumn) Complete(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) error {
144+
func (o *OpAddColumn) Complete(l Logger, conn db.DB, s *schema.Schema) ([]DBAction, error) {
145145
l.LogOperationComplete(o)
146146

147-
err := NewRenameColumnAction(conn, o.Table, TemporaryName(o.Column.Name), o.Column.Name).Execute(ctx)
148-
if err != nil {
149-
return err
150-
}
151-
152-
err = NewDropFunctionAction(conn, backfill.TriggerFunctionName(o.Table, o.Column.Name)).Execute(ctx)
153-
if err != nil {
154-
return err
155-
}
156-
157-
removeBackfillColumn := NewDropColumnAction(conn, o.Table, backfill.CNeedsBackfillColumn)
158-
err = removeBackfillColumn.Execute(ctx)
159-
if err != nil {
160-
return err
147+
dbActions := []DBAction{
148+
NewRenameColumnAction(conn, o.Table, TemporaryName(o.Column.Name), o.Column.Name),
149+
NewDropFunctionAction(conn, backfill.TriggerFunctionName(o.Table, o.Column.Name)),
150+
NewDropColumnAction(conn, o.Table, backfill.CNeedsBackfillColumn),
161151
}
162152

163153
if !o.Column.IsNullable() && o.Column.Default == nil {
164-
err = upgradeNotNullConstraintToNotNullAttribute(ctx, conn, o.Table, o.Column.Name)
165-
if err != nil {
166-
return err
167-
}
154+
dbActions = append(dbActions, upgradeNotNullConstraintToNotNullAttribute(conn, o.Table, o.Column.Name)...)
168155
}
169156

170157
if o.Column.Check != nil {
171-
err = NewValidateConstraintAction(conn, o.Table, o.Column.Check.Name).Execute(ctx)
172-
if err != nil {
173-
return err
174-
}
158+
dbActions = append(dbActions, NewValidateConstraintAction(conn, o.Table, o.Column.Check.Name))
175159
}
176160

177161
if o.Column.Unique {
178-
err := NewAddConstraintUsingUniqueIndex(conn,
162+
dbActions = append(dbActions, NewAddConstraintUsingUniqueIndex(conn,
179163
o.Table,
180164
o.Column.Name,
181-
UniqueIndexName(o.Column.Name),
182-
).Execute(ctx)
183-
if err != nil {
184-
return err
185-
}
165+
UniqueIndexName(o.Column.Name)))
186166
}
187167

188168
// If the column has a DEFAULT that could not be set using the fast-path
189169
// optimization, set it here.
190170
column := s.GetTable(o.Table).GetColumn(TemporaryName(o.Column.Name))
191171
if o.Column.HasDefault() && column.Default == nil {
192-
err := NewSetDefaultValueAction(conn, o.Table, o.Column.Name, *o.Column.Default).Execute(ctx)
193-
if err != nil {
194-
return err
195-
}
172+
dbActions = append(dbActions, NewSetDefaultValueAction(conn, o.Table, o.Column.Name, *o.Column.Default))
196173

197174
// Validate the `NOT NULL` constraint on the column if necessary
198175
if !o.Column.IsNullable() {
199-
err = upgradeNotNullConstraintToNotNullAttribute(ctx, conn, o.Table, o.Column.Name)
200-
if err != nil {
201-
return err
202-
}
176+
dbActions = append(dbActions, upgradeNotNullConstraintToNotNullAttribute(conn, o.Table, o.Column.Name)...)
203177
}
204178
}
205179

206-
return nil
180+
return dbActions, nil
207181
}
208182

209183
func (o *OpAddColumn) Rollback(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) error {
@@ -347,20 +321,12 @@ func addColumn(ctx context.Context, conn db.DB, o OpAddColumn, t *schema.Table,
347321
// upgradeNotNullConstraintToNotNullAttribute validates and upgrades a NOT NULL
348322
// constraint to a NOT NULL column attribute. The constraint is removed after
349323
// the column attribute is added.
350-
func upgradeNotNullConstraintToNotNullAttribute(ctx context.Context, conn db.DB, tableName, columnName string) error {
351-
err := NewValidateConstraintAction(conn, tableName, NotNullConstraintName(columnName)).Execute(ctx)
352-
if err != nil {
353-
return err
354-
}
355-
356-
err = NewSetNotNullAction(conn, tableName, columnName).Execute(ctx)
357-
if err != nil {
358-
return err
324+
func upgradeNotNullConstraintToNotNullAttribute(conn db.DB, tableName, columnName string) []DBAction {
325+
return []DBAction{
326+
NewValidateConstraintAction(conn, tableName, NotNullConstraintName(columnName)),
327+
NewSetNotNullAction(conn, tableName, columnName),
328+
NewDropConstraintAction(conn, tableName, NotNullConstraintName(columnName)),
359329
}
360-
361-
err = NewDropConstraintAction(conn, tableName, NotNullConstraintName(columnName)).Execute(ctx)
362-
363-
return err
364330
}
365331

366332
// UniqueIndexName returns the name of the unique index for the given column

pkg/migrations/op_alter_column.go

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -91,54 +91,41 @@ func (o *OpAlterColumn) Start(ctx context.Context, l Logger, conn db.DB, latestS
9191
return backfill.NewTask(table), nil
9292
}
9393

94-
func (o *OpAlterColumn) Complete(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) error {
94+
func (o *OpAlterColumn) Complete(l Logger, conn db.DB, s *schema.Schema) ([]DBAction, error) {
9595
l.LogOperationComplete(o)
9696

9797
ops := o.subOperations()
9898

99+
dbActions := []DBAction{}
99100
// Perform any operation specific completion steps
100101
for _, op := range ops {
101-
if err := op.Complete(ctx, l, conn, s); err != nil {
102-
return err
102+
actions, err := op.Complete(l, conn, s)
103+
if err != nil {
104+
return []DBAction{}, err
103105
}
104-
}
105-
106-
if err := NewAlterSequenceOwnerAction(conn, o.Table, o.Column, TemporaryName(o.Column)).Execute(ctx); err != nil {
107-
return err
108-
}
109-
110-
removeOldColumn := NewDropColumnAction(conn, o.Table, o.Column)
111-
err := removeOldColumn.Execute(ctx)
112-
if err != nil {
113-
return err
114-
}
115-
116-
// Remove the up and down function and trigger
117-
err = NewDropFunctionAction(conn, backfill.TriggerFunctionName(o.Table, o.Column), backfill.TriggerFunctionName(o.Table, TemporaryName(o.Column))).Execute(ctx)
118-
if err != nil {
119-
return err
120-
}
121-
122-
removeBackfillColumn := NewDropColumnAction(conn, o.Table, backfill.CNeedsBackfillColumn)
123-
err = removeBackfillColumn.Execute(ctx)
124-
if err != nil {
125-
return err
106+
dbActions = append(dbActions, actions...)
126107
}
127108

128109
// Rename the new column to the old column name
129110
table := s.GetTable(o.Table)
130111
if table == nil {
131-
return TableDoesNotExistError{Name: o.Table}
112+
return []DBAction{}, TableDoesNotExistError{Name: o.Table}
132113
}
133114
column := table.GetColumn(o.Column)
134115
if column == nil {
135-
return ColumnDoesNotExistError{Table: o.Table, Name: o.Column}
136-
}
137-
if err := NewRenameDuplicatedColumnAction(conn, table, column.Name).Execute(ctx); err != nil {
138-
return err
139-
}
140-
141-
return nil
116+
return []DBAction{}, ColumnDoesNotExistError{Table: o.Table, Name: o.Column}
117+
}
118+
119+
return append(dbActions, []DBAction{
120+
NewAlterSequenceOwnerAction(conn, o.Table, o.Column, TemporaryName(o.Column)),
121+
NewDropColumnAction(conn, o.Table, o.Column),
122+
NewDropFunctionAction(conn,
123+
backfill.TriggerFunctionName(o.Table, o.Column),
124+
backfill.TriggerFunctionName(o.Table, TemporaryName(o.Column)),
125+
),
126+
NewDropColumnAction(conn, o.Table, backfill.CNeedsBackfillColumn),
127+
NewRenameDuplicatedColumnAction(conn, table, column.Name),
128+
}...), nil
142129
}
143130

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

pkg/migrations/op_change_type.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ func (o *OpChangeType) Start(ctx context.Context, l Logger, conn db.DB, latestSc
3131
return backfill.NewTask(table), nil
3232
}
3333

34-
func (o *OpChangeType) Complete(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) error {
34+
func (o *OpChangeType) Complete(l Logger, conn db.DB, s *schema.Schema) ([]DBAction, error) {
3535
l.LogOperationComplete(o)
3636

37-
return nil
37+
return []DBAction{}, nil
3838
}
3939

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

pkg/migrations/op_create_constraint.go

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -101,83 +101,73 @@ func (o *OpCreateConstraint) Start(ctx context.Context, l Logger, conn db.DB, la
101101
return task, nil
102102
}
103103

104-
func (o *OpCreateConstraint) Complete(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) error {
104+
func (o *OpCreateConstraint) Complete(l Logger, conn db.DB, s *schema.Schema) ([]DBAction, error) {
105105
l.LogOperationComplete(o)
106106

107+
dbActions := make([]DBAction, 0)
107108
switch o.Type {
108109
case OpCreateConstraintTypeUnique:
109110
uniqueOp := &OpSetUnique{
110111
Table: o.Table,
111112
Name: o.Name,
112113
}
113-
err := uniqueOp.Complete(ctx, l, conn, s)
114+
actions, err := uniqueOp.Complete(l, conn, s)
114115
if err != nil {
115-
return err
116+
return []DBAction{}, err
116117
}
118+
dbActions = append(dbActions, actions...)
117119
case OpCreateConstraintTypeCheck:
118120
checkOp := &OpSetCheckConstraint{
119121
Table: o.Table,
120122
Check: CheckConstraint{
121123
Name: o.Name,
122124
},
123125
}
124-
err := checkOp.Complete(ctx, l, conn, s)
126+
actions, err := checkOp.Complete(l, conn, s)
125127
if err != nil {
126-
return err
128+
return []DBAction{}, err
127129
}
130+
dbActions = append(dbActions, actions...)
128131
case OpCreateConstraintTypeForeignKey:
129132
fkOp := &OpSetForeignKey{
130133
Table: o.Table,
131134
References: ForeignKeyReference{
132135
Name: o.Name,
133136
},
134137
}
135-
err := fkOp.Complete(ctx, l, conn, s)
138+
actions, err := fkOp.Complete(l, conn, s)
136139
if err != nil {
137-
return err
140+
return []DBAction{}, err
138141
}
142+
dbActions = append(dbActions, actions...)
139143
case OpCreateConstraintTypePrimaryKey:
140-
err := NewAddPrimaryKeyAction(conn, o.Table, o.Name).Execute(ctx)
141-
if err != nil {
142-
return err
143-
}
144+
dbActions = append(dbActions, NewAddPrimaryKeyAction(conn, o.Table, o.Name))
144145
}
145146

146147
for _, col := range o.Columns {
147-
if err := NewAlterSequenceOwnerAction(conn, o.Table, col, TemporaryName(col)).Execute(ctx); err != nil {
148-
return err
149-
}
148+
dbActions = append(dbActions, NewAlterSequenceOwnerAction(conn, o.Table, col, TemporaryName(col)))
150149
}
151150

152-
removeOldColumns := NewDropColumnAction(conn, o.Table, o.Columns...)
153-
err := removeOldColumns.Execute(ctx)
154-
if err != nil {
155-
return err
156-
}
151+
dbActions = append(dbActions, NewDropColumnAction(conn, o.Table, o.Columns...))
157152

158153
// rename new columns to old name
159154
table := s.GetTable(o.Table)
160155
if table == nil {
161-
return TableDoesNotExistError{Name: o.Table}
156+
return []DBAction{}, TableDoesNotExistError{Name: o.Table}
162157
}
163158
for _, col := range o.Columns {
164159
column := table.GetColumn(col)
165160
if column == nil {
166-
return ColumnDoesNotExistError{Table: o.Table, Name: col}
161+
return []DBAction{}, ColumnDoesNotExistError{Table: o.Table, Name: col}
167162
}
168-
if err := NewRenameDuplicatedColumnAction(conn, table, column.Name).Execute(ctx); err != nil {
169-
return err
170-
}
171-
}
172-
173-
if err := o.removeTriggers(ctx, conn); err != nil {
174-
return err
163+
dbActions = append(dbActions, NewRenameDuplicatedColumnAction(conn, table, column.Name))
175164
}
165+
dbActions = append(dbActions,
166+
o.removeTriggers(conn),
167+
NewDropColumnAction(conn, o.Table, backfill.CNeedsBackfillColumn),
168+
)
176169

177-
removeBackfillColumn := NewDropColumnAction(conn, o.Table, backfill.CNeedsBackfillColumn)
178-
err = removeBackfillColumn.Execute(ctx)
179-
180-
return err
170+
return dbActions, nil
181171
}
182172

183173
func (o *OpCreateConstraint) Rollback(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) error {
@@ -194,7 +184,7 @@ func (o *OpCreateConstraint) Rollback(ctx context.Context, l Logger, conn db.DB,
194184
return err
195185
}
196186

197-
if err := o.removeTriggers(ctx, conn); err != nil {
187+
if err := o.removeTriggers(conn).Execute(ctx); err != nil {
198188
return err
199189
}
200190

@@ -204,13 +194,13 @@ func (o *OpCreateConstraint) Rollback(ctx context.Context, l Logger, conn db.DB,
204194
return err
205195
}
206196

207-
func (o *OpCreateConstraint) removeTriggers(ctx context.Context, conn db.DB) error {
197+
func (o *OpCreateConstraint) removeTriggers(conn db.DB) DBAction {
208198
dropFuncs := make([]string, 0, len(o.Columns)*2)
209199
for _, column := range o.Columns {
210200
dropFuncs = append(dropFuncs, backfill.TriggerFunctionName(o.Table, column))
211201
dropFuncs = append(dropFuncs, backfill.TriggerFunctionName(o.Table, TemporaryName(column)))
212202
}
213-
return NewDropFunctionAction(conn, dropFuncs...).Execute(ctx)
203+
return NewDropFunctionAction(conn, dropFuncs...)
214204
}
215205

216206
func (o *OpCreateConstraint) Validate(ctx context.Context, s *schema.Schema) error {

pkg/migrations/op_create_index.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ func (o *OpCreateIndex) Start(ctx context.Context, l Logger, conn db.DB, latestS
7979
return nil, err
8080
}
8181

82-
func (o *OpCreateIndex) Complete(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) error {
82+
func (o *OpCreateIndex) Complete(l Logger, conn db.DB, s *schema.Schema) ([]DBAction, error) {
8383
l.LogOperationComplete(o)
8484

8585
// No-op
86-
return nil
86+
return []DBAction{}, nil
8787
}
8888

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

pkg/migrations/op_create_table.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ func (o *OpCreateTable) Start(ctx context.Context, l Logger, conn db.DB, latestS
6161
return nil, nil
6262
}
6363

64-
func (o *OpCreateTable) Complete(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) error {
64+
func (o *OpCreateTable) Complete(l Logger, conn db.DB, s *schema.Schema) ([]DBAction, error) {
6565
l.LogOperationComplete(o)
6666

6767
// No-op
68-
return nil
68+
return []DBAction{}, nil
6969
}
7070

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

pkg/migrations/op_drop_column.go

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,27 +49,14 @@ func (o *OpDropColumn) Start(ctx context.Context, l Logger, conn db.DB, latestSc
4949
return nil, nil
5050
}
5151

52-
func (o *OpDropColumn) Complete(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) error {
52+
func (o *OpDropColumn) Complete(l Logger, conn db.DB, s *schema.Schema) ([]DBAction, error) {
5353
l.LogOperationComplete(o)
5454

55-
removeColumn := NewDropColumnAction(conn, o.Table, o.Column)
56-
err := removeColumn.Execute(ctx)
57-
if err != nil {
58-
return err
59-
}
60-
61-
err = NewDropFunctionAction(conn, backfill.TriggerFunctionName(o.Table, o.Column)).Execute(ctx)
62-
if err != nil {
63-
return err
64-
}
65-
66-
removeBackfillColumn := NewDropColumnAction(conn, o.Table, backfill.CNeedsBackfillColumn)
67-
err = removeBackfillColumn.Execute(ctx)
68-
if err != nil {
69-
return err
70-
}
71-
72-
return nil
55+
return []DBAction{
56+
NewDropColumnAction(conn, o.Table, o.Column),
57+
NewDropFunctionAction(conn, backfill.TriggerFunctionName(o.Table, o.Column)),
58+
NewDropColumnAction(conn, o.Table, backfill.CNeedsBackfillColumn),
59+
}, nil
7360
}
7461

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

0 commit comments

Comments
 (0)