Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: backport migration #10625

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 44 additions & 6 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ func (m *migration) Migrate(x *xorm.Engine) error {

// Version describes the version table. Should have only one row with id==1
type Version struct {
ID int64 `xorm:"pk autoincr"`
Version int64
ID int64 `xorm:"pk autoincr"`
Version int64
SubVersion int64
}

func emptyMigration(x *xorm.Engine) error {
Expand Down Expand Up @@ -292,6 +293,18 @@ var migrations = []Migration{
NewMigration("Add block on rejected reviews branch protection", addBlockOnRejectedReviews),
}

// Backports are only applied after latest version in 'migrations'.
// This list must be **empty** in master, but can contain items in release branches.
// Backported migrations must conform to the following criteria:
// * The function must support being executed multiple times (e.g. when updating to the next
// minor revision and when the actual migration is executed again on the next major upgrade).
// * The function must not rely on data structures that might not be present in the branch
// it is backported to, unless its safe that it will add the column itself.
var backports = []Migration{
// v117 -> v117.1
NewMigration("remove dependencies from deleted repositories", purgeUnusedDependencies),
}

// Migrate database to current version
func Migrate(x *xorm.Engine) error {
if err := x.Sync(new(Version)); err != nil {
Expand All @@ -307,6 +320,7 @@ func Migrate(x *xorm.Engine) error {
// it is a fresh installation and we can skip all migrations.
currentVersion.ID = 0
currentVersion.Version = int64(minDBVersion + len(migrations))
currentVersion.SubVersion = int64(len(backports))

if _, err = x.InsertOne(currentVersion); err != nil {
return fmt.Errorf("insert: %v", err)
Expand All @@ -323,19 +337,43 @@ Please try to upgrade to a lower version (>= v0.6.0) first, then upgrade to curr
if int(v-minDBVersion) > len(migrations) {
// User downgraded Gitea.
currentVersion.Version = int64(len(migrations) + minDBVersion)
// Backports are always considered pending when downgrading
currentVersion.SubVersion = 0
if _, err = x.ID(1).Update(currentVersion); err != nil {
return err
}
} else {
for i, m := range migrations[v-minDBVersion:] {
log.Info("Migration[%d]: %s", v+int64(i), m.Description())
if err = m.Migrate(x); err != nil {
return fmt.Errorf("do migrate: %v", err)
}
currentVersion.Version = v + int64(i) + 1
currentVersion.SubVersion = 0
if _, err = x.ID(1).Update(currentVersion); err != nil {
return err
}
}
}

if currentVersion.SubVersion > int64(len(backports)) {
// Downgraded subversion
_, err = x.ID(1).Update(currentVersion)
return err
}
for i, m := range migrations[v-minDBVersion:] {
log.Info("Migration[%d]: %s", v+int64(i), m.Description())

for sv := currentVersion.SubVersion; sv < int64(len(backports)); sv++ {
m := backports[sv]
log.Info("Migration[%d.%d]: %s", currentVersion.Version, sv, m.Description())
if err = m.Migrate(x); err != nil {
return fmt.Errorf("do migrate: %v", err)
return fmt.Errorf("do backport: %v", err)
}
currentVersion.Version = v + int64(i) + 1
currentVersion.SubVersion = sv
if _, err = x.ID(1).Update(currentVersion); err != nil {
return err
}
}

return nil
}

Expand Down
18 changes: 18 additions & 0 deletions models/migrations/v117.1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package migrations

import (
"xorm.io/xorm"
)

func purgeUnusedDependencies(x *xorm.Engine) error {

if _, err := x.Exec("DELETE FROM issue_dependency WHERE issue_id NOT IN (SELECT id FROM issue)"); err != nil {
return err
}
_, err := x.Exec("DELETE FROM issue_dependency WHERE dependency_id NOT IN (SELECT id FROM issue)")
return err
}