-
Notifications
You must be signed in to change notification settings - Fork 75
/
database.go
199 lines (161 loc) · 4.47 KB
/
database.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package database
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/pkg/errors"
schemasv1alpha4 "github.com/schemahero/schemahero/pkg/apis/schemas/v1alpha4"
"github.com/schemahero/schemahero/pkg/database/mysql"
"github.com/schemahero/schemahero/pkg/database/postgres"
"github.com/schemahero/schemahero/pkg/logger"
"go.uber.org/zap"
"gopkg.in/yaml.v2"
)
type Database struct {
InputDir string
OutputDir string
Driver string
URI string
VaultURIRef string
}
func (d *Database) CreateFixturesSync() error {
logger.Infof("generating fixtures",
zap.String("input-dir", d.InputDir))
statements := []string{}
handleFile := func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
fileData, err := ioutil.ReadFile(filepath.Join(d.InputDir, info.Name()))
if err != nil {
return err
}
var spec *schemasv1alpha4.TableSpec
parsedK8sObject := schemasv1alpha4.Table{}
if err := yaml.Unmarshal(fileData, &parsedK8sObject); err == nil {
if parsedK8sObject.Spec.Schema != nil {
spec = &parsedK8sObject.Spec
}
}
if spec == nil {
plainSpec := schemasv1alpha4.TableSpec{}
if err := yaml.Unmarshal(fileData, &plainSpec); err != nil {
return err
}
spec = &plainSpec
}
if spec.Schema == nil {
return nil
}
if d.Driver == "postgres" {
if spec.Schema.Postgres == nil {
return nil
}
statement, err := postgres.CreateTableStatement(spec.Name, spec.Schema.Postgres)
if err != nil {
return err
}
statements = append(statements, statement)
} else if d.Driver == "mysql" {
if spec.Schema.Mysql == nil {
return nil
}
statement, err := mysql.CreateTableStatement(spec.Name, spec.Schema.Mysql)
if err != nil {
return err
}
statements = append(statements, statement)
} else if d.Driver == "cockroachdb" {
if spec.Schema.CockroachDB == nil {
return nil
}
statement, err := postgres.CreateTableStatement(spec.Name, spec.Schema.CockroachDB)
if err != nil {
return err
}
statements = append(statements, statement)
}
return nil
}
err := filepath.Walk(d.InputDir, handleFile)
if err != nil {
fmt.Printf("%#v\n", err)
return err
}
output := strings.Join(statements, ";\n")
output = fmt.Sprintf("/* Auto generated file. Do not edit by hand. This file was generated by SchemaHero. */\n\n %s;\n\n", output)
if _, err := os.Stat(d.OutputDir); os.IsNotExist(err) {
os.MkdirAll(d.OutputDir, 0755)
}
err = ioutil.WriteFile(filepath.Join(d.OutputDir, "fixtures.sql"), []byte(output), 0644)
if err != nil {
fmt.Printf("%#v\n", err)
return err
}
return nil
}
func (d *Database) PlanSync(filename string) ([]string, error) {
specContents, err := ioutil.ReadFile(filename)
if err != nil {
return nil, errors.Wrap(err, "failed to read file")
}
var spec *schemasv1alpha4.TableSpec
parsedK8sObject := schemasv1alpha4.Table{}
if err := yaml.Unmarshal(specContents, &parsedK8sObject); err == nil {
if parsedK8sObject.Spec.Schema != nil {
spec = &parsedK8sObject.Spec
}
}
if spec == nil {
plainSpec := schemasv1alpha4.TableSpec{}
if err := yaml.Unmarshal(specContents, &plainSpec); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal spec")
}
spec = &plainSpec
}
if spec.Schema == nil {
return []string{}, nil
}
uri, err := getURI(d)
if err != nil {
return nil, err
}
if d.Driver == "postgres" {
return postgres.PlanPostgresTable(uri, spec.Name, spec.Schema.Postgres)
} else if d.Driver == "mysql" {
return mysql.PlanMysqlTable(uri, spec.Name, spec.Schema.Mysql)
} else if d.Driver == "cockroachdb" {
return postgres.PlanPostgresTable(uri, spec.Name, spec.Schema.CockroachDB)
}
return nil, errors.New("unknown database driver")
}
func getURI(d *Database) (string, error) {
if d.VaultURIRef != "" {
b, err := ioutil.ReadFile(d.VaultURIRef)
if err != nil {
return "", errors.Wrap(err, "failed to read vault uri file")
}
return string(b), nil
}
return d.URI, nil
}
func (d *Database) ApplySync(statements []string) error {
uri, err := getURI(d)
fmt.Printf("URI is: %s\n", uri)
if err != nil {
return err
}
if d.Driver == "postgres" {
return postgres.DeployPostgresStatements(uri, statements)
} else if d.Driver == "mysql" {
return mysql.DeployMysqlStatements(uri, statements)
} else if d.Driver == "cockroachdb" {
return postgres.DeployPostgresStatements(uri, statements)
}
return errors.New("unknown database driver")
}