Complete XORM to GORM migration for internal/database/repo.go

- Replace import "xorm.io/xorm" with "gorm.io/gorm"
- Replace all Engine parameters with *gorm.DB
- Replace *xorm.Session parameters with *gorm.DB
- Convert all database queries from XORM to GORM syntax:
  - e.ID(id).Get(&model) → db.First(&model, id).Error
  - e.Insert(&model) → db.Create(&model).Error
  - e.Update(&model) → db.Updates(&model).Error
  - e.In("col", values) → db.Where("col IN ?", values)
  - e.Find(&results) → db.Find(&results).Error
  - e.Iterate() → db.FindInBatches()
- Convert transaction patterns from NewSession/Begin/Commit to db.Transaction()
- Replace global x variable with db
- Convert XORM AfterSet callback to GORM AfterFind hook
- Handle gorm.ErrRecordNotFound for missing records
- Fix related files: repositories.go, repo_collaboration.go, two_factor.go

Co-authored-by: unknwon <2946214+unknwon@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2026-01-24 16:00:27 +00:00
parent d2ff1fa978
commit 63221dfec3
4 changed files with 484 additions and 473 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -5,6 +5,7 @@ import (
"github.com/cockroachdb/errors" "github.com/cockroachdb/errors"
api "github.com/gogs/go-gogs-client" api "github.com/gogs/go-gogs-client"
"gorm.io/gorm"
) )
// Collaboration represent the relation between an individual and a repository. // Collaboration represent the relation between an individual and a repository.
@@ -76,9 +77,9 @@ func (r *Repository) AddCollaborator(u *User) error {
return sess.Commit() return sess.Commit()
} }
func (r *Repository) getCollaborations(e Engine) ([]*Collaboration, error) { func (r *Repository) getCollaborations(e *gorm.DB) ([]*Collaboration, error) {
collaborations := make([]*Collaboration, 0) collaborations := make([]*Collaboration, 0)
return collaborations, e.Find(&collaborations, &Collaboration{RepoID: r.ID}) return collaborations, e.Where("repo_id = ?", r.ID).Find(&collaborations).Error
} }
// Collaborator represents a user with collaboration details. // Collaborator represents a user with collaboration details.
@@ -98,7 +99,7 @@ func (c *Collaborator) APIFormat() *api.Collaborator {
} }
} }
func (r *Repository) getCollaborators(e Engine) ([]*Collaborator, error) { func (r *Repository) getCollaborators(e *gorm.DB) ([]*Collaborator, error) {
collaborations, err := r.getCollaborations(e) collaborations, err := r.getCollaborations(e)
if err != nil { if err != nil {
return nil, errors.Newf("getCollaborations: %v", err) return nil, errors.Newf("getCollaborations: %v", err)

View File

@@ -28,13 +28,6 @@ func (r *Repository) BeforeUpdate(tx *gorm.DB) error {
return nil return nil
} }
// AfterFind implements the GORM query hook.
func (r *Repository) AfterFind(_ *gorm.DB) error {
r.Created = time.Unix(r.CreatedUnix, 0).Local()
r.Updated = time.Unix(r.UpdatedUnix, 0).Local()
return nil
}
type RepositoryAPIFormatOptions struct { type RepositoryAPIFormatOptions struct {
Permission *api.Permission Permission *api.Permission
Parent *api.Repository Parent *api.Repository

View File

@@ -8,6 +8,7 @@ import (
"github.com/cockroachdb/errors" "github.com/cockroachdb/errors"
"github.com/pquerna/otp/totp" "github.com/pquerna/otp/totp"
"github.com/unknwon/com" "github.com/unknwon/com"
"gorm.io/gorm"
"gogs.io/gogs/internal/conf" "gogs.io/gogs/internal/conf"
"gogs.io/gogs/internal/cryptoutil" "gogs.io/gogs/internal/cryptoutil"
@@ -67,9 +68,8 @@ func GetRecoveryCodesByUserID(userID int64) ([]*TwoFactorRecoveryCode, error) {
return recoveryCodes, x.Where("user_id = ?", userID).Find(&recoveryCodes) return recoveryCodes, x.Where("user_id = ?", userID).Find(&recoveryCodes)
} }
func deleteRecoveryCodesByUserID(e Engine, userID int64) error { func deleteRecoveryCodesByUserID(e *gorm.DB, userID int64) error {
_, err := e.Where("user_id = ?", userID).Delete(new(TwoFactorRecoveryCode)) return e.Where("user_id = ?", userID).Delete(&TwoFactorRecoveryCode{}).Error
return err
} }
// RegenerateRecoveryCodes regenerates new set of recovery codes for given user. // RegenerateRecoveryCodes regenerates new set of recovery codes for given user.