~egtann/migrate

ref: 4a195dd1a1241ba970de798b06759e3c70749a1a migrate/migrate.go -rw-r--r-- 11.8 KiB
4a195dd1 — Evan Tann fix postgres metacheckpoint table 10 months ago
                                                                                
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
package migrate

import (
	"bytes"
	"crypto/md5"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"path/filepath"
	"regexp"
	"sort"
	"strconv"
	"strings"

	"github.com/pkg/errors"
)

// version of the migrate tool's database schema.
const version = 1

var spaces = regexp.MustCompile(`\s+`)

type Migrate struct {
	Migrations []Migration
	Files      []*file

	db  Store
	log Logger
	idx int
}

type file struct {
	Info     os.FileInfo
	fullpath string
}

type Migration struct {
	Filename string
	Checksum string
	Content  string
	fullpath string
}

var regexNum = regexp.MustCompile(`^\d+`)

type DBType string

const (
	DBTypeMySQL    DBType = "mysql"
	DBTypeMariaDB  DBType = "mariadb"
	DBTypePostgres DBType = "postgres"
	DBTypeSQLite   DBType = "sqlite"
)

func New(
	db Store,
	log Logger,
	dbt DBType,
	dir, skip string,
) (*Migrate, error) {
	m := &Migrate{db: db, log: log}

	// Get files in migration dir and sort them
	var err error
	m.Files, err = readDir(dir, dbt)
	if err != nil {
		return nil, errors.Wrap(err, "get migrations")
	}
	if err = sortFiles(m.Files); err != nil {
		return nil, errors.Wrap(err, "sort")
	}

	// Create meta tables if we need to, so we can store the migration
	// state in the db itself
	if err = db.CreateMetaIfNotExists(); err != nil {
		return nil, errors.Wrap(err, "create meta table")
	}
	if err = db.CreateMetaCheckpointsIfNotExists(); err != nil {
		return nil, errors.Wrap(err, "create meta checkpoints table")
	}
	curVersion, err := db.CreateMetaVersionIfNotExists(version)
	if err != nil {
		return nil, errors.Wrap(err, "create meta version table")
	}

	// Migrate the database schema to match the tool's expectations
	// automatically
	if curVersion > version {
		return nil, errors.New("must upgrade migrate: go get -u egt.run/migrate")
	}
	if curVersion < 1 {
		tmpMigrations, err := migrationsFromFiles(m)
		if err != nil {
			return nil, errors.Wrap(err, "migrations from files")
		}
		if err = db.UpgradeToV1(tmpMigrations); err != nil {
			return nil, errors.Wrap(err, "upgrade to v1")
		}
		curVersion = 1
	}

	// If skip, then we record the migrations but do not perform them. This
	// enables you to start using this package on an existing database
	if skip != "" {
		m.idx, err = m.skip(skip)
		if err != nil {
			return nil, errors.Wrap(err, "skip ahead")
		}
		m.log.Println("skipped ahead")
	}

	// Get all migrations
	m.Migrations, err = db.GetMigrations()
	if err != nil {
		return nil, errors.Wrap(err, "get migrations")
	}

	// Fill in migration fullpath field based on the db type.
	overrides, err := getOverrideSet(dir, dbt)
	if err != nil {
		return nil, fmt.Errorf("get override set: %w", err)
	}
	for i, mg := range m.Migrations {
		override, exist := overrides[mg.Filename]
		if exist {
			m.Migrations[i].fullpath = override.fullpath
		} else {
			m.Migrations[i].fullpath = filepath.Join(dir, mg.Filename)
		}
	}
	if err = m.validHistory(); err != nil {
		return nil, err
	}
	return m, nil
}

// Migrate all files in the directory. This function reports whether any
// migration took place.
func (m *Migrate) Migrate() (bool, error) {
	var migrated bool
	for i := len(m.Migrations); i < len(m.Files); i++ {
		fi := m.Files[i]
		if err := m.migrateFile(fi); err != nil {
			return false, errors.Wrap(err, "migrate file")
		}
		m.log.Println("migrated", fi.Info.Name())
		migrated = true
	}
	return migrated, nil
}

func (m *Migrate) validHistory() error {
	for i := len(m.Files); i < len(m.Migrations); i++ {
		m.log.Printf("missing already-run migration %q\n", m.Migrations[i])
	}
	if len(m.Files) < len(m.Migrations) {
		return errors.New("cannot continue with missing migrations")
	}
	for i := m.idx; i < len(m.Migrations); i++ {
		mg := m.Migrations[i]
		if mg.Filename != m.Files[i].Info.Name() {
			m.log.Printf("\n%s was added to history before %s.\n",
				m.Files[i].Info.Name(), mg.Filename)
			return errors.New("failed to migrate. migrations must be appended")
		}
		if err := m.checkHash(mg); err != nil {
			return errors.Wrap(err, "check hash")
		}
	}
	return nil
}

func (m *Migrate) checkHash(mg Migration) error {
	fi, err := os.Open(mg.fullpath)
	if err != nil {
		return err
	}
	defer fi.Close()
	_, check, err := computeChecksum(fi)
	if err != nil {
		return err
	}
	if check != mg.Checksum {
		m.log.Println("comparing", check, mg.Checksum)
		return fmt.Errorf("checksum does not match %s. has the file changed?",
			mg.Filename)
	}
	return nil
}

func (m *Migrate) migrateFile(fi *file) error {
	byt, err := ioutil.ReadFile(fi.fullpath)
	if err != nil {
		return err
	}

	// Split commands and remove comments at the start of lines
	cmds := strings.Split(string(byt), ";")
	filteredCmds := []string{}
	for _, cmd := range cmds {
		cmd = strings.TrimSpace(cmd)
		if len(cmd) > 0 && !strings.HasPrefix(cmd, "--") {
			filteredCmds = append(filteredCmds, cmd)
		}
	}

	// Ensure that commands are present
	if len(filteredCmds) == 0 {
		return fmt.Errorf("no sql statements in file: %s", fi.Info.Name())
	}

	// Get our checkpoints, if any
	checkpoints, err := m.db.GetMetaCheckpoints(fi.Info.Name())
	if err != nil {
		return errors.Wrap(err, "get checkpoints")
	}
	if len(checkpoints) > 0 {
		m.log.Printf("found %d checkpoints\n", len(checkpoints))
	}

	// Ensure commands weren't deleted from the file after we migrated them
	if len(checkpoints) >= len(filteredCmds) {
		return fmt.Errorf("len(checkpoints) %d >= len(cmds) %d",
			len(checkpoints), len(filteredCmds))
	}

	for i, cmd := range filteredCmds {
		// Confirm the file up to our checkpoint has not changed
		if i < len(checkpoints) {
			r := strings.NewReader(cmd)
			_, checksum, err := computeChecksum(r)
			if err != nil {
				return errors.Wrap(err, "compute checkpoint checksum")
			}
			if checksum != checkpoints[i] {
				return fmt.Errorf(
					"checksum does not equal checkpoint. has %s (cmd %d) changed?",
					fi.Info.Name(), i)
			}
			continue
		}

		// Print the commands we're executing to give progress updates
		// on large migrations
		shortCmd := cmd
		shortCmd = strings.ReplaceAll(shortCmd, "\n", " ")
		shortCmd = spaces.ReplaceAllString(shortCmd, " ")
		if len(shortCmd) >= 78 {
			shortCmd = shortCmd[:74] + "..."
		}
		m.log.Println(">", shortCmd)

		// Execute non-checkpointed commands one by one
		_, err := m.db.Exec(cmd)
		if err != nil {
			m.log.Println("failed on", cmd)
			return fmt.Errorf("%s: %s", fi.Info.Name(), err)
		}

		// Save a checkpoint
		_, checksum, err := computeChecksum(strings.NewReader(cmd))
		if err != nil {
			return errors.Wrap(err, "compute checksum")
		}
		err = m.db.InsertMetaCheckpoint(fi.Info.Name(), cmd, checksum, i)
		if err != nil {
			return errors.Wrap(err, "insert checkpoint")
		}
	}

	// We've successfully finished migrating the file, so we delete the
	// temporary progress in metacheckpoints and save the migration
	if err = m.db.DeleteMetaCheckpoints(); err != nil {
		return errors.Wrap(err, "delete checkpoints")
	}

	_, checksum, err := computeChecksum(bytes.NewReader(byt))
	if err != nil {
		return errors.Wrap(err, "compute file checksum")
	}
	err = m.db.InsertMigration(fi.Info.Name(), string(byt), checksum)
	if err != nil {
		return errors.Wrap(err, "insert migration")
	}
	return nil
}

func (m *Migrate) skip(toFile string) (int, error) {
	// Get just the filename if skip is a directory
	_, toFile = filepath.Split(toFile)

	// Ensure the file exists
	index := -1
	for i, fi := range m.Files {
		if fi.Info.Name() == toFile {
			index = i
			break
		}
	}
	if index == -1 {
		return 0, fmt.Errorf("%s does not exist", toFile)
	}
	for i := 0; i <= index; i++ {
		fi, err := os.Open(m.Files[i].fullpath)
		if err != nil {
			return -1, err
		}
		content, checksum, err := computeChecksum(fi)
		if err != nil {
			fi.Close()
			return -1, err
		}
		name := m.Files[i].Info.Name()
		err = m.db.UpsertMigration(name, content, checksum)
		if err != nil {
			fi.Close()
			return -1, err
		}
		if err = fi.Close(); err != nil {
			return -1, err
		}
	}
	return index, nil
}

func computeChecksum(r io.Reader) (content string, checksum string, err error) {
	h := md5.New()
	byt, err := ioutil.ReadAll(r)
	if err != nil {
		return "", "", errors.Wrap(err, "read all")
	}
	if _, err := io.Copy(h, bytes.NewReader(byt)); err != nil {
		return "", "", err
	}
	return string(byt), fmt.Sprintf("%x", h.Sum(nil)), nil
}

// readDir collects file infos from the migration directory.
func readDir(dir string, dbt DBType) ([]*file, error) {
	files := []*file{}
	tmp, err := ioutil.ReadDir(dir)
	if err != nil {
		return nil, errors.Wrap(err, "read dir")
	}

	// Allow for DB-specific workarounds. For instance, if MySQL and
	// MariaDB are subtly incompatible (and they are, as they name
	// CONSTRAINTS in different ways), then it's possible later migrations
	// will work on one database but not another, even though they should
	// be compatible. There is no easy workaround, especially when you're
	// on an OS with access to one database but not the other.
	//
	// To ease this, we crawl through secondary directories specific to the
	// name of the DB used. If "migrate -t maria-db" then we'll look for
	// the `maria-db` folder and prefer identical migration filenames in
	// that folder over the other one.
	for _, fi := range tmp {
		fullpath := filepath.Join(dir, fi.Name())

		// Skip directories and hidden files
		if fi.IsDir() || strings.HasPrefix(fi.Name(), ".") {
			continue
		}
		// Skip any non-sql files
		if filepath.Ext(fi.Name()) != ".sql" {
			continue
		}
		files = append(files, &file{Info: fi, fullpath: fullpath})
	}
	if len(files) == 0 {
		return nil, errors.New("no sql migration files found (might be the wrong -dir)")
	}

	// Prioritize our specific database over the set in the main migration
	// directory.
	overrideSet, err := getOverrideSet(dir, dbt)
	if err != nil {
		return nil, fmt.Errorf("get override set: %w", err)
	}
	for i, fi := range files {
		if override, exist := overrideSet[fi.Info.Name()]; exist {
			files[i] = override
			fmt.Println("OVERRIDING", override.Info.Name())
		}
	}
	return files, nil
}

func getOverrideSet(dir string, dbt DBType) (map[string]*file, error) {
	tmp, err := ioutil.ReadDir(dir)
	if err != nil {
		return nil, errors.Wrap(err, "read dir")
	}
	overrides := []*file{}
	for _, fi := range tmp {
		fullpath := filepath.Join(dir, fi.Name())
		if !fi.IsDir() || fi.Name() != string(dbt) {
			continue
		}

		// The empty DBType prevents recursive descent into structures
		// like ./mariadb/mariadb/mariadb/...
		overrides, err = readDir(fullpath, DBType(""))
		if err != nil {
			return nil, fmt.Errorf("read dir %s: %w",
				fi.Name(), err)
		}
	}
	overrideSet := make(map[string]*file, len(overrides))
	for _, o := range overrides {
		overrideSet[o.Info.Name()] = o
	}
	return overrideSet, nil
}

// sortFiles by name, ensuring that something like 1.sql, 2.sql, 10.sql is
// ordered correctly.
func sortFiles(files []*file) error {
	var nameErr error
	sort.Slice(files, func(i, j int) bool {
		if nameErr != nil {
			return false
		}
		fiName1 := regexNum.FindString(files[i].Info.Name())
		fiName2 := regexNum.FindString(files[j].Info.Name())
		fiNum1, err := strconv.ParseUint(fiName1, 10, 64)
		if err != nil {
			nameErr = errors.Wrapf(err, "parse uint in file %s",
				files[i].Info.Name())
			return false
		}
		fiNum2, err := strconv.ParseUint(fiName2, 10, 64)
		if err != nil {
			nameErr = errors.Wrapf(err, "parse uint in file %s",
				files[i].Info.Name())
			return false
		}
		if fiNum1 == fiNum2 {
			nameErr = fmt.Errorf("cannot have duplicate timestamp: %d", fiNum1)
			return false
		}
		return fiNum1 < fiNum2
	})
	return nameErr
}

func migrationsFromFiles(m *Migrate) ([]Migration, error) {
	ms := make([]Migration, len(m.Files))
	for i, fi := range m.Files {
		fmt.Println("FULLPATH", fi.fullpath)
		byt, err := ioutil.ReadFile(fi.fullpath)
		if err != nil {
			return nil, errors.Wrap(err, "read file")
		}
		ms[i] = Migration{
			Filename: fi.Info.Name(),
			Content:  string(byt),
			fullpath: fi.fullpath,
		}
	}
	return ms, nil
}