-
Notifications
You must be signed in to change notification settings - Fork 47
/
group.go
75 lines (64 loc) · 1.79 KB
/
group.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
Use of this source code is governed by a MIT license that can be found in the LICENSE file.
*/
package migration
import (
"context"
"database/sql"
"github.com/blend/go-sdk/db"
"github.com/blend/go-sdk/ex"
)
// NewGroup creates a new Group from a given list of actionable.
func NewGroup(options ...GroupOption) *Group {
g := Group{}
for _, o := range options {
o(&g)
}
return &g
}
// NewGroupWithAction returns a new group with a single action.
func NewGroupWithAction(guard GuardFunc, action Action, options ...GroupOption) *Group {
return NewGroup(
append([]GroupOption{OptGroupActions(NewStep(guard, action))}, options...)...,
)
}
// Group is an series of migration actions.
// It uses normally transactions to apply these actions as an atomic unit, but this transaction can be bypassed by
// setting the SkipTransaction flag to true. This allows the use of CONCURRENT index creation and other operations that
// postgres will not allow within a transaction.
type Group struct {
Actions []Action
Tx *sql.Tx
SkipTransaction bool
}
// Action runs the groups actions within a transaction.
func (ga *Group) Action(ctx context.Context, c *db.Connection) (err error) {
var tx *sql.Tx
if ga.Tx != nil { // if we have a transaction provided to us
tx = ga.Tx
} else if !ga.SkipTransaction { // if we aren't told to skip transactions
tx, err = c.Begin()
if err != nil {
return
}
defer func() {
if err != nil {
if txErr := tx.Rollback(); txErr != nil {
err = ex.Nest(err, txErr)
}
} else {
if txErr := tx.Commit(); txErr != nil {
err = ex.Nest(err, txErr)
}
}
}()
}
for _, a := range ga.Actions {
err = a.Action(ctx, c, tx)
if err != nil {
return
}
}
return
}