diff options
author | Joe Chen <jc@unknwon.io> | 2023-02-14 22:44:23 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-14 22:44:23 +0800 |
commit | 0f8c71d3b3fb55b2dad798dcd7594845e5dbe038 (patch) | |
tree | 10a1e87af2b630923c49b6d5a5f2e9ede78aba04 /internal/db/migrations/migrations.go | |
parent | 8f9895acaf43c0141269956aa174d91b7346d5a4 (diff) |
fix(migration): skip v20 if column `sha256` already exists (#7354)
Diffstat (limited to 'internal/db/migrations/migrations.go')
-rw-r--r-- | internal/db/migrations/migrations.go | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/internal/db/migrations/migrations.go b/internal/db/migrations/migrations.go index 664bea5f..04c1e363 100644 --- a/internal/db/migrations/migrations.go +++ b/internal/db/migrations/migrations.go @@ -56,8 +56,17 @@ var migrations = []Migration{ NewMigration("migrate access tokens to store SHA56", migrateAccessTokenToSHA256), // v20 -> v21:v0.13.0 NewMigration("add index to action.user_id", addIndexToActionUserID), + // v21 -> v22:v0.13.0 + // + // NOTE: There was a bug in calculating the value of the `version.version` + // column after a migration is done, thus some instances are on v21 but some are + // on v22. Let's make a noop v22 to make sure every instance will not miss a + // real future migration. + NewMigration("noop", func(*gorm.DB) error { return nil }), } +var errMigrationSkipped = errors.New("the migration has been skipped") + // Migrate migrates the database schema and/or data to the current version. func Migrate(db *gorm.DB) error { // NOTE: GORM has problem migrating tables that happen to have columns with the @@ -121,13 +130,16 @@ In case you're stilling getting this notice, go through instructions again until return db.Where("id = ?", current.ID).Updates(current).Error } - for i, m := range migrations[current.Version-minDBVersion:] { + for _, m := range migrations[current.Version-minDBVersion:] { log.Info("Migration: %s", m.Description()) if err = m.Migrate(db); err != nil { - return errors.Wrap(err, "do migrate") + if err != errMigrationSkipped { + return errors.Wrap(err, "do migrate") + } + log.Trace("The migration %q has been skipped", m.Description()) } - current.Version += int64(i) + 1 + current.Version++ err = db.Where("id = ?", current.ID).Updates(current).Error if err != nil { return errors.Wrap(err, "update the version record") |