-
Notifications
You must be signed in to change notification settings - Fork 3
/
mysql.go
392 lines (352 loc) · 9.72 KB
/
mysql.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
// Package mysql implements the Driver interface.
package mysql
import (
"bytes"
"context"
"crypto/tls"
"crypto/x509"
"database/sql"
"errors"
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/db-journey/migrate/v2/direction"
"github.com/db-journey/migrate/v2/driver"
"github.com/db-journey/migrate/v2/file"
"github.com/go-sql-driver/mysql"
)
const versionsTableName = "schema_migrations"
// directives
const (
directiveNotx = "NOTX"
directiveTxbegin = "TXBEGIN"
directiveTxend = "TXEND"
directiveNoop = ""
)
var fileTemplate = []byte(`
-- Each SQL statement MUST end with semicolon (;) FOLLOWED BY NEWLINE !
-- Whole migration will be executed inside transaction by default.
-- Place SQL between "-- TXBEGIN" and "-- TXEND" comments for custom transaction:
-- - you CAN have multiple separate transactions in single migration
-- - any SQL not wrapped into TXBEGIN - TXEND will be executed without transaction.
-- Add "-- NOTX" comment above all SQL to disable default migration. NOTE:
-- it's redundant when TXBEGIN/TXEND is used.
`)
func init() {
driver.Register("mysql", "sql", fileTemplate, Open)
}
// Driver for MySQL
type Driver struct {
db *sql.DB
versionConn *sql.Conn
}
// Open driver
func Open(url string) (driver.Driver, error) {
drv := &Driver{}
urlWithoutScheme := strings.SplitN(url, "mysql://", 2)
if len(urlWithoutScheme) != 2 {
return nil, errors.New("invalid mysql:// scheme")
}
// check if env vars vor mysql ssl connection are set and if yes use them
// XXX: reading env vars in a library is not good, such stuff should be passed
// from user (CLI code).
if os.Getenv("MYSQL_SERVER_CA") != "" && os.Getenv("MYSQL_CLIENT_KEY") != "" && os.Getenv("MYSQL_CLIENT_CERT") != "" {
rootCertPool := x509.NewCertPool()
pem, err := ioutil.ReadFile(os.Getenv("MYSQL_SERVER_CA"))
if err != nil {
return nil, err
}
if ok := rootCertPool.AppendCertsFromPEM(pem); !ok {
return nil, errors.New("Failed to append PEM")
}
clientCert := make([]tls.Certificate, 0, 1)
certs, err := tls.LoadX509KeyPair(os.Getenv("MYSQL_CLIENT_CERT"), os.Getenv("MYSQL_CLIENT_KEY"))
if err != nil {
return nil, err
}
clientCert = append(clientCert, certs)
mysql.RegisterTLSConfig("custom", &tls.Config{
RootCAs: rootCertPool,
Certificates: clientCert,
InsecureSkipVerify: true,
})
urlWithoutScheme[1] += "&tls=custom"
}
db, err := sql.Open("mysql", urlWithoutScheme[1])
if err != nil {
return nil, err
}
if err := db.Ping(); err != nil {
return nil, err
}
drv.db = db
return drv, drv.ensureVersionTableExists()
}
// Close db connection
func (drv *Driver) Close() error {
if drv.versionConn != nil {
drv.versionConn.Close() // error is no big deal here.
}
return drv.db.Close()
}
// Execute sql
func (drv *Driver) Execute(sql string) error {
_, err := drv.db.Exec(sql)
return err
}
// Migrate runs migration.
// It locks schema_migrations table, so concurrent execution is safe.
func (drv *Driver) Migrate(f file.File) error {
if drv.versionConn == nil {
return errors.New("migrate must call Lock before Migrate")
}
if err := f.ReadContent(); err != nil {
return err
}
migration, err := parseMigration(f.Content)
if err != nil {
return fmt.Errorf("failed to parse migration: %s", err)
}
err = migration.exec(drv.db)
if err != nil {
return err
}
versionUpdSQL := "INSERT INTO " + versionsTableName + " (version) VALUES (?)"
if f.Direction == direction.Down {
versionUpdSQL = "DELETE FROM " + versionsTableName + " WHERE version = ?"
}
if _, err = drv.versionConn.ExecContext(context.TODO(), versionUpdSQL, f.Version); err != nil {
err = fmt.Errorf("migration %d was successfully applied, but failed to update schema_migrations table: %s", f.Version, err)
}
return err
}
// Version returns the current migration version.
func (drv *Driver) Version() (file.Version, error) {
var version file.Version
err := drv.versionConn.QueryRowContext(context.TODO(), "SELECT version FROM "+versionsTableName+" ORDER BY version DESC").Scan(&version)
switch {
case err == sql.ErrNoRows:
return 0, nil
case err != nil:
return 0, err
default:
return version, nil
}
}
// Versions returns the list of applied migrations.
func (drv *Driver) Versions() (file.Versions, error) {
versions := file.Versions{}
err := drv.initVersionConn()
if err != nil {
return nil, err
}
rows, err := drv.versionConn.QueryContext(context.TODO(), "SELECT version FROM "+versionsTableName+" ORDER BY version DESC")
if err != nil {
return versions, err
}
defer rows.Close()
for rows.Next() {
var version file.Version
err := rows.Scan(&version)
if err != nil {
return versions, err
}
versions = append(versions, version)
}
err = rows.Err()
return versions, err
}
// Lock schema_migrations table
func (drv *Driver) Lock() error {
err := drv.initVersionConn()
if err != nil {
return err
}
_, err = drv.versionConn.ExecContext(context.TODO(), "LOCK TABLES "+versionsTableName+" WRITE")
if err != nil {
return fmt.Errorf("failed to lock %s table: %v", versionsTableName, err)
}
return nil
}
// Unlock schema_migrations table
func (drv *Driver) Unlock() error {
if drv.versionConn == nil {
return errors.New("not locked")
}
_, err := drv.versionConn.ExecContext(context.TODO(), "UNLOCK TABLES")
if err != nil {
return fmt.Errorf("failed to unlock %s table: %v", versionsTableName, err)
}
drv.versionConn.Close() // not a big deal if it fails to return connection to the pool
drv.versionConn = nil
return err
}
func (drv *Driver) initVersionConn() (err error) {
if drv.versionConn == nil {
drv.versionConn, err = drv.db.Conn(context.TODO())
}
return err
}
func (drv *Driver) ensureVersionTableExists() error {
_, err := drv.db.Exec("CREATE TABLE IF NOT EXISTS " + versionsTableName + " (version bigint not null primary key);")
if err != nil {
return err
}
r := drv.db.QueryRow("SELECT data_type FROM information_schema.columns where table_name = ? and column_name = 'version'", versionsTableName)
dataType := ""
if err = r.Scan(&dataType); err != nil {
return err
}
if dataType != "int" {
return nil
}
_, err = drv.db.Exec("ALTER TABLE " + versionsTableName + " MODIFY version bigint")
return err
}
func parseDirective(b []byte) string {
b = bytes.TrimSpace(b)
if !bytes.HasPrefix(b, []byte("-- ")) {
return directiveNoop
}
return string(b[3 : len(b)-1])
}
type migrationSegment struct {
statements []string
offsets []int // line offset from beginning of file
tx bool
txbegin, txend int // line numbers
}
type migration struct {
// noTx determines if default transaction
// should be disabled
noTx bool
segments []migrationSegment
}
// parseMigration splits given SQL source into list of sql statements/transactions
// NOTE wrapping whole migration SQL into single transaction sucks,
// b/c some stuff like CREATE TABLE commits implicitly.
// Proper formatting is documented.
func parseMigration(b []byte) (*migration, error) {
m := &migration{}
lines := bytes.Split(b, []byte("\n"))
for i := 0; i < len(lines); i++ {
if len(bytes.TrimSpace(lines[i])) == 0 {
continue
}
i = scrollEmpty(lines, i)
if i < 0 {
break
}
stmt := migrationSegment{}
if !bytes.HasPrefix(bytes.TrimSpace(lines[i]), []byte("-- ")) {
i = writeStmt(&stmt, lines, i)
m.segments = append(m.segments, stmt)
continue
}
i = scrollEmpty(lines, i)
if i < 0 {
break
}
switch parseDirective(lines[i]) {
case directiveNotx:
m.noTx = true
break
case directiveTxbegin:
m.noTx = true
stmt.tx = true
stmt.txbegin = i + 1
for ; i < len(lines); i++ {
directive := parseDirective(lines[i])
if directive != "" && directive != directiveTxend {
return nil, fmt.Errorf("expected %q, got %q at line %d", directiveTxend, directive, i+1)
}
i = writeStmt(&stmt, lines, i)
}
stmt.txend = i + 1
m.segments = append(m.segments, stmt)
break
case directiveNoop:
break
}
}
return m, nil
}
func (m migration) exec(db *sql.DB) (err error) {
var tx *sql.Tx
defer func() {
if err != nil && tx != nil {
tx.Rollback()
}
}()
if !m.noTx {
tx, err = db.Begin()
if err != nil {
return err
}
for _, seg := range m.segments {
for i, stmt := range seg.statements {
_, err = tx.Exec(stmt)
if err != nil {
return stmtExecErr(err, stmt, seg.offsets[i])
}
}
}
return tx.Commit()
}
for _, seg := range m.segments {
if seg.tx {
tx, err = db.Begin()
if err != nil {
return err
}
for i, stmt := range seg.statements {
_, err = tx.Exec(stmt)
if err != nil {
return stmtExecErr(err, stmt, seg.offsets[i])
}
}
return stmtCommitErr(tx.Commit(), seg)
}
}
return nil
}
func stmtExecErr(err error, stmt string, stmtOffset int) error {
if err == nil {
return nil
}
return fmt.Errorf("Failed to exec SQL statement at line %d:\n%s\nError:%s", stmtOffset+1, stmt, err)
}
func stmtCommitErr(err error, s migrationSegment) error {
if err == nil {
return nil
}
return fmt.Errorf("Failed to commit lines %d-%d: %s", s.txbegin, s.txend, err)
}
// writeStmt is a DRYer for migration.parse
// returns last line index of statement.
func writeStmt(stmt *migrationSegment, lines [][]byte, i int) int {
i = scrollEmpty(lines, i)
if i < 0 {
return len(lines) - 1
}
stmt.offsets = append(stmt.offsets, i)
buf := &bytes.Buffer{}
for ; i < len(lines); i++ {
fmt.Fprintf(buf, "%s\n", lines[i])
if bytes.HasSuffix(lines[i], []byte(";")) {
break
}
}
stmt.statements = append(stmt.statements, buf.String())
return i
}
// scrollEmpty returns next non-empy line index.
func scrollEmpty(lines [][]byte, i int) int {
for ; i < len(lines) && len(bytes.TrimSpace(lines[i])) == 0; i++ {
}
if i == len(lines)-1 && len(bytes.TrimSpace(lines[i])) == 0 {
return -1
}
return i
}