-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrate.go
52 lines (43 loc) · 1.1 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
package testdb
import (
"errors"
"fmt"
"os"
"os/exec"
"path"
"strings"
"testing"
)
// CliMigrator implements a migration strategy that uses the command line (linux only).
// Assumes migrate is installed: https://github.com/golang-migrate/migrate#cli-usage
func CliMigrator(t testing.TB, dir string) Migrator {
_, err := os.ReadDir(dir)
must(t, err)
return &cliMigrator{dir: dir}
}
// cliMigrator is created by CliMigrator.
type cliMigrator struct {
dir string
}
func (p *cliMigrator) Hash(t testing.TB) string {
glob := fmt.Sprintf(path.Join(p.dir, "/*"))
cmd := exec.Command("bash", "-c", fmt.Sprintf("md5sum %s | md5sum | awk '{ print $1 }'", glob))
b, err := cmd.Output()
must(t, err)
return strings.TrimSpace(string(b))
}
func (p *cliMigrator) Migrate(t testing.TB, dsn string) {
cmd := exec.Command(
"migrate",
"-database",
dsn,
"-path",
p.dir, // Hardcoded migration path.
"up",
)
_, err := cmd.Output()
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
must(t, fmt.Errorf("failed to migrate test DB (exit code: %d): %s", exitErr.ExitCode(), exitErr.Stderr))
}
}