This repository has been archived by the owner on Apr 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
upgrade.go
395 lines (330 loc) · 11.4 KB
/
upgrade.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package data
import (
"database/sql"
"fmt"
"github.com/pkg/errors"
)
func upgradeTo_1_1_0(db *sql.DB) (string, error) {
order := []string{
"cluster_type", "engine", "entity_type", "identity", "permission", "project", "role", "workgroup",
"cluster", "cluster_yarn", "datasource", "history", "identity_role", "identity_workgroup", "privilege", "role_permission",
"dataset",
"model",
"binomial_model", "label", "multinomial_model", "regression_model", "service",
}
cols := map[string][]string{
"binomial_model": []string{"model_id", "mse", "r_squared", "logloss", "auc", "gini"},
"cluster": []string{"id", "name", "type_id", "detail_id", "address", "state", "created"},
"cluster_type": []string{"id", "name"},
"cluster_yarn": []string{"id", "engine_id", "size", "application_id", "memory", "username", "output_dir"},
"dataset": []string{"id", "datasource_id", "name", "description", "frame_name", "response_column_name", "properties", "properties_version", "created"},
"datasource": []string{"id", "project_id", "name", "description", "kind", "configuration", "created"},
"engine": []string{"id", "name", "location", "created"},
"entity_type": []string{"id", "name"},
"history": []string{"id", "action", "identity_id", "entity_type_id", "entity_id", "description", "created"},
"identity": []string{"id", "name", "password", "workgroup_id", "is_active", "last_login", "created"},
"identity_role": []string{"identity_id", "role_id"},
"identity_workgroup": []string{"identity_id", "workgroup_id"},
"label": []string{"id", "project_id", "model_id", "name", "description", "created"},
"model": []string{"id", "project_id", "training_dataset_id", "validation_dataset_id", "name", "cluster_name", "model_key", "algorithm", "model_category", "dataset_name", "response_column_name", "logical_name", "location", "max_run_time", "metrics", "metrics_version", "created"},
"multinomial_model": []string{"model_id", "mse", "r_squared", "logloss"},
"permission": []string{"id", "code", "description"},
"privilege": []string{"privilege_type", "workgroup_id", "entity_type_id", "entity_id"},
"project": []string{"id", "name", "description", "model_category", "created"},
"regression_model": []string{"model_id", "mse", "r_squared", "mean_residual_deviance"},
"service": []string{"id", "project_id", "model_id", "name", "address", "port", "process_id", "state", "created"},
"role": []string{"id", "name", "description", "created"},
"role_permission": []string{"role_id", "permission_id"},
"workgroup": []string{"id", "type", "name", "description", "created"},
}
qrys := map[string]string{
"binomial_model": `
model_id integer NOT NULL,
mse double precision,
r_squared double precision,
logloss double precision,
auc double precision,
gini double precision,
PRIMARY KEY (model_id),
FOREIGN KEY (model_id) REFERENCES model(id) ON DELETE CASCADE
`,
"cluster": `
id integer PRIMARY KEY AUTOINCREMENT,
name text NOT NULL,
type_id integer NOT NULL,
detail_id integer NOT NULL,
address text NOT NULL,
state job_state NOT NULL,
created datetime NOT NULL,
FOREIGN KEY (type_id) REFERENCES cluster_type(id)
`,
"cluster_type": `
id integer PRIMARY KEY AUTOINCREMENT,
name text NOT NULL UNIQUE
`,
"cluster_yarn": `
id integer PRIMARY KEY AUTOINCREMENT,
engine_id integer NOT NULL,
size integer NOT NULL,
application_id text NOT NULL,
memory text NOT NULL,
username text NOT NULL,
output_dir text NOT NULL,
FOREIGN KEY (engine_id) REFERENCES engine(id)
`,
"dataset": `
id integer PRIMARY KEY AUTOINCREMENT,
datasource_id integer NOT NULL,
name text NOT NULL,
description text NOT NULL,
frame_name text NOT NULL,
response_column_name text NOT NULL,
properties text NOT NULL,
properties_version text NOT NULL,
created datetime NOT NULL,
FOREIGN KEY (datasource_id) REFERENCES datasource(id) ON DELETE CASCADE
`,
"datasource": `
id integer PRIMARY KEY AUTOINCREMENT,
project_id integer NOT NULL,
name text NOT NULL,
description text NOT NULL,
kind text NOT NULL,
configuration text NOT NULL,
created datetime NOT NULL,
FOREIGN KEY (project_id) REFERENCES project(id) ON DELETE CASCADE
`,
"engine": `
id integer PRIMARY KEY AUTOINCREMENT,
name text NOT NULL,
location text NOT NULL,
created datetime NOT NULL
`,
"entity_type": `
id integer PRIMARY KEY AUTOINCREMENT,
name text NOT NULL UNIQUE
`,
"history": `
id integer PRIMARY KEY AUTOINCREMENT,
action text NOT NULL,
identity_id integer NOT NULL,
entity_type_id integer NOT NULL,
entity_id integer NOT NULL,
description text NOT NULL,
created datetime NOT NULL,
FOREIGN KEY (entity_type_id) REFERENCES entity_type(id),
FOREIGN KEY (identity_id) REFERENCES identity(id)
`,
"identity": `
id integer PRIMARY KEY AUTOINCREMENT,
name text NOT NULL UNIQUE,
password text NOT NULL,
workgroup_id integer NOT NULL,
is_active boolean NOT NULL,
last_login integer with time zone,
created datetime NOT NULL
`,
"identity_role": `
identity_id integer NOT NULL,
role_id integer NOT NULL,
PRIMARY KEY (identity_id, role_id)
`,
"identity_workgroup": `
identity_id integer NOT NULL,
workgroup_id integer NOT NULL,
PRIMARY KEY (identity_id, workgroup_id),
FOREIGN KEY (identity_id) REFERENCES identity(id) ON DELETE CASCADE,
FOREIGN KEY (workgroup_id) REFERENCES workgroup(id) ON DELETE CASCADE
`,
"label": `
id integer PRIMARY KEY AUTOINCREMENT,
project_id integer NOT NULL,
model_id integer,
name text NOT NULL,
description text NOT NULL,
created datetime NOT NULL,
FOREIGN KEY (model_id) REFERENCES model(id) ON DELETE SET NULL,
FOREIGN KEY (project_id) REFERENCES project(id) ON DELETE CASCADE
`,
"model": `
id integer PRIMARY KEY AUTOINCREMENT,
project_id integer NOT NULL,
training_dataset_id integer NOT NULL,
validation_dataset_id integer,
name text NOT NULL,
cluster_id integer,
cluster_name text NOT NULL,
model_key text NOT NULL,
algorithm text NOT NULL,
model_category text NOT NULL,
dataset_name text NOT NULL,
response_column_name text NOT NULL,
logical_name text,
location text NOT NULL,
model_object_type text,
max_run_time integer,
metrics text NOT NULL,
metrics_version text NOT NULL,
created datetime NOT NULL,
FOREIGN KEY (project_id) REFERENCES project(id),
FOREIGN KEY (training_dataset_id) REFERENCES dataset(id),
FOREIGN KEY (validation_dataset_id) REFERENCES dataset(id),
FOREIGN KEY (cluster_id) REFERENCES cluster(id) ON DELETE SET NULL
`,
"multinomial_model": `
model_id integer NOT NULL,
mse double precision,
r_squared double precision,
logloss double precision,
PRIMARY KEY (model_id),
FOREIGN KEY (model_id) REFERENCES model(id) ON DELETE CASCADE
`,
"permission": `
id integer PRIMARY KEY AUTOINCREMENT,
code text NOT NULL UNIQUE,
description text NOT NULL
`,
"privilege": `
privilege_type text NOT NULL,
workgroup_id integer NOT NULL,
entity_type_id integer NOT NULL,
entity_id integer NOT NULL,
PRIMARY KEY (privilege_type, workgroup_id, entity_type_id, entity_id),
FOREIGN KEY (entity_type_id) REFERENCES entity_type(id),
FOREIGN KEY (workgroup_id) REFERENCES workgroup(id)
`,
"project": `
id integer PRIMARY KEY AUTOINCREMENT,
name text NOT NULL,
description text NOT NULL,
model_category text NOT NULL,
created datetime NOT NULL
`,
"regression_model": `
model_id integer NOT NULL,
mse double precision,
r_squared double precision,
mean_residual_deviance double precision,
PRIMARY KEY (model_id),
FOREIGN KEY (model_id) REFERENCES model(id) ON DELETE CASCADE
`,
"role": `
id integer PRIMARY KEY AUTOINCREMENT,
name text NOT NULL UNIQUE,
description text NOT NULL,
created datetime NOT NULL
`,
"role_permission": `
role_id integer NOT NULL,
permission_id integer NOT NULL,
PRIMARY KEY (role_id, permission_id),
FOREIGN KEY (permission_id) REFERENCES permission(id) ON DELETE CASCADE,
FOREIGN KEY (role_id) REFERENCES role(id) ON DELETE CASCADE
`,
"service": `
id integer PRIMARY KEY AUTOINCREMENT,
project_id integer NOT NULL,
model_id integer NOT NULL,
name text NOT NULL,
address text NOT NULL,
port integer NOT NULL,
process_id integer NOT NULL,
state job_state NOT NULL,
created datetime NOT NULL,
FOREIGN KEY (model_id) REFERENCES model(id)
`,
"workgroup": `
id integer PRIMARY KEY AUTOINCREMENT,
type workgroup_type NOT NULL,
name text NOT NULL UNIQUE,
description text NOT NULL,
created datetime NOT NULL
`,
}
tx, err := db.Begin()
if err != nil {
return "", errors.Wrap(err, "starting transaction")
}
defer tx.Rollback()
for _, table := range order {
if err := createTable(tx, table, cols[table]...); err != nil {
return "", errors.Wrapf(err, "initializing table for %s", table)
}
ok, err := checkTable(tx, table)
if err != nil {
return "", errors.Wrapf(err, "checking table values for %s", table)
}
if err := createTemp(tx, table); err != nil {
return "", errors.Wrapf(err, "creating temp for %s", table)
}
if err := createNew(tx, table, qrys[table]); err != nil {
return "", errors.Wrapf(err, "creating new table for %s", table)
}
if ok {
if err := copyTable(tx, table, cols[table]...); err != nil {
return "", errors.Wrapf(err, "copying values for %s", table)
}
}
}
for i := len(order) - 1; i >= 0; i-- {
if err := dropTemp(tx, order[i]); err != nil {
return "", errors.Wrapf(err, "dropping temp for %s", order[i])
}
}
if _, err := tx.Exec(`UPDATE meta SET value = $1 WHERE id = 1`, "1.1.0"); err != nil {
return "", errors.Wrap(err, "updating database version")
}
return "1.1.0", errors.Wrap(tx.Commit(), "commiting changes")
}
func createTable(tx *sql.Tx, table string, cols ...string) error {
var colStr string
for i, col := range cols {
if i > 0 {
colStr += ", "
}
colStr += col
}
qry := fmt.Sprintf(`CREATE TABLE IF NOT EXISTS %s(%s)`, table, colStr)
_, err := tx.Exec(qry)
return err
}
func checkTable(tx *sql.Tx, table string) (bool, error) {
qry := fmt.Sprintf(`SELECT count(*) FROM %s`, table)
row := tx.QueryRow(qry)
var ct int64
err := row.Scan(&ct)
if ct > 0 {
return true, err
}
return false, err
}
func createTemp(tx *sql.Tx, table string) error {
tmp := "temp_" + table
qry := fmt.Sprintf(`ALTER TABLE %s RENAME TO %s`, table, tmp)
_, err := tx.Exec(qry)
return err
}
func createNew(tx *sql.Tx, table, cols string) error {
qry := fmt.Sprintf(`CREATE TABLE %s (%s)`, table, cols)
_, err := tx.Exec(qry)
return err
}
func copyTable(tx *sql.Tx, table string, cols ...string) error {
tmp := "temp_" + table
var colStr string
for i, col := range cols {
if i > 0 {
colStr += ", "
}
colStr += col
}
qry := fmt.Sprintf(`INSERT INTO %s(%s) SELECT %s FROM %s`, table, colStr, colStr, tmp)
_, err := tx.Exec(qry)
return err
}
func dropTemp(tx *sql.Tx, table string) error {
tmp := "temp_" + table
qry := fmt.Sprintf(`DROP TABLE %s`, tmp)
_, err := tx.Exec(qry)
return err
}