diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2023-09-19 03:59:19 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-18 22:59:19 +0300 |
commit | e644cc9448a9271bba9789507dcef0984ac02d7f (patch) | |
tree | 0f2df72b86a253272f855139c10c455bcb5909d3 | |
parent | 323135b97b219d7fb10557fb9d9156c6bef3ae62 (diff) | |
download | gitea-e644cc9448a9271bba9789507dcef0984ac02d7f.tar.gz gitea-e644cc9448a9271bba9789507dcef0984ac02d7f.zip |
Fix wrong xorm get usage on migration (#27111)
Fix the bug on try.gitea.io
```log
2023/09/18 01:48:41 ...ations/migrations.go:635:Migrate() [I] Migration[276]: Add RemoteAddress to mirrors
2023/09/18 01:48:41 routers/common/db.go:34:InitDBEngine() [E] ORM engine initialization attempt #7/10 failed. Error: migrate: migration[276]: Add RemoteAddress to mirrors failed: exit status 128 - fatal: not a git repository (or any parent up to mount point /)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
- fatal: not a git repository (or any parent up to mount point /)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
```
Caused by #26952
---------
Co-authored-by: Jason Song <i@wolfogre.com>
-rw-r--r-- | models/migrations/v1_21/v276.go | 33 |
1 files changed, 14 insertions, 19 deletions
diff --git a/models/migrations/v1_21/v276.go b/models/migrations/v1_21/v276.go index 8746c8851e..ed1bc3bda5 100644 --- a/models/migrations/v1_21/v276.go +++ b/models/migrations/v1_21/v276.go @@ -41,6 +41,8 @@ func migratePullMirrors(x *xorm.Engine) error { ID int64 `xorm:"pk autoincr"` RepoID int64 `xorm:"INDEX"` RemoteAddress string `xorm:"VARCHAR(2048)"` + RepoOwner string + RepoName string } sess := x.NewSession() @@ -59,7 +61,9 @@ func migratePullMirrors(x *xorm.Engine) error { for { var mirrors []Mirror - if err := sess.Limit(limit, start).Find(&mirrors); err != nil { + if err := sess.Select("mirror.id, mirror.repo_id, mirror.remote_address, repository.owner_name as repo_owner, repository.name as repo_name"). + Join("INNER", "repository", "repository.id = mirror.repo_id"). + Limit(limit, start).Find(&mirrors); err != nil { return err } @@ -69,7 +73,7 @@ func migratePullMirrors(x *xorm.Engine) error { start += len(mirrors) for _, m := range mirrors { - remoteAddress, err := getRemoteAddress(sess, m.RepoID, "origin") + remoteAddress, err := getRemoteAddress(m.RepoOwner, m.RepoName, "origin") if err != nil { return err } @@ -100,6 +104,8 @@ func migratePushMirrors(x *xorm.Engine) error { RepoID int64 `xorm:"INDEX"` RemoteName string RemoteAddress string `xorm:"VARCHAR(2048)"` + RepoOwner string + RepoName string } sess := x.NewSession() @@ -118,7 +124,9 @@ func migratePushMirrors(x *xorm.Engine) error { for { var mirrors []PushMirror - if err := sess.Limit(limit, start).Find(&mirrors); err != nil { + if err := sess.Select("push_mirror.id, push_mirror.repo_id, push_mirror.remote_name, push_mirror.remote_address, repository.owner_name as repo_owner, repository.name as repo_name"). + Join("INNER", "repository", "repository.id = push_mirror.repo_id"). + Limit(limit, start).Find(&mirrors); err != nil { return err } @@ -128,7 +136,7 @@ func migratePushMirrors(x *xorm.Engine) error { start += len(mirrors) for _, m := range mirrors { - remoteAddress, err := getRemoteAddress(sess, m.RepoID, m.RemoteName) + remoteAddress, err := getRemoteAddress(m.RepoOwner, m.RepoName, m.RemoteName) if err != nil { return err } @@ -153,25 +161,12 @@ func migratePushMirrors(x *xorm.Engine) error { return sess.Commit() } -func getRemoteAddress(sess *xorm.Session, repoID int64, remoteName string) (string, error) { - var ownerName string - var repoName string - has, err := sess. - Table("repository"). - Cols("owner_name", "lower_name"). - Where("id=?", repoID). - Get(&ownerName, &repoName) - if err != nil { - return "", err - } else if !has { - return "", fmt.Errorf("repository [%v] not found", repoID) - } - +func getRemoteAddress(ownerName, repoName, remoteName string) (string, error) { repoPath := filepath.Join(setting.RepoRootPath, strings.ToLower(ownerName), strings.ToLower(repoName)+".git") remoteURL, err := git.GetRemoteAddress(context.Background(), repoPath, remoteName) if err != nil { - return "", err + return "", fmt.Errorf("get remote %s's address of %s/%s failed: %v", remoteName, ownerName, repoName, err) } u, err := giturl.Parse(remoteURL) |