2021-09-27 00:52:50 +00:00
|
|
|
package migrator
|
|
|
|
|
|
|
|
import (
|
2022-04-14 04:25:13 +00:00
|
|
|
"reflect"
|
|
|
|
"runtime"
|
2021-09-27 00:52:50 +00:00
|
|
|
|
|
|
|
portainer "github.com/portainer/portainer/api"
|
2022-09-16 16:18:44 +00:00
|
|
|
|
2022-09-28 17:56:32 +00:00
|
|
|
"github.com/pkg/errors"
|
2022-09-16 16:18:44 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2021-09-27 00:52:50 +00:00
|
|
|
)
|
|
|
|
|
2022-04-14 04:25:13 +00:00
|
|
|
type migration struct {
|
|
|
|
dbversion int
|
|
|
|
migrate func() error
|
|
|
|
}
|
|
|
|
|
2021-09-27 00:52:50 +00:00
|
|
|
func migrationError(err error, context string) error {
|
2022-09-28 17:56:32 +00:00
|
|
|
return errors.Wrap(err, "failed in "+context)
|
2021-09-27 00:52:50 +00:00
|
|
|
}
|
|
|
|
|
2022-04-14 04:25:13 +00:00
|
|
|
func newMigration(dbversion int, migrate func() error) migration {
|
|
|
|
return migration{
|
|
|
|
dbversion: dbversion,
|
|
|
|
migrate: migrate,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func dbTooOldError() error {
|
|
|
|
return errors.New("migrating from less than Portainer 1.21.0 is not supported, please contact Portainer support.")
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetFunctionName(i interface{}) string {
|
|
|
|
return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
|
|
|
|
}
|
|
|
|
|
2021-09-27 00:52:50 +00:00
|
|
|
// Migrate checks the database version and migrate the existing data to the most recent data model.
|
|
|
|
func (m *Migrator) Migrate() error {
|
|
|
|
// set DB to updating status
|
|
|
|
err := m.versionService.StoreIsUpdating(true)
|
|
|
|
if err != nil {
|
|
|
|
return migrationError(err, "StoreIsUpdating")
|
|
|
|
}
|
|
|
|
|
2022-04-14 04:25:13 +00:00
|
|
|
migrations := []migration{
|
|
|
|
// Portainer < 1.21.0
|
|
|
|
newMigration(17, dbTooOldError),
|
2021-09-27 00:52:50 +00:00
|
|
|
|
2022-04-14 04:25:13 +00:00
|
|
|
// Portainer 1.21.0
|
|
|
|
newMigration(18, m.updateUsersToDBVersion18),
|
|
|
|
newMigration(18, m.updateEndpointsToDBVersion18),
|
|
|
|
newMigration(18, m.updateEndpointGroupsToDBVersion18),
|
|
|
|
newMigration(18, m.updateRegistriesToDBVersion18),
|
2021-09-27 00:52:50 +00:00
|
|
|
|
2022-04-14 04:25:13 +00:00
|
|
|
// 1.22.0
|
|
|
|
newMigration(19, m.updateSettingsToDBVersion19),
|
2021-09-27 00:52:50 +00:00
|
|
|
|
2022-04-14 04:25:13 +00:00
|
|
|
// 1.22.1
|
|
|
|
newMigration(20, m.updateUsersToDBVersion20),
|
|
|
|
newMigration(20, m.updateSettingsToDBVersion20),
|
|
|
|
newMigration(20, m.updateSchedulesToDBVersion20),
|
2021-09-27 00:52:50 +00:00
|
|
|
|
2022-04-14 04:25:13 +00:00
|
|
|
// Portainer 1.23.0
|
|
|
|
// DBVersion 21 is missing as it was shipped as via hotfix 1.22.2
|
|
|
|
newMigration(22, m.updateResourceControlsToDBVersion22),
|
|
|
|
newMigration(22, m.updateUsersAndRolesToDBVersion22),
|
2021-09-27 00:52:50 +00:00
|
|
|
|
2022-04-14 04:25:13 +00:00
|
|
|
// Portainer 1.24.0
|
|
|
|
newMigration(23, m.updateTagsToDBVersion23),
|
|
|
|
newMigration(23, m.updateEndpointsAndEndpointGroupsToDBVersion23),
|
2021-09-27 00:52:50 +00:00
|
|
|
|
2022-04-14 04:25:13 +00:00
|
|
|
// Portainer 1.24.1
|
|
|
|
newMigration(24, m.updateSettingsToDB24),
|
2021-09-27 00:52:50 +00:00
|
|
|
|
2022-04-14 04:25:13 +00:00
|
|
|
// Portainer 2.0.0
|
|
|
|
newMigration(25, m.updateSettingsToDB25),
|
|
|
|
newMigration(25, m.updateStacksToDB24), // yes this looks odd. Don't be tempted to move it
|
2021-09-27 00:52:50 +00:00
|
|
|
|
2022-04-14 04:25:13 +00:00
|
|
|
// Portainer 2.1.0
|
|
|
|
newMigration(26, m.updateEndpointSettingsToDB25),
|
2021-09-27 00:52:50 +00:00
|
|
|
|
2022-04-14 04:25:13 +00:00
|
|
|
// Portainer 2.2.0
|
|
|
|
newMigration(27, m.updateStackResourceControlToDB27),
|
2021-09-27 00:52:50 +00:00
|
|
|
|
2022-04-14 04:25:13 +00:00
|
|
|
// Portainer 2.6.0
|
|
|
|
newMigration(30, m.migrateDBVersionToDB30),
|
2021-09-27 00:52:50 +00:00
|
|
|
|
2022-04-14 04:25:13 +00:00
|
|
|
// Portainer 2.9.0
|
|
|
|
newMigration(32, m.migrateDBVersionToDB32),
|
2021-09-27 00:52:50 +00:00
|
|
|
|
2022-04-14 04:25:13 +00:00
|
|
|
// Portainer 2.9.1, 2.9.2
|
|
|
|
newMigration(33, m.migrateDBVersionToDB33),
|
2021-09-27 00:52:50 +00:00
|
|
|
|
2022-04-14 04:25:13 +00:00
|
|
|
// Portainer 2.10
|
|
|
|
newMigration(34, m.migrateDBVersionToDB34),
|
2021-09-27 00:52:50 +00:00
|
|
|
|
2022-04-14 04:25:13 +00:00
|
|
|
// Portainer 2.9.3 (yep out of order, but 2.10 is EE only)
|
|
|
|
newMigration(35, m.migrateDBVersionToDB35),
|
2021-09-27 00:52:50 +00:00
|
|
|
|
2022-04-14 04:25:13 +00:00
|
|
|
newMigration(36, m.migrateDBVersionToDB36),
|
2022-04-26 11:25:20 +00:00
|
|
|
|
|
|
|
// Portainer 2.13
|
|
|
|
newMigration(40, m.migrateDBVersionToDB40),
|
2022-06-03 04:00:13 +00:00
|
|
|
|
|
|
|
// Portainer 2.14
|
|
|
|
newMigration(50, m.migrateDBVersionToDB50),
|
2022-07-17 23:02:14 +00:00
|
|
|
|
|
|
|
// Portainer 2.15
|
|
|
|
newMigration(60, m.migrateDBVersionToDB60),
|
2022-09-22 20:05:10 +00:00
|
|
|
|
|
|
|
// Portainer 2.16
|
|
|
|
newMigration(70, m.migrateDBVersionToDB70),
|
2022-11-07 23:28:18 +00:00
|
|
|
|
|
|
|
// Portainer 2.16.1
|
|
|
|
newMigration(71, m.migrateDBVersionToDB71),
|
2021-09-27 00:52:50 +00:00
|
|
|
}
|
|
|
|
|
2022-04-14 04:25:13 +00:00
|
|
|
var lastDbVersion int
|
|
|
|
for _, migration := range migrations {
|
|
|
|
if m.currentDBVersion < migration.dbversion {
|
2021-09-28 22:39:45 +00:00
|
|
|
|
2022-04-14 04:25:13 +00:00
|
|
|
// Print the next line only when the version changes
|
|
|
|
if migration.dbversion > lastDbVersion {
|
2022-09-16 16:18:44 +00:00
|
|
|
log.Info().Int("to_version", migration.dbversion).Msg("migrating DB")
|
2022-04-14 04:25:13 +00:00
|
|
|
}
|
2021-09-27 00:52:50 +00:00
|
|
|
|
2022-04-14 04:25:13 +00:00
|
|
|
err := migration.migrate()
|
|
|
|
if err != nil {
|
|
|
|
return migrationError(err, GetFunctionName(migration.migrate))
|
|
|
|
}
|
2021-11-17 00:21:09 +00:00
|
|
|
}
|
2022-04-14 04:25:13 +00:00
|
|
|
lastDbVersion = migration.dbversion
|
2021-11-17 00:21:09 +00:00
|
|
|
}
|
|
|
|
|
2022-09-16 16:18:44 +00:00
|
|
|
log.Info().Int("version", portainer.DBVersion).Msg("setting DB version")
|
|
|
|
|
2021-09-27 00:52:50 +00:00
|
|
|
err = m.versionService.StoreDBVersion(portainer.DBVersion)
|
|
|
|
if err != nil {
|
|
|
|
return migrationError(err, "StoreDBVersion")
|
|
|
|
}
|
2022-09-16 16:18:44 +00:00
|
|
|
|
|
|
|
log.Info().Int("version", portainer.DBVersion).Msg("updated DB version")
|
2021-09-27 00:52:50 +00:00
|
|
|
|
|
|
|
// reset DB updating status
|
|
|
|
return m.versionService.StoreIsUpdating(false)
|
|
|
|
}
|