Skip to content

Commit 7e2618d

Browse files
committed
Return list of DBAction from Complete functions
1 parent d2a1212 commit 7e2618d

25 files changed

+161
-276
lines changed

pkg/migrations/migrations.go

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

2929
// Rollback will revert the changes made by Start. It is not possible to
3030
// 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, 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, 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 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, TriggerFunctionName(o.Table, o.Column), 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+
TriggerFunctionName(o.Table, o.Column),
124+
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
@@ -30,10 +30,10 @@ func (o *OpChangeType) Start(ctx context.Context, l Logger, conn db.DB, latestSc
3030
return table, nil
3131
}
3232

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

36-
return nil
36+
return []DBAction{}, nil
3737
}
3838

3939
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
@@ -99,83 +99,73 @@ func (o *OpCreateConstraint) Start(ctx context.Context, l Logger, conn db.DB, la
9999
return table, nil
100100
}
101101

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

105+
dbActions := make([]DBAction, 0)
105106
switch o.Type {
106107
case OpCreateConstraintTypeUnique:
107108
uniqueOp := &OpSetUnique{
108109
Table: o.Table,
109110
Name: o.Name,
110111
}
111-
err := uniqueOp.Complete(ctx, l, conn, s)
112+
actions, err := uniqueOp.Complete(l, conn, s)
112113
if err != nil {
113-
return err
114+
return []DBAction{}, err
114115
}
116+
dbActions = append(dbActions, actions...)
115117
case OpCreateConstraintTypeCheck:
116118
checkOp := &OpSetCheckConstraint{
117119
Table: o.Table,
118120
Check: CheckConstraint{
119121
Name: o.Name,
120122
},
121123
}
122-
err := checkOp.Complete(ctx, l, conn, s)
124+
actions, err := checkOp.Complete(l, conn, s)
123125
if err != nil {
124-
return err
126+
return []DBAction{}, err
125127
}
128+
dbActions = append(dbActions, actions...)
126129
case OpCreateConstraintTypeForeignKey:
127130
fkOp := &OpSetForeignKey{
128131
Table: o.Table,
129132
References: ForeignKeyReference{
130133
Name: o.Name,
131134
},
132135
}
133-
err := fkOp.Complete(ctx, l, conn, s)
136+
actions, err := fkOp.Complete(l, conn, s)
134137
if err != nil {
135-
return err
138+
return []DBAction{}, err
136139
}
140+
dbActions = append(dbActions, actions...)
137141
case OpCreateConstraintTypePrimaryKey:
138-
err := NewAddPrimaryKeyAction(conn, o.Table, o.Name).Execute(ctx)
139-
if err != nil {
140-
return err
141-
}
142+
dbActions = append(dbActions, NewAddPrimaryKeyAction(conn, o.Table, o.Name))
142143
}
143144

144145
for _, col := range o.Columns {
145-
if err := NewAlterSequenceOwnerAction(conn, o.Table, col, TemporaryName(col)).Execute(ctx); err != nil {
146-
return err
147-
}
146+
dbActions = append(dbActions, NewAlterSequenceOwnerAction(conn, o.Table, col, TemporaryName(col)))
148147
}
149148

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

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

175-
removeBackfillColumn := NewDropColumnAction(conn, o.Table, backfill.CNeedsBackfillColumn)
176-
err = removeBackfillColumn.Execute(ctx)
177-
178-
return err
168+
return dbActions, nil
179169
}
180170

181171
func (o *OpCreateConstraint) Rollback(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) error {
@@ -192,7 +182,7 @@ func (o *OpCreateConstraint) Rollback(ctx context.Context, l Logger, conn db.DB,
192182
return err
193183
}
194184

195-
if err := o.removeTriggers(ctx, conn); err != nil {
185+
if err := o.removeTriggers(conn).Execute(ctx); err != nil {
196186
return err
197187
}
198188

@@ -202,13 +192,13 @@ func (o *OpCreateConstraint) Rollback(ctx context.Context, l Logger, conn db.DB,
202192
return err
203193
}
204194

205-
func (o *OpCreateConstraint) removeTriggers(ctx context.Context, conn db.DB) error {
195+
func (o *OpCreateConstraint) removeTriggers(conn db.DB) DBAction {
206196
dropFuncs := make([]string, 0, len(o.Columns)*2)
207197
for _, column := range o.Columns {
208198
dropFuncs = append(dropFuncs, TriggerFunctionName(o.Table, column))
209199
dropFuncs = append(dropFuncs, TriggerFunctionName(o.Table, TemporaryName(column)))
210200
}
211-
return NewDropFunctionAction(conn, dropFuncs...).Execute(ctx)
201+
return NewDropFunctionAction(conn, dropFuncs...)
212202
}
213203

214204
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
@@ -78,11 +78,11 @@ func (o *OpCreateIndex) Start(ctx context.Context, l Logger, conn db.DB, latestS
7878
return nil, err
7979
}
8080

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

8484
// No-op
85-
return nil
85+
return []DBAction{}, nil
8686
}
8787

8888
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
@@ -60,11 +60,11 @@ func (o *OpCreateTable) Start(ctx context.Context, l Logger, conn db.DB, latestS
6060
return nil, nil
6161
}
6262

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

6666
// No-op
67-
return nil
67+
return []DBAction{}, nil
6868
}
6969

7070
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, 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, 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)