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

fix migrations 82 to ignore unsynced tags between database and git data and missing is_archived on repository table (#6387) #6403

Merged
merged 2 commits into from
Mar 21, 2019
Merged
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
46 changes: 40 additions & 6 deletions models/migrations/v82.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
package migrations

import (
"fmt"

"code.gitea.io/git"
"code.gitea.io/gitea/models"

"github.com/go-xorm/xorm"
)

Expand All @@ -18,6 +21,17 @@ func fixReleaseSha1OnReleaseTable(x *xorm.Engine) error {
TagName string
}

type Repository struct {
ID int64
OwnerID int64
Name string
}

type User struct {
ID int64
Name string
}

// Update release sha1
const batchSize = 100
sess := x.NewSession()
Expand All @@ -27,7 +41,8 @@ func fixReleaseSha1OnReleaseTable(x *xorm.Engine) error {
err error
count int
gitRepoCache = make(map[int64]*git.Repository)
repoCache = make(map[int64]*models.Repository)
repoCache = make(map[int64]*Repository)
userCache = make(map[int64]*User)
)

if err = sess.Begin(); err != nil {
Expand All @@ -48,27 +63,46 @@ func fixReleaseSha1OnReleaseTable(x *xorm.Engine) error {
if !ok {
repo, ok := repoCache[release.RepoID]
if !ok {
repo, err = models.GetRepositoryByID(release.RepoID)
repo = new(Repository)
has, err := sess.ID(release.RepoID).Get(repo)
if err != nil {
return err
} else if !has {
return fmt.Errorf("Repository %d is not exist", release.RepoID)
}

repoCache[release.RepoID] = repo
}

gitRepo, err = git.OpenRepository(repo.RepoPath())
user, ok := userCache[repo.OwnerID]
if !ok {
user = new(User)
has, err := sess.ID(repo.OwnerID).Get(user)
if err != nil {
return err
} else if !has {
return fmt.Errorf("User %d is not exist", repo.OwnerID)
}

userCache[repo.OwnerID] = user
}

gitRepo, err = git.OpenRepository(models.RepoPath(user.Name, repo.Name))
if err != nil {
return err
}
gitRepoCache[release.RepoID] = gitRepo
}

release.Sha1, err = gitRepo.GetTagCommitID(release.TagName)
if err != nil {
if err != nil && !git.IsErrNotExist(err) {
return err
}

if _, err = sess.ID(release.ID).Cols("sha1").Update(release); err != nil {
return err
if err == nil {
if _, err = sess.ID(release.ID).Cols("sha1").Update(release); err != nil {
return err
}
}

count++
Expand Down