Jelajahi Sumber

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>
copilot-swe-agent[bot] 2 minggu lalu
induk
melakukan
63221dfec3

File diff ditekan karena terlalu besar
+ 297 - 315
internal/database/repo.go


+ 4 - 3
internal/database/repo_collaboration.go

@@ -5,6 +5,7 @@ import (
 
 	"github.com/cockroachdb/errors"
 	api "github.com/gogs/go-gogs-client"
+	"gorm.io/gorm"
 )
 
 // Collaboration represent the relation between an individual and a repository.
@@ -76,9 +77,9 @@ func (r *Repository) AddCollaborator(u *User) error {
 	return sess.Commit()
 }
 
-func (r *Repository) getCollaborations(e Engine) ([]*Collaboration, error) {
+func (r *Repository) getCollaborations(e *gorm.DB) ([]*Collaboration, error) {
 	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.
@@ -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)
 	if err != nil {
 		return nil, errors.Newf("getCollaborations: %v", err)

+ 0 - 7
internal/database/repositories.go

@@ -28,13 +28,6 @@ func (r *Repository) BeforeUpdate(tx *gorm.DB) error {
 	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 {
 	Permission *api.Permission
 	Parent     *api.Repository

+ 3 - 3
internal/database/two_factor.go

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

Beberapa file tidak ditampilkan karena terlalu banyak file yang berubah dalam diff ini