forked from peterldowns/pgtestdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdir.go
78 lines (70 loc) · 1.71 KB
/
dir.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
package atlasmigrator
import (
"context"
"database/sql"
"github.com/peterldowns/pgtestdb"
"github.com/peterldowns/pgtestdb/migrators/common"
)
// NewDirMigrator returns a [DirMigrator], which is a pgtestdb.Migrator that
// uses the `atlas` CLI tool to perform migrations.
//
// atlas migrate apply --url $DB --dir file://$migrationsDirPath
func NewDirMigrator(
migrationsDirPath string,
) *DirMigrator {
return &DirMigrator{
MigrationsDirPath: migrationsDirPath,
}
}
// DirMigrator is a pgtestdb.Migrator that uses the `atlas` CLI
// tool to perform migrations.
//
// atlas migrate apply --url $DB --dir file://$migrationsDirPath
//
// DirMigrator requires that it runs in an environment where the `atlas` CLI is
// in the $PATH. It shells out to that program to perform its migrations,
// as recommended by the Atlas maintainers.
//
// DirMigrator does not perform any Verify() or Prepare() logic.
type DirMigrator struct {
MigrationsDirPath string
}
func (m *DirMigrator) Hash() (string, error) {
return common.HashDir(m.MigrationsDirPath)
}
// Migrate shells out to the `atlas` CLI program to migrate the template
// database.
//
// atlas migrate apply --url $DB --dir file://$migrationsDirPath
func (m *DirMigrator) Migrate(
ctx context.Context,
_ *sql.DB,
templateConf pgtestdb.Config,
) error {
_, err := common.Execute(ctx, nil,
"atlas",
"migrate",
"apply",
"--url",
templateConf.URL(),
"--dir",
"file://"+m.MigrationsDirPath,
)
return err
}
// Prepare is a no-op method.
func (*DirMigrator) Prepare(
_ context.Context,
_ *sql.DB,
_ pgtestdb.Config,
) error {
return nil
}
// Verify is a no-op method.
func (*DirMigrator) Verify(
_ context.Context,
_ *sql.DB,
_ pgtestdb.Config,
) error {
return nil
}