-
Notifications
You must be signed in to change notification settings - Fork 19
/
arangomigo.go
99 lines (84 loc) · 2.09 KB
/
arangomigo.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
/*
Package arangomigo allows the tool to execute from the command line.
*/
package arangomigo
import (
"context"
"fmt"
"log"
"os"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)
func TriggerMigration(configAt string) {
config, err := loadConf(configAt)
if e(err) {
log.Fatal(err)
}
if err := migrate(*config); err != nil {
log.Fatal("Could not perform migration\n", err)
}
log.Println("Successfully completed migration")
}
// TODO remember that having replayable migrations need to be possible too.
// Have branch those into running at the end.
func migrate(c Config) error {
ctx := context.Background()
pm, err := migrations(c.MigrationsPath)
if e(err) {
return err
}
return perform(ctx, c, pm)
}
// Reads in a yaml file at the confLoc and returns the Config instance.
func loadConf(confLoc string) (*Config, error) {
bytes, _, err := open(confLoc)
if e(err) {
return nil, fmt.Errorf("couldn't locate configation at path '%s'", confLoc)
}
conf := Config{}
err = yaml.Unmarshal(bytes, &conf)
if e(err) {
return nil, errors.Wrapf(err, "Couldn't parse configation at path '%s'", confLoc)
}
if conf.Db == "" {
return nil, errors.New("Please specifiy the database name in the config")
}
encased := make(map[string]interface{})
for k, v := range conf.Extras {
encased[fmt.Sprintf("${%s}", k)] = v
}
conf.Extras = encased
arangoUrl, exists := os.LookupEnv("ARANGO_URL")
if exists {
conf.Endpoints = []string{arangoUrl}
}
return &conf, nil
}
type StringArray []string
func (a *StringArray) UnmarshalYAML(unmarshal func(interface{}) error) error {
var multi []string
err := unmarshal(&multi)
if err != nil {
var single string
err = unmarshal(&single)
if err != nil {
return err
}
*a = []string{single}
} else {
*a = multi
}
return nil
}
// Config The content of a migration configuration.
type Config struct {
Endpoints []string
Username string
Password string
MigrationsPath StringArray
Db string
SkipSslVerify bool `yaml:"skip_ssl_verify"`
// Extras allows the user to pass in replaced variables
Extras map[string]interface{}
}