Skip to content

Commit 12dd5a2

Browse files
authored
Return DBActions from Rollback (#929)
This PR changes the signature of `Rollback`. From now on, it returns a list of `DBActions`.
1 parent 531a919 commit 12dd5a2

25 files changed

+117
-165
lines changed

pkg/migrations/migrations.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type Operation interface {
2929

3030
// Rollback will revert the changes made by Start. It is not possible to
3131
// rollback a completed migration.
32-
Rollback(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) error
32+
Rollback(l Logger, conn db.DB, s *schema.Schema) ([]DBAction, error)
3333

3434
// Validate returns a descriptive error if the operation cannot be applied to the given schema.
3535
Validate(ctx context.Context, s *schema.Schema) error

pkg/migrations/op_add_column.go

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -180,33 +180,23 @@ func (o *OpAddColumn) Complete(l Logger, conn db.DB, s *schema.Schema) ([]DBActi
180180
return dbActions, nil
181181
}
182182

183-
func (o *OpAddColumn) Rollback(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) error {
183+
func (o *OpAddColumn) Rollback(l Logger, conn db.DB, s *schema.Schema) ([]DBAction, error) {
184184
l.LogOperationRollback(o)
185185

186186
table := s.GetTable(o.Table)
187187
if table == nil {
188-
return TableDoesNotExistError{Name: o.Table}
188+
return nil, TableDoesNotExistError{Name: o.Table}
189189
}
190190
column := table.GetColumn(o.Column.Name)
191191
if column == nil {
192-
return ColumnDoesNotExistError{Table: o.Table, Name: o.Column.Name}
193-
}
194-
195-
rollbackAddColumn := NewDropColumnAction(conn, table.Name, column.Name)
196-
err := rollbackAddColumn.Execute(ctx)
197-
if err != nil {
198-
return err
192+
return nil, ColumnDoesNotExistError{Table: o.Table, Name: o.Column.Name}
199193
}
200194

201-
err = NewDropFunctionAction(conn, backfill.TriggerFunctionName(o.Table, o.Column.Name)).Execute(ctx)
202-
if err != nil {
203-
return err
204-
}
205-
206-
removeBackfillColumn := NewDropColumnAction(conn, table.Name, backfill.CNeedsBackfillColumn)
207-
err = removeBackfillColumn.Execute(ctx)
208-
209-
return err
195+
return []DBAction{
196+
NewDropColumnAction(conn, table.Name, column.Name),
197+
NewDropFunctionAction(conn, backfill.TriggerFunctionName(o.Table, o.Column.Name)),
198+
NewDropColumnAction(conn, table.Name, backfill.CNeedsBackfillColumn),
199+
}, nil
210200
}
211201

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

pkg/migrations/op_alter_column.go

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -96,24 +96,24 @@ func (o *OpAlterColumn) Complete(l Logger, conn db.DB, s *schema.Schema) ([]DBAc
9696

9797
ops := o.subOperations()
9898

99-
dbActions := []DBAction{}
99+
dbActions := make([]DBAction, 0)
100100
// Perform any operation specific completion steps
101101
for _, op := range ops {
102102
actions, err := op.Complete(l, conn, s)
103103
if err != nil {
104-
return []DBAction{}, err
104+
return nil, err
105105
}
106106
dbActions = append(dbActions, actions...)
107107
}
108108

109109
// Rename the new column to the old column name
110110
table := s.GetTable(o.Table)
111111
if table == nil {
112-
return []DBAction{}, TableDoesNotExistError{Name: o.Table}
112+
return nil, TableDoesNotExistError{Name: o.Table}
113113
}
114114
column := table.GetColumn(o.Column)
115115
if column == nil {
116-
return []DBAction{}, ColumnDoesNotExistError{Table: o.Table, Name: o.Column}
116+
return nil, ColumnDoesNotExistError{Table: o.Table, Name: o.Column}
117117
}
118118

119119
return append(dbActions, []DBAction{
@@ -128,43 +128,39 @@ func (o *OpAlterColumn) Complete(l Logger, conn db.DB, s *schema.Schema) ([]DBAc
128128
}...), nil
129129
}
130130

131-
func (o *OpAlterColumn) Rollback(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) error {
131+
func (o *OpAlterColumn) Rollback(l Logger, conn db.DB, s *schema.Schema) ([]DBAction, error) {
132132
l.LogOperationRollback(o)
133133

134134
table := s.GetTable(o.Table)
135135
if table == nil {
136-
return TableDoesNotExistError{Name: o.Table}
136+
return nil, TableDoesNotExistError{Name: o.Table}
137137
}
138138
column := table.GetColumn(o.Column)
139139
if column == nil {
140-
return ColumnDoesNotExistError{Table: o.Table, Name: o.Column}
140+
return nil, ColumnDoesNotExistError{Table: o.Table, Name: o.Column}
141141
}
142142

143143
// Perform any operation specific rollback steps
144+
dbActions := make([]DBAction, 0)
144145
ops := o.subOperations()
145146
for _, ops := range ops {
146-
if err := ops.Rollback(ctx, l, conn, nil); err != nil {
147-
return err
147+
actions, err := ops.Rollback(l, conn, nil)
148+
if err != nil {
149+
return nil, err
148150
}
151+
dbActions = append(dbActions, actions...)
149152
}
150153

151-
rollbackAddColumn := NewDropColumnAction(conn, table.Name, column.Name)
152-
err := rollbackAddColumn.Execute(ctx)
153-
if err != nil {
154-
return err
155-
}
156-
157-
// Remove the up and down functions and triggers
158-
if err := NewDropFunctionAction(
159-
conn,
160-
backfill.TriggerFunctionName(o.Table, o.Column),
161-
backfill.TriggerFunctionName(o.Table, TemporaryName(o.Column)),
162-
).Execute(ctx); err != nil {
163-
return err
164-
}
154+
dbActions = append(dbActions,
155+
NewDropColumnAction(conn, table.Name, column.Name),
156+
NewDropFunctionAction(conn,
157+
backfill.TriggerFunctionName(o.Table, o.Column),
158+
backfill.TriggerFunctionName(o.Table, TemporaryName(o.Column)),
159+
),
160+
NewDropColumnAction(conn, table.Name, backfill.CNeedsBackfillColumn),
161+
)
165162

166-
removeBackfillColumn := NewDropColumnAction(conn, table.Name, backfill.CNeedsBackfillColumn)
167-
return removeBackfillColumn.Execute(ctx)
163+
return dbActions, nil
168164
}
169165

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

pkg/migrations/op_change_type.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ func (o *OpChangeType) Start(ctx context.Context, l Logger, conn db.DB, latestSc
3434
func (o *OpChangeType) Complete(l Logger, conn db.DB, s *schema.Schema) ([]DBAction, error) {
3535
l.LogOperationComplete(o)
3636

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

40-
func (o *OpChangeType) Rollback(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) error {
40+
func (o *OpChangeType) Rollback(l Logger, conn db.DB, s *schema.Schema) ([]DBAction, error) {
4141
l.LogOperationRollback(o)
4242

43-
return nil
43+
return nil, nil
4444
}
4545

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

pkg/migrations/op_create_constraint.go

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func (o *OpCreateConstraint) Complete(l Logger, conn db.DB, s *schema.Schema) ([
113113
}
114114
actions, err := uniqueOp.Complete(l, conn, s)
115115
if err != nil {
116-
return []DBAction{}, err
116+
return nil, err
117117
}
118118
dbActions = append(dbActions, actions...)
119119
case OpCreateConstraintTypeCheck:
@@ -125,7 +125,7 @@ func (o *OpCreateConstraint) Complete(l Logger, conn db.DB, s *schema.Schema) ([
125125
}
126126
actions, err := checkOp.Complete(l, conn, s)
127127
if err != nil {
128-
return []DBAction{}, err
128+
return nil, err
129129
}
130130
dbActions = append(dbActions, actions...)
131131
case OpCreateConstraintTypeForeignKey:
@@ -137,7 +137,7 @@ func (o *OpCreateConstraint) Complete(l Logger, conn db.DB, s *schema.Schema) ([
137137
}
138138
actions, err := fkOp.Complete(l, conn, s)
139139
if err != nil {
140-
return []DBAction{}, err
140+
return nil, err
141141
}
142142
dbActions = append(dbActions, actions...)
143143
case OpCreateConstraintTypePrimaryKey:
@@ -153,12 +153,12 @@ func (o *OpCreateConstraint) Complete(l Logger, conn db.DB, s *schema.Schema) ([
153153
// rename new columns to old name
154154
table := s.GetTable(o.Table)
155155
if table == nil {
156-
return []DBAction{}, TableDoesNotExistError{Name: o.Table}
156+
return nil, TableDoesNotExistError{Name: o.Table}
157157
}
158158
for _, col := range o.Columns {
159159
column := table.GetColumn(col)
160160
if column == nil {
161-
return []DBAction{}, ColumnDoesNotExistError{Table: o.Table, Name: col}
161+
return nil, ColumnDoesNotExistError{Table: o.Table, Name: col}
162162
}
163163
dbActions = append(dbActions, NewRenameDuplicatedColumnAction(conn, table, column.Name))
164164
}
@@ -170,28 +170,19 @@ func (o *OpCreateConstraint) Complete(l Logger, conn db.DB, s *schema.Schema) ([
170170
return dbActions, nil
171171
}
172172

173-
func (o *OpCreateConstraint) Rollback(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) error {
173+
func (o *OpCreateConstraint) Rollback(l Logger, conn db.DB, s *schema.Schema) ([]DBAction, error) {
174174
l.LogOperationRollback(o)
175175

176176
table := s.GetTable(o.Table)
177177
if table == nil {
178-
return TableDoesNotExistError{Name: o.Table}
179-
}
180-
181-
removeDuplicatedColumns := NewDropColumnAction(conn, table.Name, temporaryNames(o.Columns)...)
182-
err := removeDuplicatedColumns.Execute(ctx)
183-
if err != nil {
184-
return err
185-
}
186-
187-
if err := o.removeTriggers(conn).Execute(ctx); err != nil {
188-
return err
178+
return nil, TableDoesNotExistError{Name: o.Table}
189179
}
190180

191-
removeBackfillColumn := NewDropColumnAction(conn, table.Name, backfill.CNeedsBackfillColumn)
192-
err = removeBackfillColumn.Execute(ctx)
193-
194-
return err
181+
return []DBAction{
182+
NewDropColumnAction(conn, table.Name, temporaryNames(o.Columns)...),
183+
o.removeTriggers(conn),
184+
NewDropColumnAction(conn, table.Name, backfill.CNeedsBackfillColumn),
185+
}, nil
195186
}
196187

197188
func (o *OpCreateConstraint) removeTriggers(conn db.DB) DBAction {

pkg/migrations/op_create_index.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,14 @@ func (o *OpCreateIndex) Complete(l Logger, conn db.DB, s *schema.Schema) ([]DBAc
8383
l.LogOperationComplete(o)
8484

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

89-
func (o *OpCreateIndex) Rollback(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) error {
89+
func (o *OpCreateIndex) Rollback(l Logger, conn db.DB, s *schema.Schema) ([]DBAction, error) {
9090
l.LogOperationRollback(o)
9191

9292
// drop the index concurrently
93-
return NewDropIndexAction(conn, o.Name).Execute(ctx)
93+
return []DBAction{NewDropIndexAction(conn, o.Name)}, nil
9494
}
9595

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

pkg/migrations/op_create_table.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ func (o *OpCreateTable) Complete(l Logger, conn db.DB, s *schema.Schema) ([]DBAc
6565
l.LogOperationComplete(o)
6666

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

71-
func (o *OpCreateTable) Rollback(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) error {
71+
func (o *OpCreateTable) Rollback(l Logger, conn db.DB, s *schema.Schema) ([]DBAction, error) {
7272
l.LogOperationRollback(o)
7373

74-
return NewDropTableAction(conn, o.Name).Execute(ctx)
74+
return []DBAction{NewDropTableAction(conn, o.Name)}, nil
7575
}
7676

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

pkg/migrations/op_drop_column.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,27 +59,19 @@ func (o *OpDropColumn) Complete(l Logger, conn db.DB, s *schema.Schema) ([]DBAct
5959
}, nil
6060
}
6161

62-
func (o *OpDropColumn) Rollback(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) error {
62+
func (o *OpDropColumn) Rollback(l Logger, conn db.DB, s *schema.Schema) ([]DBAction, error) {
6363
l.LogOperationRollback(o)
6464

6565
table := s.GetTable(o.Table)
6666

67-
err := NewDropFunctionAction(conn, backfill.TriggerFunctionName(o.Table, o.Column)).Execute(ctx)
68-
if err != nil {
69-
return err
70-
}
71-
72-
removeBackfillColumn := NewDropColumnAction(conn, table.Name, backfill.CNeedsBackfillColumn)
73-
err = removeBackfillColumn.Execute(ctx)
74-
if err != nil {
75-
return err
76-
}
77-
7867
// Mark the column as no longer deleted so thats it's visible to preceding
7968
// rollback operations in the same migration
8069
s.GetTable(o.Table).UnRemoveColumn(o.Column)
8170

82-
return nil
71+
return []DBAction{
72+
NewDropFunctionAction(conn, backfill.TriggerFunctionName(o.Table, o.Column)),
73+
NewDropColumnAction(conn, table.Name, backfill.CNeedsBackfillColumn),
74+
}, nil
8375
}
8476

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

pkg/migrations/op_drop_constraint.go

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -97,32 +97,20 @@ func (o *OpDropConstraint) Complete(l Logger, conn db.DB, s *schema.Schema) ([]D
9797
}, nil
9898
}
9999

100-
func (o *OpDropConstraint) Rollback(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) error {
100+
func (o *OpDropConstraint) Rollback(l Logger, conn db.DB, s *schema.Schema) ([]DBAction, error) {
101101
l.LogOperationRollback(o)
102102

103103
// We have already validated that there is single column related to this constraint.
104104
table := s.GetTable(o.Table)
105105
columnName := table.GetConstraintColumns(o.Name)[0]
106106

107-
removeNewColumn := NewDropColumnAction(conn, o.Table, TemporaryName(columnName))
108-
err := removeNewColumn.Execute(ctx)
109-
if err != nil {
110-
return err
111-
}
112-
113-
// Remove the up and down functions and triggers
114-
if err := NewDropFunctionAction(
115-
conn,
116-
backfill.TriggerFunctionName(o.Table, columnName),
117-
backfill.TriggerFunctionName(o.Table, TemporaryName(columnName)),
118-
).Execute(ctx); err != nil {
119-
return err
120-
}
121-
122-
removeBackfillColumn := NewDropColumnAction(conn, table.Name, backfill.CNeedsBackfillColumn)
123-
err = removeBackfillColumn.Execute(ctx)
124-
125-
return err
107+
return []DBAction{
108+
NewDropColumnAction(conn, o.Table, TemporaryName(columnName)),
109+
NewDropFunctionAction(conn,
110+
backfill.TriggerFunctionName(o.Table, columnName),
111+
backfill.TriggerFunctionName(o.Table, TemporaryName(columnName))),
112+
NewDropColumnAction(conn, table.Name, backfill.CNeedsBackfillColumn),
113+
}, nil
126114
}
127115

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

pkg/migrations/op_drop_index.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ func (o *OpDropIndex) Complete(l Logger, conn db.DB, s *schema.Schema) ([]DBActi
3030
}, nil
3131
}
3232

33-
func (o *OpDropIndex) Rollback(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) error {
33+
func (o *OpDropIndex) Rollback(l Logger, conn db.DB, s *schema.Schema) ([]DBAction, error) {
3434
l.LogOperationRollback(o)
3535

3636
// no-op
37-
return nil
37+
return nil, nil
3838
}
3939

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

0 commit comments

Comments
 (0)