-
Notifications
You must be signed in to change notification settings - Fork 6
/
schema.go
154 lines (127 loc) · 3.28 KB
/
schema.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
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
package cmd
import (
"fmt"
"net/http"
"os"
"strings"
"time"
"github.com/ory/dockertest/v3"
"github.com/ory/dockertest/v3/docker"
"github.com/spf13/cobra"
"github.com/roryq/wrench/internal/graceful"
)
var gracefulSchemaTasks = graceful.OnShutdown{}
var schemaCmd = &cobra.Command{
Use: "schema",
Short: "Runs the migrations against a dockerised spanner emulator, then loads the schema and static data to disk. (Requires docker)",
RunE: schema,
}
func init() {
// schema flags
schemaCmd.Flags().String(flagSpannerEmulatorImage, "roryq/spanner-emulator:latest", "Spanner emulator image to use. Override this to pin version or change registry.")
// copy migrate up flags
schemaCmd.Flags().AddFlagSet(findCommand("up").LocalFlags())
}
func findCommand(name string) *cobra.Command {
for _, command := range migrateCmd.Commands() {
if command.Name() == name {
return command
}
}
return nil
}
func schema(c *cobra.Command, args []string) error {
defer gracefulSchemaTasks.Exit()
f := c.Flag(flagSpannerEmulatorImage)
_, err := runSpannerEmulator(f.Value.String())
if err != nil {
return err
}
_ = c.Flag(flagNameProject).Value.Set("schema")
_ = c.Flag(flagNameInstance).Value.Set("schema")
_ = c.Flag(flagNameDatabase).Value.Set("schema")
// run migrations
if err := migrateUp(c, args); err != nil {
return err
}
// load schema
if err := load(c, args); err != nil {
return err
}
// load discrete
if err := loadDiscrete(c, args); err != nil {
return err
}
return err
}
func connectDockerPool() (*dockertest.Pool, error) {
pool, err := dockertest.NewPool("")
if err != nil {
return nil, err
}
return pool, pool.Client.Ping()
}
func runSpannerEmulator(image string) (*spannerEmulator, error) {
pool, err := connectDockerPool()
if err != nil {
return nil, err
}
repo, tag, _ := strings.Cut(image, ":")
container, err := pool.RunWithOptions(&dockertest.RunOptions{
Repository: repo,
Tag: tag,
Env: []string{
"SPANNER_PROJECT_ID=schema",
"SPANNER_INSTANCE_ID=schema",
"SPANNER_DATABASE_ID=schema",
},
},
func(config *docker.HostConfig) {
config.AutoRemove = true
config.RestartPolicy = docker.NeverRestart()
},
)
if err != nil {
return nil, err
}
gracefulSchemaTasks.Do(func() {
if err = container.Close(); err != nil {
println("error during spanner container shutdown", err.Error())
}
})
pool.MaxWait = time.Minute
err = pool.Retry(func() error {
hcheck := fmt.Sprintf("http://localhost:%s/v1/projects/test-project/instanceConfigs", container.GetPort("9020/tcp"))
_, err := http.Get(hcheck)
return err
})
if err != nil {
return nil, err
}
unset, err := setenv("SPANNER_EMULATOR_HOST", container.GetHostPort("9010/tcp"))
if err != nil {
return nil, err
}
gracefulSchemaTasks.Do(unset)
return &spannerEmulator{container: container, pool: pool}, nil
}
type spannerEmulator struct {
container *dockertest.Resource
pool *dockertest.Pool
}
func (s *spannerEmulator) SpannerEmulatorHost() string {
return s.container.GetHostPort("9010/tcp")
}
func setenv(key, value string) (func(), error) {
prevValue, ok := os.LookupEnv(key)
if err := os.Setenv(key, value); err != nil {
return func() {}, err
}
return func() {
if ok {
os.Setenv(key, prevValue)
} else {
os.Unsetenv(key)
}
}, nil
}