forked from pachyderm/pachyderm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migration.go
91 lines (82 loc) · 2.26 KB
/
migration.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
package persist
import (
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/dancannon/gorethink"
)
type migrationFunc func(address string, databaseName string) error
// MissingMigrationErr denotes that no migration is supported for the provided versions
type MissingMigrationErr struct {
error
}
func newMissingMigrationErr(msg error) MissingMigrationErr {
return MissingMigrationErr{msg}
}
var (
migrationMap = map[string]migrationFunc{
"1.3.4-1.3.7": oneThreeFourToOneThreeSeven,
"1.3.7-1.3.8": oneThreeSevenToOneThreeEight,
}
)
// Migrate updates the database schema only in the forward direction
func Migrate(address, databaseName, migrationKey string) error {
migrate, ok := migrationMap[migrationKey]
if !ok {
return newMissingMigrationErr(fmt.Errorf("migration %s is not supported for %v", migrationKey, databaseName))
}
return migrate(address, databaseName)
}
// 1.3.4 -> 1.3.7
func oneThreeFourToOneThreeSeven(address, databaseName string) error {
session, err := DbConnect(address)
if err != nil {
return err
}
log.Infof("Renaming Diff 'Size' to 'SizeBytes'")
if _, err := gorethink.DB(databaseName).Table(diffTable).Replace(
func(row gorethink.Term) gorethink.Term {
return row.Without("Size").Merge(
map[string]gorethink.Term{
"SizeBytes": row.Field("Size"),
},
)
},
).RunWrite(session); err != nil {
return err
}
log.Infof("Renaming Repo 'Size' to 'SizeBytes'")
if _, err := gorethink.DB(databaseName).Table(repoTable).Replace(
func(row gorethink.Term) gorethink.Term {
return row.Without("Size").Merge(
map[string]gorethink.Term{
"SizeBytes": row.Field("Size"),
},
)
},
).RunWrite(session); err != nil {
return err
}
log.Infof("Migration succeeded")
return nil
}
// 1.3.7 -> 1.3.8
func oneThreeSevenToOneThreeEight(address, databaseName string) error {
session, err := DbConnect(address)
if err != nil {
return err
}
log.Infof("Renaming Commit 'Size' to 'SizeBytes'")
if _, err := gorethink.DB(databaseName).Table(commitTable).Replace(
func(row gorethink.Term) gorethink.Term {
return row.Without("Size").Merge(
map[string]gorethink.Term{
"SizeBytes": row.Field("Size"),
},
)
},
).RunWrite(session); err != nil {
return err
}
log.Infof("Migration succeeded")
return nil
}