2019-08-27 12:04:15 +00:00
|
|
|
package sqlite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2019-11-21 16:19:22 +00:00
|
|
|
encryptmigrations "github.com/status-im/status-go/protocol/encryption/migrations"
|
|
|
|
appmigrations "github.com/status-im/status-go/protocol/migrations"
|
2020-07-22 07:41:40 +00:00
|
|
|
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"
|
2020-01-13 19:17:30 +00:00
|
|
|
wakumigrations "github.com/status-im/status-go/protocol/transport/waku/migrations"
|
|
|
|
whispermigrations "github.com/status-im/status-go/protocol/transport/whisper/migrations"
|
2019-08-27 12:04:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type getter func(string) ([]byte, error)
|
|
|
|
|
|
|
|
type migrationsWithGetter struct {
|
|
|
|
Names []string
|
|
|
|
Getter getter
|
|
|
|
}
|
|
|
|
|
|
|
|
var defaultMigrations = []migrationsWithGetter{
|
|
|
|
{
|
2020-01-13 19:17:30 +00:00
|
|
|
Names: whispermigrations.AssetNames(),
|
|
|
|
Getter: whispermigrations.Asset,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Names: wakumigrations.AssetNames(),
|
|
|
|
Getter: wakumigrations.Asset,
|
2019-08-27 12:04:15 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Names: encryptmigrations.AssetNames(),
|
|
|
|
Getter: encryptmigrations.Asset,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Names: appmigrations.AssetNames(),
|
|
|
|
Getter: appmigrations.Asset,
|
|
|
|
},
|
2020-07-01 12:04:09 +00:00
|
|
|
{
|
|
|
|
Names: push_notification_server_migrations.AssetNames(),
|
|
|
|
Getter: push_notification_server_migrations.Asset,
|
|
|
|
},
|
2020-07-07 09:00:04 +00:00
|
|
|
{
|
|
|
|
Names: push_notification_client_migrations.AssetNames(),
|
|
|
|
Getter: push_notification_client_migrations.Asset,
|
|
|
|
},
|
2019-08-27 12:04:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|