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-09-26 03:36:51 +00:00
|
|
|
// https://github.com/portainer/portainer/issues/1235
|
|
|
|
if m.CurrentDBVersion < 5 {
|
|
|
|
err := m.updateSettingsToVersion5()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-27 07:26:04 +00:00
|
|
|
// https://github.com/portainer/portainer/issues/1236
|
|
|
|
if m.CurrentDBVersion < 6 {
|
|
|
|
err := m.updateSettingsToVersion6()
|
|
|
|
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
|
|
|
|
}
|