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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
// Copyright 2019 Northern.tech AS
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package migrate
import (
"context"
"fmt"
"sort"
"strings"
"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/mongo"
"github.com/mendersoftware/go-lib-micro/log"
)
var (
ErrNeedsMigration = "db needs migration"
)
func IsErrNeedsMigration(e error) bool {
return strings.HasPrefix(e.Error(), ErrNeedsMigration)
}
// SimpleMigratior applies migrations by comparing `Version` of migrations
// passed to Apply() and already applied migrations. Only migrations that are of
// version higher than the last applied migration will be run. For example:
//
// already applied migrations: 1.0.0, 1.0.1, 1.0.2
// migrations in Apply(): 1.0.1, 1.0.3, 1.1.0
// migrations that will be applied: 1.0.3, 1.1.0
//
type SimpleMigrator struct {
Client *mongo.Client
Db string
Automigrate bool
}
// Apply will apply migrations, provided that Automigrate is on. After each successful migration a new migration
// record will be added to DB with the version of migration that was just
// applied. If a migration fails, Apply() returns an error and does not add a
// migration record (so last migration that is recorded is N-1).
//
// Apply() will log some messages when running. Logger will be extracted from
// context using go-lib-micro/log.LoggerContextKey as key.
// If Automigrate is off, the migrator will just check if the DB is up-to-date, and return with ErrNeedsMigration otherwise.
// Check for it with IsErrNeedsMigration.
func (m *SimpleMigrator) Apply(ctx context.Context, target Version, migrations []Migration) error {
l := log.FromContext(ctx).F(log.Ctx{"db": m.Db})
sort.Slice(migrations, func(i int, j int) bool {
return VersionIsLess(migrations[i].Version(), migrations[j].Version())
})
applied, err := GetMigrationInfo(ctx, m.Client, m.Db)
if err != nil {
return errors.Wrap(err, "failed to list applied migrations")
}
// starts at 0.0.0
last := Version{}
if len(applied) != 0 {
// sort applied migrations wrt. version
sort.Slice(applied, func(i int, j int) bool {
return VersionIsLess(applied[i].Version, applied[j].Version)
})
// last version from already applied migrations
last = applied[len(applied)-1].Version
}
// if Automigrate is disabled - just check
// if the last applied migration is lower than the target one
if !m.Automigrate {
if VersionIsLess(last, target) {
return fmt.Errorf(ErrNeedsMigration+": %s has version %s, needs version %s", m.Db, last.String(), target.String())
} else {
return nil
}
}
// try to apply migrations
for _, migration := range migrations {
mv := migration.Version()
if VersionIsLess(target, mv) {
l.Warnf("migration to version %s skipped, target version %s is lower",
mv, target)
} else if VersionIsLess(last, mv) {
// log, migration applied
l.Infof("applying migration from version %s to %s",
last, mv)
// apply migration
if err := migration.Up(last); err != nil {
l.Errorf("migration from %s to %s failed: %s",
last, mv, err)
// migration from last to migration.Version() failed: err
return errors.Wrapf(err,
"failed to apply migration from %s to %s",
last, mv)
}
if err := UpdateMigrationInfo(ctx, mv, m.Client, m.Db); err != nil {
return errors.Wrapf(err,
"failed to record migration from %s to %s",
last, mv)
}
last = mv
} else {
// log migration already applied
l.Infof("migration to version %s skipped", mv)
}
}
// ideally, when all migrations have completed, DB should be in `target` version
if VersionIsLess(last, target) {
l.Warnf("last migration to version %s did not produce target version %s",
last, target)
// record DB version anyways
if err := UpdateMigrationInfo(ctx, target, m.Client, m.Db); err != nil {
return errors.Wrapf(err,
"failed to record migration from %s to %s",
last, target)
}
} else {
l.Infof("DB migrated to version %s", target)
}
return nil
}
|