-
Notifications
You must be signed in to change notification settings - Fork 0
/
atlas.go
67 lines (56 loc) · 1.6 KB
/
atlas.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
package migrator
import (
"ariga.io/atlas-go-sdk/atlasexec"
"context"
"fmt"
"github.com/vectrum-io/strongforce/pkg/db"
)
type Atlas struct {
migrationDir string
atlasBinaryPath string
atlasBinaryName string
baseline string
allowDirty bool
}
type AtlasOptions struct {
MigrationDir string
AtlasBinaryPath string
AtlasBinaryName string
BaselineVersion string
AllowDirty bool
}
func NewAtlasMigrator(options *AtlasOptions) *Atlas {
if options.AtlasBinaryName == "" {
options.AtlasBinaryName = "atlas"
}
return &Atlas{
migrationDir: options.MigrationDir,
atlasBinaryPath: options.AtlasBinaryPath,
atlasBinaryName: options.AtlasBinaryName,
baseline: options.BaselineVersion,
allowDirty: options.AllowDirty,
}
}
func (a *Atlas) Migrate(ctx context.Context, dsn string) (*db.MigrationResult, error) {
client, err := atlasexec.NewClient(a.atlasBinaryPath, a.atlasBinaryName)
if err != nil {
return nil, fmt.Errorf("failed to create atlas client: %w", err)
}
res, err := client.MigrateApply(ctx, &atlasexec.MigrateApplyParams{
URL: dsn,
DirURL: fmt.Sprintf("file://%s", a.migrationDir),
BaselineVersion: a.baseline,
AllowDirty: a.allowDirty,
})
if err != nil {
return nil, fmt.Errorf("failed to apply migrations: %w", err)
}
if res.Error != "" {
return nil, fmt.Errorf("failed to apply: %s", res.Error)
}
appliedMigrations := make([]string, len(res.Applied))
for i, applied := range res.Applied {
appliedMigrations[i] = applied.File.Name
}
return &db.MigrationResult{AppliedMigrations: appliedMigrations}, nil
}