2017-03-12 16:24:15 +00:00
|
|
|
package bolt
|
|
|
|
|
2017-05-23 18:56:10 +00:00
|
|
|
import "github.com/portainer/portainer"
|
2017-03-12 16:24:15 +00:00
|
|
|
|
2017-05-23 18:56:10 +00:00
|
|
|
// Migrator defines a service to migrate data after a Portainer version update.
|
2017-03-12 16:24:15 +00:00
|
|
|
type Migrator struct {
|
|
|
|
UserService *UserService
|
|
|
|
EndpointService *EndpointService
|
|
|
|
ResourceControlService *ResourceControlService
|
2017-08-10 08:35:23 +00:00
|
|
|
SettingsService *SettingsService
|
2017-03-12 16:24:15 +00:00
|
|
|
VersionService *VersionService
|
|
|
|
CurrentDBVersion int
|
|
|
|
store *Store
|
|
|
|
}
|
|
|
|
|
2017-05-23 18:56:10 +00:00
|
|
|
// NewMigrator creates a new Migrator.
|
2017-03-12 16:24:15 +00:00
|
|
|
func NewMigrator(store *Store, version int) *Migrator {
|
|
|
|
return &Migrator{
|
|
|
|
UserService: store.UserService,
|
|
|
|
EndpointService: store.EndpointService,
|
|
|
|
ResourceControlService: store.ResourceControlService,
|
2017-08-10 08:35:23 +00:00
|
|
|
SettingsService: store.SettingsService,
|
2017-03-12 16:24:15 +00:00
|
|
|
VersionService: store.VersionService,
|
|
|
|
CurrentDBVersion: version,
|
|
|
|
store: store,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-23 18:56:10 +00:00
|
|
|
// Migrate checks the database version and migrate the existing data to the most recent data model.
|
2017-03-12 16:24:15 +00:00
|
|
|
func (m *Migrator) Migrate() error {
|
|
|
|
|
|
|
|
// Portainer < 1.12
|
2017-09-14 06:08:37 +00:00
|
|
|
if m.CurrentDBVersion < 1 {
|
2017-05-23 18:56:10 +00:00
|
|
|
err := m.updateAdminUserToDBVersion1()
|
2017-03-12 16:24:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-23 18:56:10 +00:00
|
|
|
// Portainer 1.12.x
|
2017-09-14 06:08:37 +00:00
|
|
|
if m.CurrentDBVersion < 2 {
|
2017-05-23 18:56:10 +00:00
|
|
|
err := m.updateResourceControlsToDBVersion2()
|
2017-03-12 16:24:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-23 18:56:10 +00:00
|
|
|
err = m.updateEndpointsToDBVersion2()
|
2017-03-12 16:24:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-23 18:56:10 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 08:35:23 +00:00
|
|
|
// Portainer 1.13.x
|
2017-09-14 06:08:37 +00:00
|
|
|
if m.CurrentDBVersion < 3 {
|
|
|
|
err := m.updateSettingsToDBVersion3()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Portainer 1.14.0
|
|
|
|
if m.CurrentDBVersion < 4 {
|
|
|
|
err := m.updateEndpointsToDBVersion4()
|
2017-08-10 08:35:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-23 18:56:10 +00:00
|
|
|
err := m.VersionService.StoreDBVersion(portainer.DBVersion)
|
|
|
|
if err != nil {
|
2017-03-12 16:24:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|