-
Notifications
You must be signed in to change notification settings - Fork 244
/
migrations.go
80 lines (69 loc) · 2.12 KB
/
migrations.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
package sqlite
import (
"strings"
"github.com/pkg/errors"
encryptmigrations "github.com/status-im/status-go/protocol/encryption/migrations"
appmigrations "github.com/status-im/status-go/protocol/migrations"
push_notification_client_migrations "github.com/status-im/status-go/protocol/pushnotificationclient/migrations"
push_notification_server_migrations "github.com/status-im/status-go/protocol/pushnotificationserver/migrations"
wakumigrations "github.com/status-im/status-go/protocol/transport/waku/migrations"
whispermigrations "github.com/status-im/status-go/protocol/transport/whisper/migrations"
)
type getter func(string) ([]byte, error)
type migrationsWithGetter struct {
Names []string
Getter getter
}
var defaultMigrations = []migrationsWithGetter{
{
Names: whispermigrations.AssetNames(),
Getter: whispermigrations.Asset,
},
{
Names: wakumigrations.AssetNames(),
Getter: wakumigrations.Asset,
},
{
Names: encryptmigrations.AssetNames(),
Getter: encryptmigrations.Asset,
},
{
Names: appmigrations.AssetNames(),
Getter: appmigrations.Asset,
},
{
Names: push_notification_server_migrations.AssetNames(),
Getter: push_notification_server_migrations.Asset,
},
{
Names: push_notification_client_migrations.AssetNames(),
Getter: push_notification_client_migrations.Asset,
},
}
func prepareMigrations(migrations []migrationsWithGetter) ([]string, getter, error) {
var allNames []string
nameToGetter := make(map[string]getter)
for _, m := range migrations {
for _, name := range m.Names {
if !validateName(name) {
continue
}
if _, ok := nameToGetter[name]; ok {
return nil, nil, errors.Errorf("migration with name %s already exists", name)
}
allNames = append(allNames, name)
nameToGetter[name] = m.Getter
}
}
return allNames, func(name string) ([]byte, error) {
getter, ok := nameToGetter[name]
if !ok {
return nil, errors.Errorf("no migration for name %s", name)
}
return getter(name)
}, nil
}
// validateName verifies that only *.sql files are taken into consideration.
func validateName(name string) bool {
return strings.HasSuffix(name, ".sql")
}