-
Notifications
You must be signed in to change notification settings - Fork 1
/
migrate.go
338 lines (279 loc) · 8.16 KB
/
migrate.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
package migration
import (
"context"
"database/sql"
"fmt"
"io/ioutil"
"log"
"os"
"time"
"github.com/go-sql-driver/mysql"
"github.com/pkg/errors"
)
type Logger interface {
Printf(format string, v ...interface{})
}
var Log Logger = log.New(os.Stdout, "", log.LstdFlags)
type Migration interface {
Version() int
Migrate(ctx context.Context, conn *sql.DB) error
}
type Definition struct {
ID int
Up string
}
func (s *Definition) Version() int {
return s.ID
}
func (s *Definition) Migrate(ctx context.Context, conn *sql.DB) error {
if _, err := conn.ExecContext(ctx, s.Up); err != nil {
return err
}
return nil
}
func MustMigrate(ctx context.Context, dsn string, migrations []Migration) {
if err := Migrate(ctx, dsn, migrations); err != nil {
panic(err)
}
}
func Migrate(ctx context.Context, dsn string, migrations []Migration) error {
if err := createDBIfNotExists(ctx, dsn); err != nil {
return err
}
conn, err := connect(dsn)
if err != nil {
return err
}
if err := createMigrationsTableIfNotExists(ctx, conn); err != nil {
return err
}
if err := runMigrations(ctx, conn, migrations); err != nil {
return err
}
return nil
}
func MustLoadSchema(ctx context.Context, dsn string, location string) {
if err := LoadSchema(ctx, dsn, location); err != nil {
panic(err)
}
}
func LoadSchema(ctx context.Context, dsn string, location string) error {
if err := createDBIfNotExists(ctx, dsn); err != nil {
return err
}
conn, err := connect(dsn)
if err != nil {
return err
}
if err := createMigrationsTableIfNotExists(ctx, conn); err != nil {
return err
}
// load the migrations table with necessary version information
if _, err := os.Stat(location + "/_migrations.sql"); os.IsNotExist(err) {
return nil
}
files, err := ioutil.ReadDir(location)
if err != nil {
return errors.Wrapf(err, "failed reading dir %q", location)
}
for _, file := range files {
name := file.Name()
if name[len(name)-4:] == ".sql" {
schema, err := ioutil.ReadFile(fmt.Sprintf("%s/%s", location, name))
if err != nil {
return errors.Wrapf(err, "unable to read %q", name)
}
if _, err := conn.ExecContext(ctx, string(schema)); err != nil {
return errors.Wrapf(err, "failed loading %q", name)
}
}
}
return nil
}
func MustDumpSchema(ctx context.Context, dsn string, location string) {
if err := DumpSchema(ctx, dsn, location); err != nil {
panic(err)
}
}
func DumpSchema(ctx context.Context, dsn string, location string) error {
conn, err := connect(dsn)
if err != nil {
return errors.Wrap(err, "unable to dump schema")
}
rows, err := conn.QueryContext(ctx, "SHOW TABLES")
if err != nil {
return errors.Wrap(err, "unable to show tables")
}
defer rows.Close()
tables := []string{}
for rows.Next() {
var tableName string
if err := rows.Scan(&tableName); err != nil {
return errors.Wrap(err, "unable to scan table name")
}
if tableName != "_migrations" {
tables = append(tables, tableName)
}
}
for _, table := range tables {
var tableName, createStatement string
err := conn.QueryRowContext(ctx, fmt.Sprintf("SHOW CREATE TABLE %s", table)).Scan(&tableName, &createStatement)
if err != nil {
return errors.Wrapf(err, "failed showing create statement for table %q", table)
}
err = ioutil.WriteFile(fmt.Sprintf("%s/%s.sql", location, table), []byte(createStatement), 0644)
if err != nil {
return errors.Wrapf(err, "failed writing out create table statement for table %q", table)
}
}
rowsVersions, err := conn.QueryContext(ctx, "SELECT id, created_at FROM _migrations ORDER BY id ASC")
if err != nil {
return errors.Wrap(err, "unable to select from _migrations table")
}
defer rowsVersions.Close()
versions := ""
for rowsVersions.Next() {
var id int
var createdAt time.Time
if err := rowsVersions.Scan(&id, &createdAt); err != nil {
return errors.Wrap(err, "unable to scan _migrations")
}
versions = versions + fmt.Sprintf("(%d, %q),\n", id, createdAt.Format("2006-01-02 15:04:05"))
}
if len(versions) > 0 {
migrations := fmt.Sprintf("INSERT INTO _migrations (id, created_at) VALUES\n%s", versions[:len(versions)-2])
if err := ioutil.WriteFile(fmt.Sprintf("%s/_migrations.sql", location), []byte(migrations), 0644); err != nil {
return errors.Wrap(err, "failed writing out create table statement for _migrations")
}
}
return nil
}
func runMigrations(ctx context.Context, conn *sql.DB, migrations []Migration) error {
if err := validateMigrations(migrations); err != nil {
return err
}
for _, migration := range migrations {
alreadyExecuted, err := migrationAlreadyExecuted(ctx, conn, migration.Version())
if err != nil {
return err
}
if !alreadyExecuted {
start := time.Now()
err := migration.Migrate(ctx, conn)
if err != nil {
return errors.Wrapf(err, "failed executing migration %d", migration.Version())
}
timeTaken := time.Now().Sub(start)
if err := markMigrationSuccessful(ctx, conn, migration.Version()); err != nil {
return err
}
log.Printf("executed migration %d in %s", migration.Version(), timeTaken)
} else {
log.Printf("skipping migration %d as it has already been executed", migration.Version())
}
}
return nil
}
func validateMigrations(migrations []Migration) error {
versions := make([]int, len(migrations))
for i, migration := range migrations {
for _, version := range versions {
if version == migration.Version() {
return errors.Errorf("duplicate migration version %d", version)
}
}
versions[i] = migration.Version()
}
return nil
}
func oneExists(ctx context.Context, conn *sql.DB, query string, args ...interface{}) (bool, error) {
var val interface{}
row := conn.QueryRowContext(ctx, query, args...)
err := row.Scan(&val)
switch {
case err == sql.ErrNoRows:
return false, nil
case err != nil:
return false, err
default:
return true, nil
}
}
func migrationAlreadyExecuted(ctx context.Context, conn *sql.DB, version int) (bool, error) {
return oneExists(ctx, conn, "SELECT id FROM _migrations WHERE id = ?", version)
}
func markMigrationSuccessful(ctx context.Context, conn *sql.DB, version int) error {
_, err := conn.ExecContext(ctx, "INSERT INTO _migrations (id, created_at) VALUES(?, ?)", version, time.Now())
return err
}
func createMigrationsTableIfNotExists(ctx context.Context, conn *sql.DB) error {
exists, err := migrationsTableExists(ctx, conn)
if err != nil {
return errors.Wrapf(err, "failed checking if table %q exists", "_migrations")
}
if !exists {
log.Printf("table _migrations doesn't exist")
_, err := conn.ExecContext(
ctx,
`CREATE TABLE _migrations (
id INT NOT NULL,
created_at DATETIME NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci`,
)
if err != nil {
return errors.Wrapf(err, "failed creating table %q", "_migrations")
}
log.Printf("created _migrations table")
}
return nil
}
func migrationsTableExists(ctx context.Context, conn *sql.DB) (bool, error) {
return oneExists(ctx, conn, `SHOW TABLES LIKE "_migrations"`)
}
func createDBIfNotExists(ctx context.Context, dsn string) error {
parsed, err := mysql.ParseDSN(dsn)
if err != nil {
return errors.Wrap(err, "unable to parse dsn")
}
dbname := parsed.DBName
if len(dbname) == 0 {
return errors.Errorf("dsn missing database name")
}
parsed.DBName = ""
conn, err := connect(parsed.FormatDSN())
if err != nil {
return err
}
defer conn.Close()
dbExists, err := dbExists(ctx, conn, dbname)
if err != nil {
return errors.Wrapf(err, "failed checking if db %q exists", dbname)
}
if !dbExists {
log.Printf("db %q doesn't exist", dbname)
if err := createDB(ctx, conn, dbname); err != nil {
return errors.Wrapf(err, "failed creating db %q", dbname)
}
log.Printf("created db %q", dbname)
}
return nil
}
func dbExists(ctx context.Context, conn *sql.DB, dbname string) (bool, error) {
return oneExists(ctx, conn, fmt.Sprintf(`SHOW DATABASES LIKE %q`, dbname))
}
func createDB(ctx context.Context, conn *sql.DB, dbname string) error {
_, err := conn.ExecContext(
ctx,
fmt.Sprintf(
`CREATE DATABASE %s
DEFAULT CHARACTER SET = utf8mb4
DEFAULT COLLATE = utf8mb4_unicode_520_ci`,
dbname,
),
)
if err != nil {
return err
}
return nil
}