Browse Source

Removes reliance on server specific SQL (#393)

Breaks the retrieval of repositories into two queries
This fetches the paged ids in one go, then the
actual repository information in a second query

Some databases do not support SELECT with *
when group by is used.
tags/v1.21.12.1
btrepp Lunny Xiao 9 years ago
parent
commit
302fa42980
1 changed files with 16 additions and 2 deletions
  1. +16
    -2
      models/org.go

+ 16
- 2
models/org.go View File

@@ -598,15 +598,29 @@ func (org *User) GetUserRepositories(userID int64, page, pageSize int) ([]*Repos
if page <= 0 { if page <= 0 {
page = 1 page = 1
} }

repos := make([]*Repository, 0, pageSize) repos := make([]*Repository, 0, pageSize)


if err := x.Select("`repository`.*").
if err := x.
Select("`repository`.id").
Join("INNER", "team_repo", "`team_repo`.repo_id=`repository`.id"). Join("INNER", "team_repo", "`team_repo`.repo_id=`repository`.id").
Where(cond). Where(cond).
GroupBy("`repository`.id").
GroupBy("`repository`.id,`repository`.updated_unix").
OrderBy("updated_unix DESC"). OrderBy("updated_unix DESC").
Limit(pageSize, (page-1)*pageSize). Limit(pageSize, (page-1)*pageSize).
Find(&repos); err != nil { Find(&repos); err != nil {
return nil, 0, fmt.Errorf("get repository ids: %v", err)
}

repoIDs := make([]int64,pageSize)
for i := range repos {
repoIDs[i] = repos[i].ID
}

if err := x.
Select("`repository`.*").
Where(builder.In("`repository`.id",repoIDs)).
Find(&repos); err!=nil {
return nil, 0, fmt.Errorf("get repositories: %v", err) return nil, 0, fmt.Errorf("get repositories: %v", err)
} }




Loading…
Cancel
Save