gitforge/models/gitea_migrations/v1_9/v82.go
Mathieu Fenniak 29eddd311b chore: upgrade to https://code.forgejo.org/xorm/xorm v1.4.0 (#12639)
Upgrade Forgejo to our forked [xorm v1.4.0](https://code.forgejo.org/xorm/xorm/compare/v1.3.9-forgejo.12...v1.4.0), which is now named `code.forgejo.org/xorm/xorm` to reflect the current expectation that it is a permanent fork.  A small number of API changes were made recently in https://code.forgejo.org/xorm/xorm/issues/120 which are accounted for in this PR, in addition to the module rename.

## Checklist

The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. All work and communication must conform to Forgejo's [AI Agreement](https://codeberg.org/forgejo/governance/src/branch/main/AIAgreement.md). There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org).

### Documentation

- [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change.
- [x] I did not document these changes and I do not expect someone else to do it.

### Release notes

- [ ] This change will be noticed by a Forgejo user or admin (feature, bug fix, performance, etc.). I suggest to include a release note for this change.
- [x] This change is not visible to a Forgejo user or admin (refactor, dependency upgrade, etc.). I think there is no need to add a release note for this change.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/12639
Reviewed-by: Otto <otto@codeberg.org>
2026-05-20 20:20:08 +02:00

133 lines
2.8 KiB
Go

// Copyright 2019 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package v1_9
import (
"fmt"
"path/filepath"
"strings"
"forgejo.org/modules/git"
"forgejo.org/modules/setting"
"code.forgejo.org/xorm/xorm"
)
func FixReleaseSha1OnReleaseTable(x *xorm.Engine) error {
type Release struct {
ID int64
RepoID int64
Sha1 string
TagName string
}
type Repository struct {
ID int64
OwnerID int64
Name string
}
type User struct {
ID int64
Name string
}
// UserPath returns the path absolute path of user repositories.
UserPath := func(userName string) string {
return filepath.Join(setting.RepoRootPath, strings.ToLower(userName))
}
// RepoPath returns repository path by given user and repository name.
RepoPath := func(userName, repoName string) string {
return filepath.Join(UserPath(userName), strings.ToLower(repoName)+".git")
}
// Update release sha1
const batchSize = 100
sess := x.NewSession()
defer sess.Close()
var (
err error
count int
gitRepoCache = make(map[int64]*git.Repository)
repoCache = make(map[int64]*Repository)
userCache = make(map[int64]*User)
)
if err = sess.Begin(); err != nil {
return err
}
for start := 0; ; start += batchSize {
releases := make([]*Release, 0, batchSize)
if err = sess.Limit(batchSize, start).Asc("id").Where("is_tag=?", false).Find(&releases); err != nil {
return err
}
if len(releases) == 0 {
break
}
for _, release := range releases {
gitRepo, ok := gitRepoCache[release.RepoID]
if !ok {
repo, ok := repoCache[release.RepoID]
if !ok {
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
}
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(git.DefaultContext, RepoPath(user.Name, repo.Name))
if err != nil {
return err
}
defer gitRepo.Close()
gitRepoCache[release.RepoID] = gitRepo
}
release.Sha1, err = gitRepo.GetTagCommitID(release.TagName)
if err != nil && !git.IsErrNotExist(err) {
return err
}
if err == nil {
if _, err = sess.ID(release.ID).Cols("sha1").Update(release); err != nil {
return err
}
}
count++
if count >= 1000 {
if err = sess.Commit(); err != nil {
return err
}
if err = sess.Begin(); err != nil {
return err
}
count = 0
}
}
}
return sess.Commit()
}