-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun_isolated.go
281 lines (230 loc) · 5.72 KB
/
run_isolated.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package migrations
import (
"bytes"
"database/sql"
"errors"
"strings"
)
var ErrNoCommand = errors.New("no SQL command found")
var ErrNoState = errors.New("no SQL parser state")
// RequestChannel is the channel for submitting asynchronous migration requests. Asynchronous
// migrations are run in the background, and must complete in order, but they do not wait for
// synchronous migrations to complete.
type RequestChannel chan AsyncRequest
// ResultChannel is the channel on which asynchronous migrations return their results (did they
// succeed or fail). The MigrateAsync function returns this channel, so your application can
// listen for the background migrations to complete before reporting completion.
type ResultChannel chan AsyncResult
// AsyncRequest is submitted to the background asynchronous migration processor.
type AsyncRequest struct {
Migration string // Full path to the migration
Direction Direction // The direction to run
SQL SQL // The SQL to run (parsed from the migration)
Target int // The desired revision number to migration to
}
// AsyncResult is returned by asynchronous SQL migration commands on the Results channel
type AsyncResult struct {
Migration string // The migration filename
Err error // Is nil if the migration succeeded
Command SQL // The SQL command that failed if the migration failed; blank otherwise
}
// HandleAsync should be run in the background to listen for asynchronous migration requests. These
// requests are run in order, within a transaction, but the main synchronous migrations will not
// wait for these to complete before continuing.
func HandleAsync(db *sql.DB, requests RequestChannel, results ResultChannel) {
defer close(results)
for req := range requests {
Log.Infof("Running migration %s %s asynchronously", req.Migration, req.Direction)
if cmd, err := RunIsolated(db, req); err != nil {
results <- AsyncResult{req.Migration, err, cmd}
continue
}
results <- AsyncResult{Migration: req.Migration}
}
}
// RunIsolated breaks apart a SQL migration into separate commands and runs each in a single
// transaction. Helps asynchronous migrations return additional details about failures.
func RunIsolated(db *sql.DB, req AsyncRequest) (SQL, error) {
commands, err := ParseSQL(req.SQL)
if err != nil {
return "", err
}
tx, err := db.Begin()
if err != nil {
return "", err
}
for _, SQL := range commands {
_, err = tx.Exec(string(SQL))
if err != nil {
_ = tx.Rollback()
return SQL, err
}
}
if err = tx.Commit(); err != nil {
return "", err
}
return "", nil
}
// ParseSQL breaks the SQL document apart into individual commands, so we can submit them to the
// database one at a time.
func ParseSQL(doc SQL) ([]SQL, error) {
var cmds []SQL
parser := NewSQLParser(strings.TrimSpace(string(doc)))
for parser.Next() {
cmd, err := parser.Get()
if err != nil {
return nil, err
}
if cmd != "" {
cmds = append(cmds, cmd)
}
}
return cmds, nil
}
// SQLParser breaks apart a document of SQL commands into their individual commands.
type SQLParser struct {
sql string
idx int
state []parserState
cmd []byte
err error
}
// NewSQLParser creats a new SQL parser.
func NewSQLParser(sql string) *SQLParser {
return &SQLParser{
sql: sql,
}
}
// Next fetches the SQL command from from the document.
func (p *SQLParser) Next() bool {
if p.idx == len(p.sql) {
return false
}
p.cmd = p.cmd[:0]
p.pushState(start)
for {
if p.idx == len(p.sql) || len(p.state) == 0 {
return true
}
if err := p.fwd(); err != nil {
p.err = err
return true
}
}
}
// Get the SQL command parsed by Next(). Note that the semicolon will be stripped off.
func (p *SQLParser) Get() (SQL, error) {
if p.cmd == nil {
return "", ErrNoCommand
} else if p.err != nil {
return "", p.err
}
return SQL(bytes.TrimSpace(p.cmd)), nil
}
func (p *SQLParser) pushState(state parserState) {
p.state = append(p.state, state)
}
func (p *SQLParser) popState() parserState {
if len(p.state) == 0 {
return nil
}
state := p.state[len(p.state)-1]
p.state = p.state[:len(p.state)-1]
return state
}
func (p *SQLParser) fwd() error {
if len(p.state) == 0 {
return ErrNoState // shouldn't happen, but just in case
}
state := p.state[len(p.state)-1]
return state(p)
}
// Look ahead one character.
func (p *SQLParser) peek() byte {
if p.idx == len(p.sql) {
return 0
}
return p.sql[p.idx]
}
// Look ahead N characters.
func (p *SQLParser) peekN(n int) string {
if p.idx+n >= len(p.sql) {
n = len(p.sql) - p.idx
}
if n <= 0 {
return ""
}
return p.sql[p.idx : p.idx+n]
}
// Get the next character and advance the index.
func (p *SQLParser) pop() byte {
ch := p.sql[p.idx]
p.cmd = append(p.cmd, ch)
p.idx++
return ch
}
// parserState is used to track the state of quoted strings in the SQL commands.
type parserState func(*SQLParser) error
func start(p *SQLParser) error {
ch := p.peek()
switch ch {
case '\'':
p.pop()
p.pushState(single)
case '"':
p.pop()
p.pushState(double)
case '-':
// Handle comments
if p.peek() == '-' {
for p.idx < len(p.sql) {
p.idx = p.idx + 1
if p.peek() == '\n' {
break
}
}
break
}
p.pop()
case ';':
p.idx = p.idx + 1
p.popState()
default:
p.pop()
}
return nil
}
func single(p *SQLParser) error {
ch := p.pop()
switch ch {
case '\\':
if p.peek() == '\'' {
p.pop()
}
case '\'':
// Support ''' for escaping a single quote
if p.peekN(2) == "''" {
p.pop()
p.pop()
return nil
}
p.popState()
default:
// do nothing...
}
return nil
}
func double(p *SQLParser) error {
ch := p.pop()
switch ch {
case '\\':
if p.peek() == '"' {
p.pop()
}
case '"':
p.popState()
default:
// do nothing...
}
return nil
}